GRASS Programmer's Manual
6.4.2(2012)
|
00001 /* 00002 * Copyright (C) 1995. Bill Brown <brown@gis.uiuc.edu> & Michael Shapiro 00003 * 00004 * This program is free software under the GPL (>=v2) 00005 * Read the file GPL.TXT coming with GRASS for details. 00006 */ 00007 #include <time.h> 00008 #include <grass/datetime.h> 00009 00010 extern struct tm *localtime(); 00011 extern struct tm *gmtime(); 00012 00013 /* 00014 ** NOTE: the extern variable "timezone" seems to be treated 00015 ** differently by different OS, and the tm_zone element of struct tm 00016 ** is missing in some OS (IRIX), so we're converting localtime() and 00017 ** gmtime() structures to datetimes, then doing a difference to get the 00018 ** timezone offset. -Bill Brown 5/31/95 00019 */ 00020 00021 00033 int datetime_get_local_timezone(int *minutes) 00034 { 00035 struct tm *local, *gm; 00036 time_t clock; 00037 DateTime dtl, dtg, dtdiff; 00038 00039 time(&clock); 00040 00041 local = localtime(&clock); 00042 00043 datetime_set_type(&dtl, DATETIME_ABSOLUTE, DATETIME_YEAR, DATETIME_SECOND, 00044 0); 00045 00046 /* now put current {year,month,day,hour,minute,second} into local */ 00047 datetime_set_year(&dtl, (int)local->tm_year + 1900); 00048 datetime_set_month(&dtl, (int)local->tm_mon + 1); 00049 datetime_set_day(&dtl, (int)local->tm_mday); 00050 datetime_set_hour(&dtl, (int)local->tm_hour); 00051 datetime_set_minute(&dtl, (int)local->tm_min); 00052 datetime_set_second(&dtl, (double)local->tm_sec); 00053 00054 gm = gmtime(&clock); 00055 00056 datetime_set_type(&dtg, DATETIME_ABSOLUTE, DATETIME_YEAR, DATETIME_SECOND, 00057 0); 00058 00059 /* now put current {year,month,day,hour,minute,second} into gmt */ 00060 datetime_set_year(&dtg, (int)gm->tm_year + 1900); 00061 datetime_set_month(&dtg, (int)gm->tm_mon + 1); 00062 datetime_set_day(&dtg, (int)gm->tm_mday); 00063 datetime_set_hour(&dtg, (int)gm->tm_hour); 00064 datetime_set_minute(&dtg, (int)gm->tm_min); 00065 datetime_set_second(&dtg, (double)gm->tm_sec); 00066 00067 datetime_set_type(&dtdiff, DATETIME_RELATIVE, 00068 DATETIME_DAY, DATETIME_SECOND, 0); 00069 datetime_difference(&dtl, &dtg, &dtdiff); 00070 datetime_change_from_to(&dtdiff, DATETIME_MINUTE, DATETIME_MINUTE, 0); 00071 00072 *minutes = dtdiff.positive ? dtdiff.minute : -dtdiff.minute; 00073 return 0; 00074 } 00075 00076 00087 void datetime_get_local_time(DateTime * dt) 00088 { 00089 time_t clock; 00090 struct tm *local; 00091 00092 /* first set dt to absolute full date */ 00093 datetime_set_type(dt, DATETIME_ABSOLUTE, DATETIME_YEAR, DATETIME_SECOND, 00094 0); 00095 00096 /* get the current date/time */ 00097 time(&clock); 00098 local = localtime(&clock); 00099 00100 /* now put current {year,month,day,hour,minute,second} into dt */ 00101 datetime_set_year(dt, (int)local->tm_year + 1900); 00102 datetime_set_month(dt, (int)local->tm_mon + 1); 00103 datetime_set_day(dt, (int)local->tm_mday); 00104 datetime_set_hour(dt, (int)local->tm_hour); 00105 datetime_set_minute(dt, (int)local->tm_min); 00106 datetime_set_second(dt, (double)local->tm_sec); 00107 }