2.94
xs_time.h
1/* copyright (c) 2022 - 2026 grunfink et al. / MIT license */
2
3#ifndef _XS_TIME_H
4
5#define _XS_TIME_H
6
7#include <time.h>
8
9xs_str *xs_str_time(time_t t, const char *fmt, int local);
10#define xs_str_localtime(t, fmt) xs_str_time(t, fmt, 1)
11#define xs_str_utctime(t, fmt) xs_str_time(t, fmt, 0)
12#define xs_str_iso_date(t) xs_str_time(t, "%Y-%m-%dT%H:%M:%SZ", 0)
13time_t xs_parse_iso_date(const char *iso_date, int local);
14time_t xs_parse_time(const char *str, const char *fmt, int local);
15#define xs_parse_localtime(str, fmt) xs_parse_time(str, fmt, 1)
16#define xs_parse_utctime(str, fmt) xs_parse_time(str, fmt, 0)
17xs_str *xs_str_time_diff(time_t time_diff);
18xs_list *xs_tz_list(void);
19int xs_tz_offset(const char *tz);
20
21#ifdef XS_IMPLEMENTATION
22
23xs_str *xs_str_time(time_t t, const char *fmt, int local)
24/* returns a string with a formated time */
25{
26 struct tm tm;
27 char tmp[64];
28
29 if (t == 0)
30 t = time(NULL);
31
32 if (local)
33 localtime_r(&t, &tm);
34 else
35 gmtime_r(&t, &tm);
36
37 strftime(tmp, sizeof(tmp), fmt, &tm);
38
39 return xs_str_new(tmp);
40}
41
42
43xs_str *xs_str_time_diff(time_t time_diff)
44/* returns time_diff in seconds to 'human' units (d:hh:mm:ss) */
45{
46 int secs = time_diff % 60;
47 int mins = (time_diff /= 60) % 60;
48 int hours = (time_diff /= 60) % 24;
49 int days = (time_diff /= 24);
50
51 return xs_fmt("%d:%02d:%02d:%02d", days, hours, mins, secs);
52}
53
54
55char *strptime(const char *s, const char *format, struct tm *tm);
56
57time_t xs_parse_time(const char *str, const char *fmt, int local)
58{
59 time_t t = 0;
60
61#ifndef WITHOUT_STRPTIME
62
63 struct tm tm = {0};
64
65 strptime(str, fmt, &tm);
66
67 /* try to guess the Daylight Saving Time */
68 if (local)
69 tm.tm_isdst = -1;
70
71 t = local ? mktime(&tm) : timegm(&tm);
72
73#endif /* WITHOUT_STRPTIME */
74
75 return t;
76}
77
78
79time_t xs_parse_iso_date(const char *iso_date, int local)
80/* parses a YYYY-MM-DDTHH:MM:SS date string */
81{
82 time_t t = 0;
83
84#ifndef WITHOUT_STRPTIME
85
86 t = xs_parse_time(iso_date, "%Y-%m-%dT%H:%M:%S", local);
87
88#else /* WITHOUT_STRPTIME */
89
90 struct tm tm = {0};
91
92 if (sscanf(iso_date, "%d-%d-%dT%d:%d:%d",
93 &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
94 &tm.tm_hour, &tm.tm_min, &tm.tm_sec) == 6) {
95
96 tm.tm_year -= 1900;
97 tm.tm_mon -= 1;
98
99 if (local)
100 tm.tm_isdst = -1;
101
102 t = local ? mktime(&tm) : timegm(&tm);
103 }
104
105#endif /* WITHOUT_STRPTIME */
106
107 return t;
108}
109
110
111/** timezones **/
112
113/* intentionally dead simple */
114
115struct {
116 const char *tz; /* timezone name */
117 float h_offset; /* hour offset */
118} xs_tz[] = {
119 { "UTC", 0 },
120 { "WET (Western European Time)", 0 },
121 { "WEST (Western European Summer Time)", 1 },
122 { "CET (Central European Time)", 1 },
123 { "CEST (Central European Summer Time)", 2 },
124 { "EET (Eastern European Time)", 2 },
125 { "EEST (Eastern European Summer Time)", 3 },
126 { "MSK (Moskow Time Zone)", 3 },
127 { "EST (Eastern Time Zone)", -5 },
128 { "AST (Atlantic Time Zone)", -4 },
129 { "ADT (Atlantic Daylight Time Zone)", -3 },
130 { "CST (Central Time Zone)", -6 },
131 { "CDT (Central Daylight Time Zone)", -5 },
132 { "MST (Mountain Time Zone)", -7 },
133 { "MDT (Mountain Daylight Time Zone)", -6 },
134 { "PST (Pacific Time Zone)", -8 },
135 { "PDT (Pacific Daylight Time Zone)", -7 },
136 { "AKST (Alaska Time Zone)", -9 },
137 { "AKDT (Alaska Daylight Time Zone)", -8 },
138 { "China Time Zone", 8 },
139 { "IST (Indian Standard Time)", 5.5 },
140 { "IDT (Israel Daylight Standard Time)", 3 },
141 { "WIB (Western Indonesia Time)", 7 },
142 { "WITA (Central Indonesia Time)", 8 },
143 { "WIT (Eastern Indonesia Time)", 9 },
144 { "AWST (Australian Western Time)", 8 },
145 { "ACST (Australian Eastern Time)", 9.5 },
146 { "ACDT (Australian Daylight Eastern Time)", 10.5 },
147 { "AEST (Australian Eastern Time)", 10 },
148 { "AEDT (Australian Daylight Eastern Time)", 11 },
149 { "NZST (New Zealand Time)", 12 },
150 { "NZDT (New Zealand Daylight Time)", 13 },
151 { "UTC", 0 },
152 { "UTC+1", 1 },
153 { "UTC+2", 2 },
154 { "UTC+3", 3 },
155 { "UTC+4", 4 },
156 { "UTC+5", 5 },
157 { "UTC+6", 6 },
158 { "UTC+7", 7 },
159 { "UTC+8", 8 },
160 { "UTC+9", 9 },
161 { "UTC+10", 10 },
162 { "UTC+11", 11 },
163 { "UTC+12", 12 },
164 { "UTC-1", -1 },
165 { "UTC-2", -2 },
166 { "UTC-3", -3 },
167 { "UTC-4", -4 },
168 { "UTC-5", -5 },
169 { "UTC-6", -6 },
170 { "UTC-7", -7 },
171 { "UTC-8", -8 },
172 { "UTC-9", -9 },
173 { "UTC-10", -10 },
174 { "UTC-11", -11 },
175 { "UTC-12", -12 },
176 { "UTC-13", -13 },
177 { "UTC-14", -14 },
178 { "GMT", 0 },
179 { "GMT+1", 1 },
180 { "GMT+2", 2 },
181 { "GMT+3", 3 },
182 { "GMT+4", 4 },
183 { "GMT+5", 5 },
184 { "GMT+6", 6 },
185 { "GMT+7", 7 },
186 { "GMT+8", 8 },
187 { "GMT+9", 9 },
188 { "GMT+10", 10 },
189 { "GMT+11", 11 },
190 { "GMT+12", 12 },
191 { "GMT-1", -1 },
192 { "GMT-2", -2 },
193 { "GMT-3", -3 },
194 { "GMT-4", -4 },
195 { "GMT-5", -5 },
196 { "GMT-6", -6 },
197 { "GMT-7", -7 },
198 { "GMT-8", -8 },
199 { "GMT-9", -9 },
200 { "GMT-10", -10 },
201 { "GMT-11", -11 },
202 { "GMT-12", -12 },
203 { "GMT-13", -13 },
204 { "GMT-14", -14 },
205 { NULL, 0 }
206};
207
208
209xs_list *xs_tz_list(void)
210/* returns the list of supported timezones */
211{
212 xs_list *l = xs_list_new();
213
214 for (int n = 0; xs_tz[n].tz != NULL; n++)
215 l = xs_list_append(l, xs_tz[n].tz);
216
217 return l;
218}
219
220
221int xs_tz_offset(const char *tz)
222/* returns the offset in seconds from the specified Time Zone to UTC */
223{
224 for (int n = 0; xs_tz[n].tz != NULL; n++) {
225 if (strcmp(xs_tz[n].tz, tz) == 0)
226 return xs_tz[n].h_offset * 3600;
227 }
228
229 return 0;
230}
231
232
233#endif /* XS_IMPLEMENTATION */
234
235#endif /* _XS_TIME_H */