Debian Codegraph @

garglk/cheapglk/cgdate.cpp

raw
1 /******************************************************************************
2 * *
3 * Copyright (C) 2011 by Andrew Plotkin, Ben Cressey. *
4 * *
5 * This file is part of Gargoyle. *
6 * *
7 * Gargoyle is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation; either version 2 of the License, or *
10 * (at your option) any later version. *
11 * *
12 * Gargoyle is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU General Public License *
18 * along with Gargoyle; if not, write to the Free Software *
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *
20 * *
21 *****************************************************************************/
22
23 /* cgdate.c: Date and time functions for Glk API.
24 Designed by Andrew Plotkin <[email protected]>
25 http://eblong.com/zarf/glk/
26
27 Portions of this file are copyright (c) 1998-2016, Andrew Plotkin
28 It is distributed under the MIT license; see the "LICENSE" file.
29 */
30
31 #include <stdio.h>
32 #include <string.h>
33 #include <stdlib.h>
34 #include <time.h>
35 #include "glk.h"
36 #include "garglk.h"
37
38 #ifndef _MSC_VER
39 #include <sys/time.h>
40 #endif
41
42 #ifdef GARGLK
43 #ifdef _WIN32
44 #include <windows.h>
45 #endif
46
47 /* timegm() is non-standard (neither C nor POSIX), so unconditionally
48 use a replacement.
49 */
50 #define NO_TIMEGM_AVAIL
51
52 /* ... but due to the mess that is C++ and POSIX, give it a
53 different name, since Unix environments tend to expose everything to
54 C++, standard or otherwise, unconditionally.
55 */
56 #define timegm cg_timegm
57
58 /* bzero() is deprecated */
59 #undef bzero
60 #define bzero(s, n) memset((s), 0, (n))
61 #endif
62
63 #ifdef GLK_MODULE_DATETIME
64
65 #ifdef NO_TIMEGM_AVAIL
66 static time_t timegm(struct tm *tm);
67 #endif /* NO_TIMEGM_AVAIL */
68
69 #ifdef WIN32
70 /* Some alterations to make this code work on Windows, in case that's helpful
71 to you. */
72 #define mktime(tm) gli_mktime(tm)
73 extern time_t timegm(struct tm *tm);
74 extern time_t gli_mktime(struct tm *timeptr);
75 static struct tm *gmtime_r(const time_t *timer, struct tm *result);
76 static struct tm *localtime_r(const time_t *timer, struct tm *result);
77
78 #ifdef GARGLK
79 /* These functions are POSIX but aren't supplied by MinGW. */
80 static int setenv(const char *name, const char *value, int overwrite)
81 {
82 SetEnvironmentVariableA(name, value);
83 return 0;
84 }
85 static int unsetenv(const char *name)
86 {
87 SetEnvironmentVariableA(name, NULL);
88 return 0;
89 }
90 #endif
91 #endif /* WIN32 */
92
93
94 /* Copy a POSIX tm structure to a glkdate. */
95 static void gli_date_from_tm(glkdate_t *date, struct tm *tm)
96 {
97 date->year = 1900 + tm->tm_year;
98 date->month = 1 + tm->tm_mon;
99 date->day = tm->tm_mday;
100 date->weekday = tm->tm_wday;
101 date->hour = tm->tm_hour;
102 date->minute = tm->tm_min;
103 date->second = tm->tm_sec;
104 }
105
106 /* Copy a glkdate to a POSIX tm structure.
107 This is used in the "glk_date_to_..." functions, which are supposed
108 to normalize the glkdate. We're going to rely on the mktime() /
109 timegm() functions to do that -- except they don't handle microseconds.
110 So we'll have to do that normalization here, adjust the tm_sec value,
111 and return the normalized number of microseconds.
112 */
113 static glsi32 gli_date_to_tm(glkdate_t *date, struct tm *tm)
114 {
115 glsi32 microsec;
116
117 bzero(tm, sizeof(*tm));
118 tm->tm_year = date->year - 1900;
119 tm->tm_mon = date->month - 1;
120 tm->tm_mday = date->day;
121 tm->tm_wday = date->weekday;
122 tm->tm_hour = date->hour;
123 tm->tm_min = date->minute;
124 tm->tm_sec = date->second;
125 microsec = date->microsec;
126
127 if (microsec >= 1000000) {
128 tm->tm_sec += (microsec / 1000000);
129 microsec = microsec % 1000000;
130 }
131 else if (microsec < 0) {
132 microsec = -1 - microsec;
133 tm->tm_sec -= (1 + microsec / 1000000);
134 microsec = 999999 - (microsec % 1000000);
135 }
136
137 return microsec;
138 }
139
140 /* Convert a Unix timestamp, along with a microseconds value, to
141 a glktimeval.
142 */
143 static void gli_timestamp_to_time(time_t timestamp, glsi32 microsec,
144 glktimeval_t *time)
145 {
146 if (sizeof(timestamp) <= 4) {
147 /* This platform has 32-bit time, but we can't do anything
148 about that. Hope it's not 2038 yet. */
149 if (timestamp >= 0)
150 time->high_sec = 0;
151 else
152 time->high_sec = -1;
153 time->low_sec = timestamp;
154 }
155 else {
156 /* The cast to int64_t shouldn't be necessary, but it
157 suppresses a pointless warning in the 32-bit case.
158 (Remember that we won't be executing this line in the
159 32-bit case.) */
160 time->high_sec = (((int64_t)timestamp) >> 32) & 0xFFFFFFFF;
161 time->low_sec = timestamp & 0xFFFFFFFF;
162 }
163
164 time->microsec = microsec;
165 }
166
167 /* Divide a Unix timestamp by a (positive) value. */
168 static glsi32 gli_simplify_time(time_t timestamp, glui32 factor)
169 {
170 /* We want to round towards negative infinity, which takes a little
171 bit of fussing. */
172 if (timestamp >= 0) {
173 return timestamp / (time_t)factor;
174 }
175 else {
176 return -1 - (((time_t)-1 - timestamp) / (time_t)factor);
177 }
178 }
179
180 void glk_current_time(glktimeval_t *time)
181 {
182 // TIME_UTC is C++17 so maintain the earlier version where
183 // necessary; and while Gargoyle will soon be switching to C++17,
184 // even with C++17, MinGW doesn't provide TIME_UTC with MSVCRT (it
185 // seems to with UCRT, but version of Windows older than 10 don't
186 // support UCRT out of the box, so we'll need to continue checking
187 // whether TIME_UTC is defined, even in C++17).
188 #if __cplusplus >= 201703L && defined(TIME_UTC)
189 struct timespec ts;
190
191 if (!timespec_get(&ts, TIME_UTC)) {
192 gli_timestamp_to_time(0, 0, time);
193 gli_strict_warning("current_time: timespec_get() failed.");
194 return;
195 }
196
197 gli_timestamp_to_time(ts.tv_sec, ts.tv_nsec/1000, time);
198 #else
199 struct timeval tv;
200 if (gettimeofday(&tv, NULL)) {
201 gli_timestamp_to_time(0, 0, time);
202 gli_strict_warning("current_time: gettimeofday() failed.");
203 return;
204 }
205 gli_timestamp_to_time(tv.tv_sec, tv.tv_usec, time);
206 #endif
207 }
208
209 glsi32 glk_current_simple_time(glui32 factor)
210 {
211 #if __cplusplus >= 201703L && defined(TIME_UTC)
212 struct timespec ts;
213 #endif
214
215 if (factor == 0) {
216 gli_strict_warning("current_simple_time: factor cannot be zero.");
217 return 0;
218 }
219
220 #if __cplusplus >= 201703L && defined(TIME_UTC)
221 if (!timespec_get(&ts, TIME_UTC)) {
222 gli_strict_warning("current_simple_time: timespec_get() failed.");
223 return 0;
224 }
225
226 return gli_simplify_time(ts.tv_sec, factor);
227 #else
228 struct timeval tv;
229 if (gettimeofday(&tv, NULL)) {
230 gli_strict_warning("current_simple_time: gettimeofday() failed.");
231 return 0;
232 }
233 return gli_simplify_time(tv.tv_sec, factor);
234 #endif
235 }
236
237 void glk_time_to_date_utc(glktimeval_t *time, glkdate_t *date)
238 {
239 time_t timestamp;
240 struct tm tm;
241
242 timestamp = time->low_sec;
243 if (sizeof(timestamp) > 4) {
244 timestamp += ((int64_t)time->high_sec << 32);
245 }
246
247 gmtime_r(&timestamp, &tm);
248
249 gli_date_from_tm(date, &tm);
250 date->microsec = time->microsec;
251 }
252
253 void glk_time_to_date_local(glktimeval_t *time, glkdate_t *date)
254 {
255 time_t timestamp;
256 struct tm tm;
257
258 timestamp = time->low_sec;
259 if (sizeof(timestamp) > 4) {
260 timestamp += ((int64_t)time->high_sec << 32);
261 }
262
263 localtime_r(&timestamp, &tm);
264
265 gli_date_from_tm(date, &tm);
266 date->microsec = time->microsec;
267 }
268
269 void glk_simple_time_to_date_utc(glsi32 time, glui32 factor,
270 glkdate_t *date)
271 {
272 time_t timestamp = (time_t)time * factor;
273 struct tm tm;
274
275 gmtime_r(&timestamp, &tm);
276
277 gli_date_from_tm(date, &tm);
278 date->microsec = 0;
279 }
280
281 void glk_simple_time_to_date_local(glsi32 time, glui32 factor,
282 glkdate_t *date)
283 {
284 time_t timestamp = (time_t)time * factor;
285 struct tm tm;
286
287 localtime_r(&timestamp, &tm);
288
289 gli_date_from_tm(date, &tm);
290 date->microsec = 0;
291 }
292
293 void glk_date_to_time_utc(glkdate_t *date, glktimeval_t *time)
294 {
295 time_t timestamp;
296 struct tm tm;
297 glsi32 microsec;
298
299 microsec = gli_date_to_tm(date, &tm);
300 /* The timegm function is not standard POSIX. If it's not available
301 on your platform, try setting the env var "TZ" to "", calling
302 mktime(), and then resetting "TZ". */
303 tm.tm_isdst = 0;
304 timestamp = timegm(&tm);
305
306 gli_timestamp_to_time(timestamp, microsec, time);
307 }
308
309 void glk_date_to_time_local(glkdate_t *date, glktimeval_t *time)
310 {
311 time_t timestamp;
312 struct tm tm;
313 glsi32 microsec;
314
315 microsec = gli_date_to_tm(date, &tm);
316 tm.tm_isdst = -1;
317 timestamp = mktime(&tm);
318
319 gli_timestamp_to_time(timestamp, microsec, time);
320 }
321
322 glsi32 glk_date_to_simple_time_utc(glkdate_t *date, glui32 factor)
323 {
324 time_t timestamp;
325 struct tm tm;
326
327 if (factor == 0) {
328 gli_strict_warning("date_to_simple_time_utc: factor cannot be zero.");
329 return 0;
330 }
331
332 gli_date_to_tm(date, &tm);
333 /* The timegm function is not standard POSIX. If it's not available
334 on your platform, try setting the env var "TZ" to "", calling
335 mktime(), and then resetting "TZ". */
336 tm.tm_isdst = 0;
337 timestamp = timegm(&tm);
338
339 return gli_simplify_time(timestamp, factor);
340 }
341
342 glsi32 glk_date_to_simple_time_local(glkdate_t *date, glui32 factor)
343 {
344 time_t timestamp;
345 struct tm tm;
346
347 if (factor == 0) {
348 gli_strict_warning("date_to_simple_time_local: factor cannot be zero.");
349 return 0;
350 }
351
352 gli_date_to_tm(date, &tm);
353 tm.tm_isdst = -1;
354 timestamp = mktime(&tm);
355
356 return gli_simplify_time(timestamp, factor);
357 }
358
359 #ifdef NO_TIMEGM_AVAIL
360 /* If you have no timegm() function, you can #define NO_TIMEGM_AVAIL to
361 get this definition. */
362
363 time_t timegm(struct tm *tm)
364 {
365 time_t res;
366 char *origtz;
367
368 origtz = getenv("TZ");
369 setenv("TZ", "", 1);
370 tzset();
371 res = mktime(tm);
372 if (origtz)
373 setenv("TZ", origtz, 1);
374 else
375 unsetenv("TZ");
376 tzset();
377
378 return res;
379 }
380
381 #endif /* NO_TIMEGM_AVAIL */
382
383
384 #ifdef _WIN32
385 /* Windows needs wrappers for time functions to handle pre-epoch dates */
386
387 /* 31,557,600 seconds per year */
388 #define OFFSET84 ((glui32) 0x9E009580) /* 1902-1951 => 1986-2035, +84 years */
389 #define OFFSET28 ((glui32) 0x34AADC80) /* 1952-1969 => 1980-1997, +28 years */
390
391 time_t gli_mktime (struct tm * timeptr)
392 {
393 glui32 offset = 0;
394 glui32 adjust = 0;
395
396 if (timeptr->tm_year < 70 && timeptr->tm_year > 1)
397 {
398 if (timeptr->tm_year < 52)
399 {
400 offset = OFFSET84;
401 adjust = 84;
402 }
403 else
404 {
405 offset = OFFSET28;
406 adjust = 28;
407 }
408 }
409 timeptr->tm_year += adjust;
410 time_t ret = (mktime)(timeptr) - offset;
411 timeptr->tm_year -= adjust;
412
413 return ret;
414 }
415
416 #ifndef GARGLK
417 time_t timegm(struct tm *tm)
418 {
419 time_t answer;
420 putenv("TZ=UTC");
421 tzset();
422 answer=mktime(tm);
423 putenv("TZ=");
424 tzset();
425 return answer;
426 }
427 #endif
428
429 #define UTC_1901 (-2145916801) /* Dec 31, 1901 at 23:59:59 UTC */
430 #define UTC_1951 (-568080001) /* Dec 31, 1951 at 23:59:59 UTC */
431
432 static struct tm * gmtime_r(const time_t *timer, struct tm *result)
433 {
434 time_t eval = *timer;
435 glui32 adjust = 0;
436
437 if (eval < 0 && eval > UTC_1901)
438 {
439 if (eval > UTC_1951)
440 {
441 eval += OFFSET28;
442 adjust = 28;
443 }
444 else
445 {
446 eval += OFFSET84;
447 adjust = 84;
448 }
449 }
450
451 struct tm *gm = gmtime(&eval);
452
453 if (!gm)
454 {
455 time_t zero = 0;
456 gm = gmtime(&zero);
457 adjust = 0;
458 }
459
460 *result = *gm;
461 result->tm_year -= adjust;
462
463 return result;
464 }
465
466 static struct tm * localtime_r(const time_t *timer, struct tm *result)
467 {
468 time_t eval = *timer;
469 glui32 adjust = 0;
470
471 if (eval < 0 && eval > UTC_1901)
472 {
473 if (eval > UTC_1951)
474 {
475 eval += OFFSET28;
476 adjust = 28;
477 }
478 else
479 {
480 eval += OFFSET84;
481 adjust = 84;
482 }
483 }
484
485 struct tm *loc = localtime(&eval);
486
487 if (!loc)
488 {
489 time_t zero = 0;
490 loc = localtime(&zero);
491 adjust = 0;
492 }
493
494 *result = *loc;
495 result->tm_year -= adjust;
496
497 return result;
498 }
499 #endif /* WIN32 */
500
501 #endif /* GLK_MODULE_DATETIME */