Coverage Report

Created: 2025-08-29 06:40

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