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