/src/rnp/src/common/time-utils.cpp
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2021 [Ribose Inc](https://www.ribose.com). |
3 | | * All rights reserved. |
4 | | * |
5 | | * Redistribution and use in source and binary forms, with or without modification, |
6 | | * are permitted provided that the following conditions are met: |
7 | | * |
8 | | * 1. Redistributions of source code must retain the above copyright notice, |
9 | | * this list of conditions and the following disclaimer. |
10 | | * |
11 | | * 2. Redistributions in binary form must reproduce the above copyright notice, |
12 | | * this list of conditions and the following disclaimer in the documentation |
13 | | * and/or other materials provided with the distribution. |
14 | | * |
15 | | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
16 | | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
17 | | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
18 | | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE |
19 | | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
20 | | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
21 | | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
22 | | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
23 | | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
24 | | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
25 | | */ |
26 | | /** Time utilities |
27 | | * @file |
28 | | */ |
29 | | |
30 | | #include <stdint.h> |
31 | | #include "time-utils.h" |
32 | | |
33 | | static inline time_t |
34 | | adjust_time32(time_t t) |
35 | 21.5k | { |
36 | 21.5k | return (sizeof(t) == 4 && t < 0) ? INT32_MAX : t; |
37 | 21.5k | } |
38 | | |
39 | | bool |
40 | | rnp_y2k38_warning(time_t t) |
41 | 21.5k | { |
42 | 21.5k | return (sizeof(t) == 4 && (t < 0 || t == INT32_MAX)); |
43 | 21.5k | } |
44 | | |
45 | | time_t |
46 | | rnp_mktime(struct tm *tm) |
47 | 0 | { |
48 | 0 | return adjust_time32(mktime(tm)); |
49 | 0 | } |
50 | | |
51 | | void |
52 | | rnp_gmtime(time_t t, struct tm &tm) |
53 | 0 | { |
54 | 0 | time_t adjusted = adjust_time32(t); |
55 | 0 | #ifndef _WIN32 |
56 | 0 | gmtime_r(&adjusted, &tm); |
57 | | #else |
58 | | (void) gmtime_s(&tm, &adjusted); |
59 | | #endif |
60 | 0 | } |
61 | | |
62 | | void |
63 | | rnp_localtime(time_t t, struct tm &tm) |
64 | 0 | { |
65 | 0 | time_t adjusted = adjust_time32(t); |
66 | 0 | #ifndef _WIN32 |
67 | 0 | localtime_r(&adjusted, &tm); |
68 | | #else |
69 | | (void) localtime_s(&tm, &adjusted); |
70 | | #endif |
71 | 0 | } |
72 | | |
73 | | std::string |
74 | | rnp_ctime(time_t t) |
75 | 21.5k | { |
76 | 21.5k | char time_buf[26]; |
77 | 21.5k | time_t adjusted = adjust_time32(t); |
78 | 21.5k | #ifndef _WIN32 |
79 | 21.5k | (void) ctime_r(&adjusted, time_buf); |
80 | | #else |
81 | | (void) ctime_s(time_buf, sizeof(time_buf), &adjusted); |
82 | | #endif |
83 | 21.5k | return std::string(time_buf); |
84 | 21.5k | } |
85 | | |
86 | | time_t |
87 | | rnp_timeadd(time_t t1, time_t t2) |
88 | 0 | { |
89 | 0 | if (sizeof(time_t) == 4) { |
90 | 0 | if (t1 < 0 || t2 < 0) { |
91 | 0 | return INT32_MAX; |
92 | 0 | } |
93 | 0 | return adjust_time32(t1 + t2); |
94 | 0 | } |
95 | 0 | return t1 + t2; |
96 | 0 | } |