1 | //======================================================================== |
2 | // |
3 | // glibc.h |
4 | // |
5 | // Emulate various non-portable glibc functions. |
6 | // |
7 | // This file is licensed under the GPLv2 or later |
8 | // |
9 | // Copyright (C) 2016 Adrian Johnson <ajohnson@redneon.com> |
10 | // Copyright (C) 2022 Albert Astals Cid <aacid@kde.org> |
11 | // |
12 | //======================================================================== |
13 | |
14 | #include "glibc.h" |
15 | |
16 | #ifndef HAVE_GMTIME_R |
17 | struct tm *gmtime_r(const time_t *timep, struct tm *result) |
18 | { |
19 | struct tm *gt; |
20 | gt = gmtime(timep); |
21 | if (gt) |
22 | *result = *gt; |
23 | return gt; |
24 | } |
25 | #endif |
26 | |
27 | #ifndef HAVE_LOCALTIME_R |
28 | struct tm *localtime_r(const time_t *timep, struct tm *result) |
29 | { |
30 | struct tm *lt; |
31 | lt = localtime(timep); |
32 | *result = *lt; |
33 | return lt; |
34 | } |
35 | #endif |
36 | |
37 | #ifndef HAVE_TIMEGM |
38 | // Get offset of local time from UTC in seconds. DST is ignored. |
39 | static time_t getLocalTimeZoneOffset() |
40 | { |
41 | time_t utc, local; |
42 | struct tm tm_utc; |
43 | time(&utc); |
44 | gmtime_r(&utc, &tm_utc); |
45 | local = mktime(&tm_utc); |
46 | return static_cast<time_t>(difftime(utc, local)); |
47 | } |
48 | |
49 | time_t timegm(struct tm *tm) |
50 | { |
51 | tm->tm_isdst = 0; |
52 | time_t t = mktime(tm); |
53 | if (t == -1) |
54 | return t; |
55 | |
56 | t += getLocalTimeZoneOffset(); |
57 | return t; |
58 | } |
59 | #endif |
60 | |