GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
local.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1995. Bill Brown <brown@gis.uiuc.edu> & Michael Shapiro
3  *
4  * This program is free software under the GPL (>=v2)
5  * Read the file GPL.TXT coming with GRASS for details.
6  */
7 #include <time.h>
8 #include <grass/datetime.h>
9 
10 extern struct tm *localtime();
11 extern struct tm *gmtime();
12 
13 /*
14  ** NOTE: the extern variable "timezone" seems to be treated
15  ** differently by different OS, and the tm_zone element of struct tm
16  ** is missing in some OS (IRIX), so we're converting localtime() and
17  ** gmtime() structures to datetimes, then doing a difference to get the
18  ** timezone offset. -Bill Brown 5/31/95
19  */
20 
21 
33 int datetime_get_local_timezone(int *minutes)
34 {
35  struct tm *local, *gm;
36  time_t clock;
37  DateTime dtl, dtg, dtdiff;
38 
39  time(&clock);
40 
41  local = localtime(&clock);
42 
43  datetime_set_type(&dtl, DATETIME_ABSOLUTE, DATETIME_YEAR, DATETIME_SECOND,
44  0);
45 
46  /* now put current {year,month,day,hour,minute,second} into local */
47  datetime_set_year(&dtl, (int)local->tm_year + 1900);
48  datetime_set_month(&dtl, (int)local->tm_mon + 1);
49  datetime_set_day(&dtl, (int)local->tm_mday);
50  datetime_set_hour(&dtl, (int)local->tm_hour);
51  datetime_set_minute(&dtl, (int)local->tm_min);
52  datetime_set_second(&dtl, (double)local->tm_sec);
53 
54  gm = gmtime(&clock);
55 
56  datetime_set_type(&dtg, DATETIME_ABSOLUTE, DATETIME_YEAR, DATETIME_SECOND,
57  0);
58 
59  /* now put current {year,month,day,hour,minute,second} into gmt */
60  datetime_set_year(&dtg, (int)gm->tm_year + 1900);
61  datetime_set_month(&dtg, (int)gm->tm_mon + 1);
62  datetime_set_day(&dtg, (int)gm->tm_mday);
63  datetime_set_hour(&dtg, (int)gm->tm_hour);
64  datetime_set_minute(&dtg, (int)gm->tm_min);
65  datetime_set_second(&dtg, (double)gm->tm_sec);
66 
67  datetime_set_type(&dtdiff, DATETIME_RELATIVE,
68  DATETIME_DAY, DATETIME_SECOND, 0);
69  datetime_difference(&dtl, &dtg, &dtdiff);
70  datetime_change_from_to(&dtdiff, DATETIME_MINUTE, DATETIME_MINUTE, 0);
71 
72  *minutes = dtdiff.positive ? dtdiff.minute : -dtdiff.minute;
73  return 0;
74 }
75 
76 
87 void datetime_get_local_time(DateTime * dt)
88 {
89  time_t clock;
90  struct tm *local;
91 
92  /* first set dt to absolute full date */
93  datetime_set_type(dt, DATETIME_ABSOLUTE, DATETIME_YEAR, DATETIME_SECOND,
94  0);
95 
96  /* get the current date/time */
97  time(&clock);
98  local = localtime(&clock);
99 
100  /* now put current {year,month,day,hour,minute,second} into dt */
101  datetime_set_year(dt, (int)local->tm_year + 1900);
102  datetime_set_month(dt, (int)local->tm_mon + 1);
103  datetime_set_day(dt, (int)local->tm_mday);
104  datetime_set_hour(dt, (int)local->tm_hour);
105  datetime_set_minute(dt, (int)local->tm_min);
106  datetime_set_second(dt, (double)local->tm_sec);
107 }
int datetime_set_hour(DateTime *dt, int hour)
returns 0 on success or negative value on error
Definition: values.c:398
int datetime_set_day(DateTime *dt, int day)
if dt.mode = ABSOLUTE, then the dt.year, dt.month:
Definition: values.c:354
int datetime_set_type(DateTime *dt, int mode, int from, int to, int fracsec)
Definition: datetime/type.c:37
int datetime_set_year(DateTime *dt, int year)
if dt.mode = ABSOLUTE, this also sets dt.day = 0
Definition: values.c:251
void datetime_get_local_time(DateTime *dt)
set mode/from/to ABSOLUTE/YEAR/SECOND set the local time into &#39;dt&#39; does not set timezone.
Definition: local.c:87
int datetime_change_from_to(DateTime *dt, int from, int to, int round)
Changes the from/to of the type for dt. The &#39;from/to&#39; must be legal values for the mode of dt; (if th...
Definition: change.c:54
struct tm * localtime()
struct tm * gmtime()
int datetime_set_month(DateTime *dt, int month)
if dt.mode = ABSOLUTE, this also sets dt.day = 0
Definition: values.c:300
int datetime_set_second(DateTime *dt, double second)
returns 0 on success or negative value on error
Definition: values.c:486
int datetime_set_minute(DateTime *dt, int minute)
returns 0 on success or negative value on error
Definition: values.c:442
int datetime_difference(const DateTime *a, const DateTime *b, DateTime *result)
This performs the formula: result = a - b;.
Definition: diff.c:81
int datetime_get_local_timezone(int *minutes)
Returns: 0 OK -1 local timezone info not available.
Definition: local.c:33