Coverage Report

Created: 2020-06-30 13:58

/src/botan/src/lib/utils/calendar.cpp
Line
Count
Source (jump to first uncovered line)
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
0
   {
22
0
   std::tm tm;
23
0
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
0
   return tm;
36
0
   }
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
0
   {
49
0
   if(month <= 2)
50
0
      year -= 1;
51
0
   const uint32_t era = year / 400;
52
0
   const uint32_t yoe = year - era * 400;      // [0, 399]
53
0
   const uint32_t doy = (153*(month + (month > 2 ? -3 : 9)) + 2)/5 + day-1;  // [0, 365]
54
0
   const uint32_t doe = yoe * 365 + yoe/4 - yoe/100 + doy;         // [0, 146096]
55
0
   return era * 146097 + doe - 719468;
56
0
   }
57
58
}
59
60
std::chrono::system_clock::time_point calendar_point::to_std_timepoint() const
61
0
   {
62
0
   if(get_year() < 1970)
63
0
      throw Invalid_Argument("calendar_point::to_std_timepoint() does not support years before 1970");
64
0
65
0
   // 32 bit time_t ends at January 19, 2038
66
0
   // https://msdn.microsoft.com/en-us/library/2093ets1.aspx
67
0
   // Throw after 2037 if 32 bit time_t is used
68
0
69
0
   BOTAN_IF_CONSTEXPR(sizeof(std::time_t) == 4)
70
0
      {
71
0
      if(get_year() > 2037)
72
0
         {
73
0
         throw Invalid_Argument("calendar_point::to_std_timepoint() does not support years after 2037 on this system");
74
0
         }
75
0
      }
76
0
77
0
   // This upper bound is completely arbitrary
78
0
   if(get_year() >= 2400)
79
0
      {
80
0
      throw Invalid_Argument("calendar_point::to_std_timepoint() does not support years after 2400");
81
0
      }
82
0
83
0
   const uint64_t seconds_64 = (days_since_epoch(get_year(), get_month(), get_day()) * 86400) +
84
0
                                (get_hour() * 60 * 60) + (get_minutes() * 60) + get_seconds();
85
0
86
0
   const time_t seconds_time_t = static_cast<time_t>(seconds_64);
87
0
88
0
   if(seconds_64 - seconds_time_t != 0)
89
0
      {
90
0
      throw Invalid_Argument("calendar_point::to_std_timepoint time_t overflow");
91
0
      }
92
0
93
0
   return std::chrono::system_clock::from_time_t(seconds_time_t);
94
0
   }
95
96
std::string calendar_point::to_string() const
97
0
   {
98
0
   // desired format: <YYYY>-<MM>-<dd>T<HH>:<mm>:<ss>
99
0
   std::stringstream output;
100
0
   output << std::setfill('0')
101
0
          << std::setw(4) << get_year() << "-"
102
0
          << std::setw(2) << get_month() << "-"
103
0
          << std::setw(2) << get_day() << "T"
104
0
          << std::setw(2) << get_hour() << ":"
105
0
          << std::setw(2) << get_minutes() << ":"
106
0
          << std::setw(2) << get_seconds();
107
0
   return output.str();
108
0
   }
109
110
111
calendar_point calendar_value(
112
   const std::chrono::system_clock::time_point& time_point)
113
0
   {
114
0
   std::tm tm = do_gmtime(std::chrono::system_clock::to_time_t(time_point));
115
0
116
0
   return calendar_point(tm.tm_year + 1900,
117
0
                         tm.tm_mon + 1,
118
0
                         tm.tm_mday,
119
0
                         tm.tm_hour,
120
0
                         tm.tm_min,
121
0
                         tm.tm_sec);
122
0
   }
123
124
}