-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathah-date.H
More file actions
156 lines (124 loc) · 5.13 KB
/
ah-date.H
File metadata and controls
156 lines (124 loc) · 5.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
Aleph_w
Data structures & Algorithms
version 2.0.0b
https://github.com/lrleon/Aleph-w
This file is part of Aleph-w library
Copyright (c) 2002-2026 Leandro Rabindranath Leon
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
# ifndef AH_DATE_H
# define AH_DATE_H
/** @file ah-date.H
* @brief Lightweight helpers for validating and formatting civil dates.
*
* These functions wrap POSIX @c strptime/@c mktime functionality with
* Aleph-w error handling in order to surface invalid inputs early and to
* provide a consistent interface for common conversions involving @c time_t.
*
* @ingroup Utilities
* @author Leandro Rabindranath León
*/
# include <array>
# include <ctime>
# include <limits>
# include <string>
# include <ah-errors.H>
namespace Aleph
{
namespace detail
{
inline constexpr size_t tm_epoch_year = 1900u;
inline constexpr size_t max_supported_year =
static_cast<size_t>(std::numeric_limits<int>::max()) + tm_epoch_year;
inline constexpr time_t seconds_per_day =
static_cast<time_t>(24 * 60 * 60);
} // namespace detail
/** @brief Return true if the provided year is a leap year. */
[[nodiscard]] inline bool is_leap_year(const size_t yy) noexcept
{
return (yy % 4 == 0 and yy % 100 != 0) or (yy % 400 == 0);
}
/** @brief Check whether the month number is within the [1, 12] range. */
[[nodiscard]] inline bool valid_month(const size_t mm) noexcept
{
return mm >= 1 and mm <= 12;
}
/** @brief Check whether a given day exists for the supplied month/year. */
[[nodiscard]] inline bool valid_day(const size_t yy, const size_t mm, const size_t dd) noexcept
{
if (not valid_month(mm) or dd == 0)
return false;
static constexpr std::array<size_t, 12> month_lengths =
{31u, 28u, 31u, 30u, 31u, 30u, 31u, 31u, 30u, 31u, 30u, 31u};
const size_t base = month_lengths[mm - 1];
const size_t extra = (mm == 2 and is_leap_year(yy)) ? 1u : 0u;
return dd <= base + extra;
}
/** @brief Convert a (day, month, year) triple to a POSIX @c time_t value. */
inline time_t to_time_t(const size_t dd, const size_t mm, const size_t yy)
{
ah_domain_error_if(yy < detail::tm_epoch_year or
yy > detail::max_supported_year)
<< "Year " << yy << " is outside the supported range ["
<< detail::tm_epoch_year << ", " << detail::max_supported_year << ']';
ah_domain_error_if(not valid_day(yy, mm, dd))
<< "Invalid date components"
<< " (dd=" << dd << ", mm=" << mm << ", yy=" << yy << ')';
std::tm tm{};
tm.tm_mday = static_cast<int>(dd);
tm.tm_mon = static_cast<int>(mm) - 1;
tm.tm_year = static_cast<int>(yy) - 1900;
tm.tm_isdst = -1;
return std::mktime(&tm);
}
/** @brief Convert a formatted string to @c time_t using the provided format. */
inline time_t to_time_t(const std::string & s, const std::string & format)
{
std::tm tm{};
const char *res = ::strptime(s.c_str(), format.c_str(), &tm);
ah_domain_error_if(res == nullptr or *res != '\0')
<< "Input string '" << s << "' does not match format '" << format << '\'';
tm.tm_isdst = -1;
return std::mktime(&tm);
}
/** @brief Convenience overload for ISO strings (YYYY-MM-DD HH:MM:SS). */
inline time_t to_time_t(const std::string & s)
{
return to_time_t(s, "%Y-%m-%d %H:%M:%S");
}
/** @brief Return the number of whole days represented by @p t. */
inline size_t to_days(const time_t t)
{
ah_domain_error_if(t < 0)
<< "to_days() expects non-negative timestamps";
return static_cast<size_t>(t / detail::seconds_per_day);
}
/** @brief Format a @c time_t value into a string using @p format. */
inline std::string to_string(const time_t t, const std::string & format)
{
const std::tm *tm_ptr = std::localtime(&t);
ah_runtime_error_if(tm_ptr == nullptr) << "localtime() failed";
std::tm tm = *tm_ptr;
char buff[128]{};
const size_t written = std::strftime(buff, sizeof(buff), format.c_str(), &tm);
ah_range_error_if(written == 0)
<< "Unable to format time using pattern '" << format << "'";
return std::string(buff, written);
}
}
# endif