GRASS GIS 8 Programmer's Manual  8.5.0dev(2025)-a277d8547c
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  snprintf(temp, sizeof(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  snprintf(temp, sizeof(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  snprintf(temp, sizeof(temp), "%02d", n);
67  strcat(buf, temp);
68  }
69 
70  if (datetime_get_minute(dt, &n) == 0) {
71  if (*buf)
72  strcat(buf, ":");
73  snprintf(temp, sizeof(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  snprintf(temp, sizeof(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  snprintf(temp, sizeof(temp), "%s%02d%02d", n < 0 ? "-" : "+", hour,
93  minute);
94  strcat(buf, temp);
95  }
96  }
97 
98  if (datetime_is_relative(dt)) {
99  if (datetime_is_negative(dt))
100  strcat(buf, "-");
101 
102  if (datetime_get_year(dt, &n) == 0) {
103  if (*buf)
104  strcat(buf, " ");
105  snprintf(temp, sizeof(temp), "%d year%s", n, n == 1 ? "" : "s");
106  strcat(buf, temp);
107  }
108 
109  if (datetime_get_month(dt, &n) == 0) {
110  if (*buf)
111  strcat(buf, " ");
112  snprintf(temp, sizeof(temp), "%d month%s", n, n == 1 ? "" : "s");
113  strcat(buf, temp);
114  }
115 
116  if (datetime_get_day(dt, &n) == 0) {
117  if (*buf)
118  strcat(buf, " ");
119  snprintf(temp, sizeof(temp), "%d day%s", n, n == 1 ? "" : "s");
120  strcat(buf, temp);
121  }
122 
123  if (datetime_get_hour(dt, &n) == 0) {
124  if (*buf)
125  strcat(buf, " ");
126  snprintf(temp, sizeof(temp), "%d hour%s", n, n == 1 ? "" : "s");
127  strcat(buf, temp);
128  }
129 
130  if (datetime_get_minute(dt, &n) == 0) {
131  if (*buf)
132  strcat(buf, " ");
133  snprintf(temp, sizeof(temp), "%d minute%s", n, n == 1 ? "" : "s");
134  strcat(buf, temp);
135  }
136 
137  if (datetime_get_second(dt, &sec) == 0) {
138  if (*buf)
139  strcat(buf, " ");
140  if (datetime_get_fracsec(dt, &n) != 0)
141  n = 0;
142  snprintf(temp, sizeof(temp), "%.*f second%s", n, sec,
143  (sec == 1.0 && n == 0) ? "" : "s");
144  strcat(buf, temp);
145  }
146  }
147 
148  return 0;
149 }
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