GRASS GIS 8 Programmer's Manual  8.4.0dev(2024)-112dd97adf
datetime/format.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 <stdio.h>
8 #include <string.h>
9 #include <grass/datetime.h>
10 
11 static char *months[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
12  "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
13 
14 /*!
15  * \brief
16  *
17  * formats DateTime structure as a human-readable string
18  * returns 0 when successful and 'buf' is filled with the string
19  * returns a negative number on error
20  *
21  * \param dt
22  * \param buf
23  * \return int
24  */
25 
26 int datetime_format(const DateTime *dt, char *buf)
27 {
28  /* Format the DateTime structure as a human-readable string */
29  /* Returns 0 when successful, and buf is filled with the
30  formatted data.
31  Returns a negative number as an error code if the DateTime
32  structure is not valid.
33  */
34  char temp[128];
35  int n;
36  double sec;
37 
38  *buf = 0;
39  if (!datetime_is_valid_type(dt))
40  return datetime_error_code();
41 
42  if (datetime_is_absolute(dt)) {
43  if (datetime_get_day(dt, &n) == 0) {
44  sprintf(temp, "%d", n);
45  strcat(buf, temp);
46  }
47 
48  if (datetime_get_month(dt, &n) == 0) {
49  if (*buf)
50  strcat(buf, " ");
51  strcat(buf, months[n - 1]);
52  }
53 
54  if (datetime_get_year(dt, &n) == 0) {
55  if (*buf)
56  strcat(buf, " ");
57  sprintf(temp, "%d", n);
58  strcat(buf, temp);
59  if (datetime_is_negative(dt))
60  strcat(buf, " bc");
61  }
62 
63  if (datetime_get_hour(dt, &n) == 0) {
64  if (*buf)
65  strcat(buf, " ");
66  sprintf(temp, "%02d", n);
67  strcat(buf, temp);
68  }
69 
70  if (datetime_get_minute(dt, &n) == 0) {
71  if (*buf)
72  strcat(buf, ":");
73  sprintf(temp, "%02d", n);
74  strcat(buf, temp);
75  }
76 
77  if (datetime_get_second(dt, &sec) == 0) {
78  if (*buf)
79  strcat(buf, ":");
80  if (datetime_get_fracsec(dt, &n) != 0)
81  n = 0;
82  sprintf(temp, "%02.*f", n, sec);
83  strcat(buf, temp);
84  }
85 
86  if (datetime_get_timezone(dt, &n) == 0) {
87  int hour, minute;
88 
89  if (*buf)
90  strcat(buf, " ");
91  datetime_decompose_timezone(n, &hour, &minute);
92  sprintf(temp, "%s%02d%02d", n < 0 ? "-" : "+", hour, minute);
93  strcat(buf, temp);
94  }
95  }
96 
97  if (datetime_is_relative(dt)) {
98  if (datetime_is_negative(dt))
99  strcat(buf, "-");
100 
101  if (datetime_get_year(dt, &n) == 0) {
102  if (*buf)
103  strcat(buf, " ");
104  sprintf(temp, "%d year%s", n, n == 1 ? "" : "s");
105  strcat(buf, temp);
106  }
107 
108  if (datetime_get_month(dt, &n) == 0) {
109  if (*buf)
110  strcat(buf, " ");
111  sprintf(temp, "%d month%s", n, n == 1 ? "" : "s");
112  strcat(buf, temp);
113  }
114 
115  if (datetime_get_day(dt, &n) == 0) {
116  if (*buf)
117  strcat(buf, " ");
118  sprintf(temp, "%d day%s", n, n == 1 ? "" : "s");
119  strcat(buf, temp);
120  }
121 
122  if (datetime_get_hour(dt, &n) == 0) {
123  if (*buf)
124  strcat(buf, " ");
125  sprintf(temp, "%d hour%s", n, n == 1 ? "" : "s");
126  strcat(buf, temp);
127  }
128 
129  if (datetime_get_minute(dt, &n) == 0) {
130  if (*buf)
131  strcat(buf, " ");
132  sprintf(temp, "%d minute%s", n, n == 1 ? "" : "s");
133  strcat(buf, temp);
134  }
135 
136  if (datetime_get_second(dt, &sec) == 0) {
137  if (*buf)
138  strcat(buf, " ");
139  if (datetime_get_fracsec(dt, &n) != 0)
140  n = 0;
141  sprintf(temp, "%.*f second%s", n, sec,
142  (sec == 1.0 && n == 0) ? "" : "s");
143  strcat(buf, temp);
144  }
145  }
146 
147  return 0;
148 }
int datetime_format(const DateTime *dt, char *buf)
formats DateTime structure as a human-readable string returns 0 when successful and 'buf' is filled w...
int datetime_is_negative(const DateTime *dt)
Returns: 1 if the DateTime is negative 0 otherwise.
Definition: sign.c:36
int datetime_get_second(const DateTime *dt, double *second)
returns 0 on success or negative value on error
Definition: values.c:445
int datetime_get_fracsec(const DateTime *dt, int *fracsec)
returns 0 on success or negative value on error
Definition: values.c:487
int datetime_get_timezone(const DateTime *dt, int *minutes)
returns 0 on success
Definition: tz1.c:46
int datetime_is_valid_type(const DateTime *dt)
Returns: 1 if datetime_check_type() returns 0 0 if not.
Definition: datetime/type.c:77
int datetime_error_code(void)
returns an error code
int datetime_is_absolute(const DateTime *dt)
Returns: 1 if dt.mode is absolute 0 if not (even if dt.mode is not defined)
int datetime_get_hour(const DateTime *dt, int *hour)
returns 0 on success or negative value on error
Definition: values.c:361
int datetime_get_year(const DateTime *dt, int *year)
returns 0 on success or negative value on error
Definition: values.c:218
int datetime_is_relative(const DateTime *dt)
Returns: 1 if dt.mode is relative 0 if not (even if dt.mode is not defined)
int datetime_get_minute(const DateTime *dt, int *minute)
returns 0 on success or negative value on error
Definition: values.c:403
int datetime_get_day(const DateTime *dt, int *day)
returns 0 on success or negative value on error
Definition: values.c:312
int datetime_get_month(const DateTime *dt, int *month)
returns 0 on success or negative value on error
Definition: values.c:265
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