Botan 3.8.1
Crypto and TLS for C&
calendar.cpp
Go to the documentation of this file.
1/*
2* Calendar Functions
3* (C) 1999-2010,2017 Jack Lloyd
4* (C) 2015 Simon Warta (Kullo GmbH)
5*
6* Botan is released under the Simplified BSD License (see license.txt)
7*/
8
9#include <botan/internal/calendar.h>
10
11#include <botan/assert.h>
12#include <botan/exceptn.h>
13#include <botan/internal/target_info.h>
14#include <ctime>
15#include <iomanip>
16#include <sstream>
17
18namespace Botan {
19
20namespace {
21
22std::tm do_gmtime(std::time_t time_val) {
23 std::tm tm;
24
25#if defined(BOTAN_TARGET_OS_HAS_WIN32)
26 ::gmtime_s(&tm, &time_val); // Windows
27#elif defined(BOTAN_TARGET_OS_HAS_POSIX1)
28 ::gmtime_r(&time_val, &tm); // Unix/SUSv2
29#else
30 std::tm* tm_p = std::gmtime(&time_val);
31 if(tm_p == nullptr)
32 throw Encoding_Error("time_t_to_tm could not convert");
33 tm = *tm_p;
34#endif
35
36 return tm;
37}
38
39/*
40Portable replacement for timegm, _mkgmtime, etc
41
42Algorithm due to Howard Hinnant
43
44See https://howardhinnant.github.io/date_algorithms.html#days_from_civil
45for details and explaination. The code is slightly simplified by our assumption
46that the date is at least 1970, which is sufficient for our purposes.
47*/
48uint64_t days_since_epoch(uint32_t year, uint32_t month, uint32_t day) {
49 BOTAN_ARG_CHECK(year >= 1970, "Years before 1970 not supported");
50
51 if(month <= 2) {
52 year -= 1;
53 }
54 const uint32_t era = year / 400;
55 const uint32_t yoe = year - era * 400; // [0, 399]
56 const uint32_t doy = (153 * (month + (month > 2 ? -3 : 9)) + 2) / 5 + day - 1; // [0, 365]
57 const uint32_t doe = yoe * 365 + yoe / 4 - yoe / 100 + doy; // [0, 146096]
58 return era * 146097 + doe - 719468;
59}
60
61} // namespace
62
64 return (days_since_epoch(year(), month(), day()) * 86400) + (hour() * 60 * 60) + (minutes() * 60) + seconds();
65}
66
67std::chrono::system_clock::time_point calendar_point::to_std_timepoint() const {
68 const uint64_t seconds_64 = this->seconds_since_epoch();
69 const time_t seconds_time_t = static_cast<time_t>(seconds_64);
70
71 if(seconds_64 - seconds_time_t != 0) {
72 throw Invalid_Argument("calendar_point::to_std_timepoint time_t overflow");
73 }
74
75 return std::chrono::system_clock::from_time_t(seconds_time_t);
76}
77
78std::string calendar_point::to_string() const {
79 // desired format: <YYYY>-<MM>-<dd>T<HH>:<mm>:<ss>
80 std::stringstream output;
81 output << std::setfill('0') << std::setw(4) << year() << "-" << std::setw(2) << month() << "-" << std::setw(2)
82 << day() << "T" << std::setw(2) << hour() << ":" << std::setw(2) << minutes() << ":" << std::setw(2)
83 << seconds();
84 return output.str();
85}
86
87calendar_point::calendar_point(const std::chrono::system_clock::time_point& time_point) {
88 std::tm tm = do_gmtime(std::chrono::system_clock::to_time_t(time_point));
89
90 m_year = tm.tm_year + 1900;
91 m_month = tm.tm_mon + 1;
92 m_day = tm.tm_mday;
93 m_hour = tm.tm_hour;
94 m_minutes = tm.tm_min;
95 m_seconds = tm.tm_sec;
96}
97
98} // namespace Botan
#define BOTAN_ARG_CHECK(expr, msg)
Definition assert.h:31
uint32_t hour() const
Definition calendar.h:33
uint32_t seconds() const
Definition calendar.h:41
uint32_t day() const
Definition calendar.h:30
uint64_t seconds_since_epoch() const
Definition calendar.cpp:63
std::string to_string() const
Definition calendar.cpp:78
std::chrono::system_clock::time_point to_std_timepoint() const
Definition calendar.cpp:67
calendar_point(uint32_t y, uint32_t mon, uint32_t d, uint32_t h, uint32_t min, uint32_t sec)
Definition calendar.h:52
uint32_t minutes() const
Definition calendar.h:36
uint32_t month() const
Definition calendar.h:27
uint32_t year() const
Definition calendar.h:24