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