Botan 3.0.0-alpha0
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#include <botan/exceptn.h>
11#include <ctime>
12#include <sstream>
13#include <iomanip>
14
15namespace Botan {
16
17namespace {
18
19std::tm do_gmtime(std::time_t time_val)
20 {
21 std::tm tm;
22
23#if defined(BOTAN_TARGET_OS_HAS_WIN32)
24 ::gmtime_s(&tm, &time_val); // Windows
25#elif defined(BOTAN_TARGET_OS_HAS_POSIX1)
26 ::gmtime_r(&time_val, &tm); // Unix/SUSv2
27#else
28 std::tm* tm_p = std::gmtime(&time_val);
29 if (tm_p == nullptr)
30 throw Encoding_Error("time_t_to_tm could not convert");
31 tm = *tm_p;
32#endif
33
34 return tm;
35 }
36
37/*
38Portable replacement for timegm, _mkgmtime, etc
39
40Algorithm due to Howard Hinnant
41
42See https://howardhinnant.github.io/date_algorithms.html#days_from_civil
43for details and explaination. The code is slightly simplified by our assumption
44that the date is at least 1970, which is sufficient for our purposes.
45*/
46size_t days_since_epoch(uint32_t year, uint32_t month, uint32_t day)
47 {
48 if(month <= 2)
49 year -= 1;
50 const uint32_t era = year / 400;
51 const uint32_t yoe = year - era * 400; // [0, 399]
52 const uint32_t doy = (153*(month + (month > 2 ? -3 : 9)) + 2)/5 + day-1; // [0, 365]
53 const uint32_t doe = yoe * 365 + yoe/4 - yoe/100 + doy; // [0, 146096]
54 return era * 146097 + doe - 719468;
55 }
56
57}
58
59std::chrono::system_clock::time_point calendar_point::to_std_timepoint() const
60 {
61 if(year() < 1970)
62 throw Invalid_Argument("calendar_point::to_std_timepoint() does not support years before 1970");
63
64 // 32 bit time_t ends at January 19, 2038
65 // https://msdn.microsoft.com/en-us/library/2093ets1.aspx
66 // Throw after 2037 if 32 bit time_t is used
67
68 if constexpr(sizeof(std::time_t) == 4)
69 {
70 if(year() > 2037)
71 {
72 throw Invalid_Argument("calendar_point::to_std_timepoint() does not support years after 2037 on this system");
73 }
74 }
75
76 // This upper bound is completely arbitrary
77 if(year() >= 2400)
78 {
79 throw Invalid_Argument("calendar_point::to_std_timepoint() does not support years after 2400");
80 }
81
82 const uint64_t seconds_64 = (days_since_epoch(year(), month(), day()) * 86400) +
83 (hour() * 60 * 60) + (minutes() * 60) + seconds();
84
85 const time_t seconds_time_t = static_cast<time_t>(seconds_64);
86
87 if(seconds_64 - seconds_time_t != 0)
88 {
89 throw Invalid_Argument("calendar_point::to_std_timepoint time_t overflow");
90 }
91
92 return std::chrono::system_clock::from_time_t(seconds_time_t);
93 }
94
95std::string calendar_point::to_string() const
96 {
97 // desired format: <YYYY>-<MM>-<dd>T<HH>:<mm>:<ss>
98 std::stringstream output;
99 output << std::setfill('0')
100 << std::setw(4) << year() << "-"
101 << std::setw(2) << month() << "-"
102 << std::setw(2) << day() << "T"
103 << std::setw(2) << hour() << ":"
104 << std::setw(2) << minutes() << ":"
105 << std::setw(2) << seconds();
106 return output.str();
107 }
108
109calendar_point::calendar_point(const std::chrono::system_clock::time_point& time_point)
110 {
111 std::tm tm = do_gmtime(std::chrono::system_clock::to_time_t(time_point));
112
113 m_year = tm.tm_year + 1900;
114 m_month = tm.tm_mon + 1;
115 m_day = tm.tm_mday;
116 m_hour = tm.tm_hour;
117 m_minutes = tm.tm_min;
118 m_seconds = tm.tm_sec;
119 }
120
121}
uint32_t hour() const
Definition: calendar.h:35
uint32_t seconds() const
Definition: calendar.h:43
uint32_t day() const
Definition: calendar.h:32
std::string to_string() const
Definition: calendar.cpp:95
std::chrono::system_clock::time_point to_std_timepoint() const
Definition: calendar.cpp:59
calendar_point(uint32_t y, uint32_t mon, uint32_t d, uint32_t h, uint32_t min, uint32_t sec)
Definition: calendar.h:54
uint32_t minutes() const
Definition: calendar.h:38
uint32_t month() const
Definition: calendar.h:29
uint32_t year() const
Definition: calendar.h:26
Definition: alg_id.cpp:13