GRASS 8 Programmer's Manual  8.5.0dev(2025)-c070206eb1
tz2.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 <grass/datetime.h>
8 
9 /*!
10  * \brief
11  *
12  * if dt has a timezone, increment dt by minutes-dt.tz MINUTES and set dt.tz =
13  * minutes Returns: 0 OK <b>datetime_check_timezone</b> (dt) if not -4 if
14  * minutes invalid
15  *
16  * \param dt
17  * \param minutes
18  * \return int
19  */
20 int datetime_change_timezone(DateTime *dt, int minutes)
21 { /* new timezone in minutes */
22  int stat;
23  int old_minutes, diff_minutes;
24  DateTime incr;
25 
26  stat = datetime_get_timezone(dt, &old_minutes);
27  if (stat != 0)
28  return stat;
29  if (!datetime_is_valid_timezone(minutes))
30  return datetime_error(-4, "invalid datetime timezone");
31 
32  /* create a relative minute increment */
34  DATETIME_MINUTE, 0);
35 
36  /* BB - needed to set SIGN here */
37  diff_minutes = minutes - old_minutes;
38  if (diff_minutes >= 0) {
39  datetime_set_minute(&incr, diff_minutes);
40  }
41  else {
42  datetime_invert_sign(&incr);
43  datetime_set_minute(&incr, -diff_minutes);
44  }
45 
46  return datetime_increment(dt, &incr);
47 }
48 
49 /*!
50  * \brief
51  *
52  * Return <b>datetime_change_timezone</b> (dt, 0);
53  *
54  * \param dt
55  * \return int
56  */
58 {
59  return datetime_change_timezone(dt, 0);
60 }
61 
62 /*!
63  * \brief
64  *
65  * tz = abs(tz)
66  * *hour = tz/60
67  * *minute = tz%60
68  * Note: hour,minute are non-negative. Must look at sign of tz itself to see if
69  * the tz is negative offset or not. This routine would be used to format tz for
70  * output. For example if tz=-350 this would be hour=5 minute=50, but negative.
71  * Output might encode this as -0550: printf ("%s%02d%02d", tz<0?"-":"",
72  * hour, minute)
73  *
74  * \param tz
75  * \param hours
76  * \param minutes
77  * \return void
78  */
79 void datetime_decompose_timezone(int tz, int *hours, int *minutes)
80 {
81  if (tz < 0)
82  tz = -tz;
83 
84  *hours = tz / 60;
85  *minutes = tz % 60;
86 }
#define DATETIME_MINUTE
Definition: datetime.h:14
#define DATETIME_RELATIVE
Definition: datetime.h:5
int datetime_is_valid_timezone(int minutes)
Returns: 1 OK: -720 <= minutes <= 780 (720 = 12 hours; 780 = 13 hours) 0 NOT OK.
Definition: tz1.c:101
int datetime_error(int code, char *msg)
record 'code' and 'msg' as error code/msg (in static variables) code==0 will clear the error (ie set ...
int datetime_get_timezone(const DateTime *dt, int *minutes)
returns 0 on success
Definition: tz1.c:44
int datetime_set_type(DateTime *dt, int mode, int from, int to, int fracsec)
Definition: datetime/type.c:36
void datetime_invert_sign(DateTime *dt)
Definition: sign.c:71
int datetime_increment(DateTime *src, DateTime *incr)
This function changes the 'src' date/time data based on the 'incr' The type (mode/from/to) of the 'sr...
Definition: incr1.c:67
int datetime_set_minute(DateTime *dt, int minute)
returns 0 on success or negative value on error
Definition: values.c:407
int datetime_change_to_utc(DateTime *dt)
Return datetime_change_timezone (dt, 0);.
Definition: tz2.c:57
int datetime_change_timezone(DateTime *dt, int minutes)
if dt has a timezone, increment dt by minutes-dt.tz MINUTES and set dt.tz = minutes Returns: 0 OK dat...
Definition: tz2.c:20
void datetime_decompose_timezone(int tz, int *hours, int *minutes)
tz = abs(tz) *hour = tz/60 *minute = tz%60 Note: hour,minute are non-negative. Must look at sign of t...
Definition: tz2.c:79