GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-d6dec75dd4
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 
21 int datetime_change_timezone(DateTime *dt, int minutes)
22 { /* new timezone in minutes */
23  int stat;
24  int old_minutes, diff_minutes;
25  DateTime incr;
26 
27  stat = datetime_get_timezone(dt, &old_minutes);
28  if (stat != 0)
29  return stat;
30  if (!datetime_is_valid_timezone(minutes))
31  return datetime_error(-4, "invalid datetime timezone");
32 
33  /* create a relative minute increment */
35  DATETIME_MINUTE, 0);
36 
37  /* BB - needed to set SIGN here */
38  diff_minutes = minutes - old_minutes;
39  if (diff_minutes >= 0) {
40  datetime_set_minute(&incr, diff_minutes);
41  }
42  else {
43  datetime_invert_sign(&incr);
44  datetime_set_minute(&incr, -diff_minutes);
45  }
46 
47  return datetime_increment(dt, &incr);
48 }
49 
50 /*!
51  * \brief
52  *
53  * Return <b>datetime_change_timezone</b> (dt, 0);
54  *
55  * \param dt
56  * \return int
57  */
58 
60 {
61  return datetime_change_timezone(dt, 0);
62 }
63 
64 /*!
65  * \brief
66  *
67  * tz = abs(tz)
68  * *hour = tz/60
69  * *minute = tz%60
70  * Note: hour,minute are non-negative. Must look at sign of tz itself to see if
71  * the tz is negative offset or not. This routine would be used to format tz for
72  * output. For example if tz=-350 this would be hour=5 minute=50, but negative.
73  * Output might encode this as -0550: printf ("%s%02d%02d", tz<0?"-":"",
74  * hour, minute)
75  *
76  * \param tz
77  * \param hours
78  * \param minutes
79  * \return void
80  */
81 
82 void datetime_decompose_timezone(int tz, int *hours, int *minutes)
83 {
84  if (tz < 0)
85  tz = -tz;
86 
87  *hours = tz / 60;
88  *minutes = tz % 60;
89 }
#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:106
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:46
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:76
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:68
int datetime_set_minute(DateTime *dt, int minute)
returns 0 on success or negative value on error
Definition: values.c:424
int datetime_change_to_utc(DateTime *dt)
Return datetime_change_timezone (dt, 0);.
Definition: tz2.c:59
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:21
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:82