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