Branch data TLA Line data Source code
1 : : // Copyright (c) 2024-2025 xlnt-community
2 : : //
3 : : // Permission is hereby granted, free of charge, to any person obtaining a copy
4 : : // of this software and associated documentation files (the "Software"), to deal
5 : : // in the Software without restriction, including without limitation the rights
6 : : // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 : : // copies of the Software, and to permit persons to whom the Software is
8 : : // furnished to do so, subject to the following conditions:
9 : : //
10 : : // The above copyright notice and this permission notice shall be included in
11 : : // all copies or substantial portions of the Software.
12 : : //
13 : : // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 : : // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 : : // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 : : // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 : : // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 : : // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19 : : // THE SOFTWARE
20 : : //
21 : : // @license: http://www.opensource.org/licenses/mit-license.php
22 : : // @author: see AUTHORS file
23 : :
24 : : #pragma once
25 : :
26 : : #include <ctime>
27 : : #include <xlnt/utils/optional.hpp>
28 : : #include <xlnt/utils/environment.hpp>
29 : :
30 : : namespace xlnt {
31 : : namespace detail {
32 : :
33 : : /// Converts given time since epoch (a time_t value) into calendar time, expressed in local time, in the struct tm format.
34 : : /// If the conversion failed, an empty optional will be returned.
35 :CBC 3 : inline optional<std::tm> localtime_safe(std::time_t raw_time)
36 : : {
37 : 3 : optional<std::tm> returned_value;
38 : :
39 : : #ifdef _MSC_VER
40 : : std::tm result{};
41 : : errno_t err = localtime_s(&result, &raw_time);
42 : : if (err == 0)
43 : : {
44 : : returned_value = result;
45 : : }
46 : : #elif XLNT_HAS_C_VERSION(XLNT_C_23) || _POSIX_C_SOURCE >= 1 || _XOPEN_SOURCE || _BSD_SOURCE || _SVID_SOURCE || _POSIX_SOURCE
47 : 3 : std::tm result{};
48 [ + - ]: 3 : if (localtime_r(&raw_time, &result))
49 : : {
50 : 3 : returned_value = result;
51 : : }
52 : : #else
53 : : std::tm *tm = std::localtime(&raw_time);
54 : :
55 : : if (tm != nullptr)
56 : : {
57 : : returned_value = *tm;
58 : : }
59 : : #endif
60 : :
61 : 3 : return returned_value;
62 : : }
63 : :
64 : : /// Converts given time since epoch (a time_t value) into calendar time, expressed in Coordinated Universal Time (UTC) in the struct tm format.
65 : : /// If the conversion failed, an empty optional will be returned.
66 : : inline optional<std::tm> gmtime_safe(std::time_t raw_time)
67 : : {
68 : : optional<std::tm> returned_value;
69 : :
70 : : #ifdef _MSC_VER
71 : : std::tm result{};
72 : : errno_t err = gmtime_s(&result, &raw_time);
73 : : if (err == 0)
74 : : {
75 : : returned_value = result;
76 : : }
77 : : #elif XLNT_HAS_C_VERSION(XLNT_C_23) || _POSIX_C_SOURCE >= 1 || _XOPEN_SOURCE || _BSD_SOURCE || _SVID_SOURCE || _POSIX_SOURCE
78 : : std::tm result{};
79 : : if (gmtime_r(&raw_time, &result))
80 : : {
81 : : returned_value = result;
82 : : }
83 : : #else
84 : : std::tm *tm = std::gmtime(&raw_time);
85 : :
86 : : if (tm != nullptr)
87 : : {
88 : : returned_value = *tm;
89 : : }
90 : : #endif
91 : :
92 : : return returned_value;
93 : : }
94 : :
95 : : } // namespace detail
96 : : } // namespace xlnt
|