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

randombit / botan / 5079590438

25 May 2023 12:28PM UTC coverage: 92.228% (+0.5%) from 91.723%
5079590438

Pull #3502

github

Pull Request #3502: Apply clang-format to the codebase

75589 of 81959 relevant lines covered (92.23%)

12139530.51 hits per line

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

97.44
/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/exceptn.h>
12
#include <ctime>
13
#include <iomanip>
14
#include <sstream>
15

16
namespace Botan {
17

18
namespace {
19

20
std::tm do_gmtime(std::time_t time_val) {
3,981✔
21
   std::tm tm;
3,981✔
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
3,981✔
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;
3,981✔
35
}
36

37
/*
38
Portable replacement for timegm, _mkgmtime, etc
39

40
Algorithm due to Howard Hinnant
41

42
See https://howardhinnant.github.io/date_algorithms.html#days_from_civil
43
for details and explaination. The code is slightly simplified by our assumption
44
that the date is at least 1970, which is sufficient for our purposes.
45
*/
46
size_t days_since_epoch(uint32_t year, uint32_t month, uint32_t day) {
484✔
47
   if(month <= 2)
484✔
48
      year -= 1;
373✔
49
   const uint32_t era = year / 400;
484✔
50
   const uint32_t yoe = year - era * 400;                                          // [0, 399]
484✔
51
   const uint32_t doy = (153 * (month + (month > 2 ? -3 : 9)) + 2) / 5 + day - 1;  // [0, 365]
484✔
52
   const uint32_t doe = yoe * 365 + yoe / 4 - yoe / 100 + doy;                     // [0, 146096]
484✔
53
   return era * 146097 + doe - 719468;
484✔
54
}
55

56
}
57

58
std::chrono::system_clock::time_point calendar_point::to_std_timepoint() const {
487✔
59
   if(year() < 1970)
487✔
60
      throw Invalid_Argument("calendar_point::to_std_timepoint() does not support years before 1970");
3✔
61

62
   // 32 bit time_t ends at January 19, 2038
63
   // https://msdn.microsoft.com/en-us/library/2093ets1.aspx
64
   // Throw after 2037 if 32 bit time_t is used
65

66
   if constexpr(sizeof(std::time_t) == 4) {
484✔
67
      if(year() > 2037) {
68
         throw Invalid_Argument("calendar_point::to_std_timepoint() does not support years after 2037 on this system");
69
      }
70
   }
71

72
   // This upper bound is completely arbitrary
73
   if(year() >= 2400) {
484✔
74
      throw Invalid_Argument("calendar_point::to_std_timepoint() does not support years after 2400");
×
75
   }
76

77
   const uint64_t seconds_64 =
484✔
78
      (days_since_epoch(year(), month(), day()) * 86400) + (hour() * 60 * 60) + (minutes() * 60) + seconds();
484✔
79

80
   const time_t seconds_time_t = static_cast<time_t>(seconds_64);
484✔
81

82
   if(seconds_64 - seconds_time_t != 0) {
484✔
83
      throw Invalid_Argument("calendar_point::to_std_timepoint time_t overflow");
84
   }
85

86
   return std::chrono::system_clock::from_time_t(seconds_time_t);
484✔
87
}
88

89
std::string calendar_point::to_string() const {
10✔
90
   // desired format: <YYYY>-<MM>-<dd>T<HH>:<mm>:<ss>
91
   std::stringstream output;
10✔
92
   output << std::setfill('0') << std::setw(4) << year() << "-" << std::setw(2) << month() << "-" << std::setw(2)
10✔
93
          << day() << "T" << std::setw(2) << hour() << ":" << std::setw(2) << minutes() << ":" << std::setw(2)
10✔
94
          << seconds();
10✔
95
   return output.str();
20✔
96
}
10✔
97

98
calendar_point::calendar_point(const std::chrono::system_clock::time_point& time_point) {
3,981✔
99
   std::tm tm = do_gmtime(std::chrono::system_clock::to_time_t(time_point));
3,981✔
100

101
   m_year = tm.tm_year + 1900;
3,981✔
102
   m_month = tm.tm_mon + 1;
3,981✔
103
   m_day = tm.tm_mday;
3,981✔
104
   m_hour = tm.tm_hour;
3,981✔
105
   m_minutes = tm.tm_min;
3,981✔
106
   m_seconds = tm.tm_sec;
3,981✔
107
}
3,981✔
108

109
}
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

© 2025 Coveralls, Inc