GRASS GIS 8 Programmer's Manual  8.2.2dev(2023)-3d2c704037
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 
22 /*!
23  * \brief
24  *
25  * Returns:
26  * 0 OK
27  * -1 local timezone info not available
28  *
29  * \param minutes
30  * \return int
31  */
32 
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 
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 
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 
69  datetime_difference(&dtl, &dtg, &dtdiff);
71 
72  *minutes = dtdiff.positive ? dtdiff.minute : -dtdiff.minute;
73  return 0;
74 }
75 
76 
77 /*!
78  * \brief
79  *
80  * set mode/from/to ABSOLUTE/YEAR/SECOND
81  * set the local time into 'dt' does not set timezone.
82  *
83  * \param dt
84  * \return void
85  */
86 
88 {
89  time_t clock;
90  struct tm *local;
91 
92  /* first set dt to absolute full date */
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 }
#define DATETIME_SECOND
Definition: datetime.h:15
int datetime_set_second(DateTime *dt, double second)
returns 0 on success or negative value on error
Definition: values.c:486
int datetime_set_day(DateTime *dt, int day)
if dt.mode = ABSOLUTE, then the dt.year, dt.month:
Definition: values.c:354
#define DATETIME_MINUTE
Definition: datetime.h:14
#define DATETIME_RELATIVE
Definition: datetime.h:5
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
struct tm * localtime()
struct tm * gmtime()
#define DATETIME_YEAR
Definition: datetime.h:10
int positive
Definition: datetime.h:25
int datetime_set_hour(DateTime *dt, int hour)
returns 0 on success or negative value on error
Definition: values.c:398
int datetime_set_year(DateTime *dt, int year)
if dt.mode = ABSOLUTE, this also sets dt.day = 0
Definition: values.c:251
#define DATETIME_DAY
Definition: datetime.h:12
int datetime_set_type(DateTime *dt, int mode, int from, int to, int fracsec)
Definition: datetime/type.c:37
int datetime_get_local_timezone(int *minutes)
Returns: 0 OK -1 local timezone info not available.
Definition: local.c:33
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_set_month(DateTime *dt, int month)
if dt.mode = ABSOLUTE, this also sets dt.day = 0
Definition: values.c:300
#define DATETIME_ABSOLUTE
Definition: datetime.h:4
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:55
int minute
Definition: datetime.h:23