GRASS 8 Programmer's Manual 8.6.0dev(2026)-f6f2c534ea
Loading...
Searching...
No Matches
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
11static 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 */
25int datetime_format(const DateTime *dt, char *buf)
26{
27 /* Format the DateTime structure as a human-readable string */
28 /* Returns 0 when successful, and buf is filled with the
29 formatted data.
30 Returns a negative number as an error code if the DateTime
31 structure is not valid.
32 */
33 char temp[128];
34 int n;
35 double sec;
36
37 *buf = 0;
39 return datetime_error_code();
40
41 if (datetime_is_absolute(dt)) {
42 if (datetime_get_day(dt, &n) == 0) {
43 snprintf(temp, sizeof(temp), "%d", n);
44 strcat(buf, temp);
45 }
46
47 if (datetime_get_month(dt, &n) == 0) {
48 if (*buf)
49 strcat(buf, " ");
50 strcat(buf, months[n - 1]);
51 }
52
53 if (datetime_get_year(dt, &n) == 0) {
54 if (*buf)
55 strcat(buf, " ");
56 snprintf(temp, sizeof(temp), "%d", n);
57 strcat(buf, temp);
59 strcat(buf, " bc");
60 }
61
62 if (datetime_get_hour(dt, &n) == 0) {
63 if (*buf)
64 strcat(buf, " ");
65 snprintf(temp, sizeof(temp), "%02d", n);
66 strcat(buf, temp);
67 }
68
69 if (datetime_get_minute(dt, &n) == 0) {
70 if (*buf)
71 strcat(buf, ":");
72 snprintf(temp, sizeof(temp), "%02d", n);
73 strcat(buf, temp);
74 }
75
76 if (datetime_get_second(dt, &sec) == 0) {
77 if (*buf)
78 strcat(buf, ":");
79 if (datetime_get_fracsec(dt, &n) != 0)
80 n = 0;
81 snprintf(temp, sizeof(temp), "%02.*f", n, sec);
82 strcat(buf, temp);
83 }
84
85 if (datetime_get_timezone(dt, &n) == 0) {
86 int hour, minute;
87
88 if (*buf)
89 strcat(buf, " ");
90 datetime_decompose_timezone(n, &hour, &minute);
91 snprintf(temp, sizeof(temp), "%s%02d%02d", n < 0 ? "-" : "+", hour,
92 minute);
93 strcat(buf, temp);
94 }
95 }
96
97 if (datetime_is_relative(dt)) {
99 strcat(buf, "-");
100
101 if (datetime_get_year(dt, &n) == 0) {
102 if (*buf)
103 strcat(buf, " ");
104 snprintf(temp, sizeof(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 snprintf(temp, sizeof(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 snprintf(temp, sizeof(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 snprintf(temp, sizeof(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 snprintf(temp, sizeof(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 snprintf(temp, sizeof(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:34
int datetime_get_second(const DateTime *dt, double *second)
returns 0 on success or negative value on error
Definition values.c:427
int datetime_get_fracsec(const DateTime *dt, int *fracsec)
returns 0 on success or negative value on error
Definition values.c:467
int datetime_get_timezone(const DateTime *dt, int *minutes)
returns 0 on success
Definition tz1.c:44
int datetime_is_valid_type(const DateTime *dt)
Returns: 1 if datetime_check_type() returns 0 0 if not.
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:347
int datetime_get_year(const DateTime *dt, int *year)
returns 0 on success or negative value on error
Definition values.c:210
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:387
int datetime_get_day(const DateTime *dt, int *day)
returns 0 on success or negative value on error
Definition values.c:300
int datetime_get_month(const DateTime *dt, int *month)
returns 0 on success or negative value on error
Definition values.c:255
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