GRASS GIS 8 Programmer's Manual 8.4.1(2025)-45ca3179ab
Loading...
Searching...
No Matches
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/*
11 ** NOTE: the extern variable "timezone" seems to be treated
12 ** differently by different OS, and the tm_zone element of struct tm
13 ** is missing in some OS (IRIX), so we're converting localtime() and
14 ** gmtime() structures to datetimes, then doing a difference to get the
15 ** timezone offset. -Bill Brown 5/31/95
16 */
17
18/*!
19 * \brief
20 *
21 * Returns:
22 * 0 OK
23 * -1 local timezone info not available
24 *
25 * \param minutes
26 * \return int
27 */
28
30{
31 struct tm *local, *gm;
32 time_t clock;
33 DateTime dtl, dtg, dtdiff;
34
35 time(&clock);
36
37 local = localtime(&clock);
38
39 datetime_set_type(&dtl, DATETIME_ABSOLUTE, DATETIME_YEAR, DATETIME_SECOND,
40 0);
41
42 /* now put current {year,month,day,hour,minute,second} into local */
43 datetime_set_year(&dtl, (int)local->tm_year + 1900);
44 datetime_set_month(&dtl, (int)local->tm_mon + 1);
45 datetime_set_day(&dtl, (int)local->tm_mday);
46 datetime_set_hour(&dtl, (int)local->tm_hour);
47 datetime_set_minute(&dtl, (int)local->tm_min);
48 datetime_set_second(&dtl, (double)local->tm_sec);
49
50 gm = gmtime(&clock);
51
52 datetime_set_type(&dtg, DATETIME_ABSOLUTE, DATETIME_YEAR, DATETIME_SECOND,
53 0);
54
55 /* now put current {year,month,day,hour,minute,second} into gmt */
56 datetime_set_year(&dtg, (int)gm->tm_year + 1900);
57 datetime_set_month(&dtg, (int)gm->tm_mon + 1);
58 datetime_set_day(&dtg, (int)gm->tm_mday);
59 datetime_set_hour(&dtg, (int)gm->tm_hour);
60 datetime_set_minute(&dtg, (int)gm->tm_min);
61 datetime_set_second(&dtg, (double)gm->tm_sec);
62
63 datetime_set_type(&dtdiff, DATETIME_RELATIVE, DATETIME_DAY, DATETIME_SECOND,
64 0);
65 datetime_difference(&dtl, &dtg, &dtdiff);
66 datetime_change_from_to(&dtdiff, DATETIME_MINUTE, DATETIME_MINUTE, 0);
67
68 *minutes = dtdiff.positive ? dtdiff.minute : -dtdiff.minute;
69 return 0;
70}
71
72/*!
73 * \brief
74 *
75 * set mode/from/to ABSOLUTE/YEAR/SECOND
76 * set the local time into 'dt' does not set timezone.
77 *
78 * \param dt
79 * \return void
80 */
81
82void datetime_get_local_time(DateTime *dt)
83{
84 time_t clock;
85 struct tm *local;
86
87 /* first set dt to absolute full date */
88 datetime_set_type(dt, DATETIME_ABSOLUTE, DATETIME_YEAR, DATETIME_SECOND, 0);
89
90 /* get the current date/time */
91 time(&clock);
92 local = localtime(&clock);
93
94 /* now put current {year,month,day,hour,minute,second} into dt */
95 datetime_set_year(dt, (int)local->tm_year + 1900);
96 datetime_set_month(dt, (int)local->tm_mon + 1);
97 datetime_set_day(dt, (int)local->tm_mday);
98 datetime_set_hour(dt, (int)local->tm_hour);
99 datetime_set_minute(dt, (int)local->tm_min);
100 datetime_set_second(dt, (double)local->tm_sec);
101}
int datetime_change_from_to(DateTime *dt, int from, int to, int round)
Changes the from/to of the type for dt. The 'from/to' must be legal values for the mode of dt; (if th...
Definition change.c:54
int datetime_difference(const DateTime *a, const DateTime *b, DateTime *result)
This performs the formula: result = a - b;.
Definition diff.c:79
void datetime_get_local_time(DateTime *dt)
set mode/from/to ABSOLUTE/YEAR/SECOND set the local time into 'dt' does not set timezone.
Definition local.c:82
int datetime_get_local_timezone(int *minutes)
Returns: 0 OK -1 local timezone info not available.
Definition local.c:29
int datetime_set_type(DateTime *dt, int mode, int from, int to, int fracsec)
Definition type.c:36
int datetime_set_day(DateTime *dt, int day)
if dt.mode = ABSOLUTE, then the dt.year, dt.month:
Definition values.c:340
int datetime_set_month(DateTime *dt, int month)
if dt.mode = ABSOLUTE, this also sets dt.day = 0
Definition values.c:288
int datetime_set_hour(DateTime *dt, int hour)
returns 0 on success or negative value on error
Definition values.c:382
int datetime_set_year(DateTime *dt, int year)
if dt.mode = ABSOLUTE, this also sets dt.day = 0
Definition values.c:241
int datetime_set_second(DateTime *dt, double second)
returns 0 on success or negative value on error
Definition values.c:466
int datetime_set_minute(DateTime *dt, int minute)
returns 0 on success or negative value on error
Definition values.c:424