• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

randombit / botan / 15654190339

14 Jun 2025 04:56PM UTC coverage: 90.571% (-0.01%) from 90.585%
15654190339

push

github

web-flow
Merge pull request #4912 from randombit/jack/clang-tidy-headers-part-2

Further clang-tidy fixes in header files

98789 of 109074 relevant lines covered (90.57%)

12363815.23 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.3
/src/lib/utils/calendar.cpp
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
std::tm do_gmtime(std::time_t time_val) {
12,459✔
24
   std::tm tm;
12,459✔
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
   if(!::gmtime_r(&time_val, &tm)) {
12,459✔
30
      throw Encoding_Error("do_gmtime could not convert");
×
31
   }
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
   return tm;
12,459✔
41
}
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 explaination. The code is slightly simplified by our assumption
50
that the date is at least 1970, which is sufficient for our purposes.
51
*/
52
uint64_t days_since_epoch(uint32_t year, uint32_t month, uint32_t day) {
3,186✔
53
   BOTAN_ARG_CHECK(year >= 1970, "Years before 1970 not supported");
3,186✔
54

55
   if(month <= 2) {
3,183✔
56
      year -= 1;
3,067✔
57
   }
58
   const uint32_t era = year / 400;
3,183✔
59
   const uint32_t yoe = year - era * 400;                                          // [0, 399]
3,183✔
60
   const uint32_t doy = (153 * (month + (month > 2 ? -3 : 9)) + 2) / 5 + day - 1;  // [0, 365]
3,183✔
61
   const uint32_t doe = yoe * 365 + yoe / 4 - yoe / 100 + doy;                     // [0, 146096]
3,183✔
62
   return era * 146097 + doe - 719468;
3,183✔
63
}
64

65
}  // namespace
66

67
uint64_t calendar_point::seconds_since_epoch() const {
3,186✔
68
   return (days_since_epoch(year(), month(), day()) * 86400) + (hour() * 60 * 60) + (minutes() * 60) + seconds();
3,186✔
69
}
70

71
std::chrono::system_clock::time_point calendar_point::to_std_timepoint() const {
3,182✔
72
   const uint64_t seconds_64 = this->seconds_since_epoch();
3,182✔
73
   const time_t seconds_time_t = static_cast<time_t>(seconds_64);
3,179✔
74

75
   if(seconds_64 - seconds_time_t != 0) {
3,179✔
76
      throw Invalid_Argument("calendar_point::to_std_timepoint time_t overflow");
77
   }
78

79
   return std::chrono::system_clock::from_time_t(seconds_time_t);
3,179✔
80
}
81

82
std::string calendar_point::to_string() const {
10✔
83
   // desired format: <YYYY>-<MM>-<dd>T<HH>:<mm>:<ss>
84
   std::stringstream output;
10✔
85
   output << std::setfill('0') << std::setw(4) << year() << "-" << std::setw(2) << month() << "-" << std::setw(2)
10✔
86
          << day() << "T" << std::setw(2) << hour() << ":" << std::setw(2) << minutes() << ":" << std::setw(2)
10✔
87
          << seconds();
10✔
88
   return output.str();
20✔
89
}
10✔
90

91
calendar_point::calendar_point(const std::chrono::system_clock::time_point& time_point) {
12,459✔
92
   std::tm tm = do_gmtime(std::chrono::system_clock::to_time_t(time_point));
12,459✔
93

94
   m_year = tm.tm_year + 1900;
12,459✔
95
   m_month = tm.tm_mon + 1;
12,459✔
96
   m_day = tm.tm_mday;
12,459✔
97
   m_hour = tm.tm_hour;
12,459✔
98
   m_minutes = tm.tm_min;
12,459✔
99
   m_seconds = tm.tm_sec;
12,459✔
100
}
12,459✔
101

102
}  // namespace Botan
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc