Botan 3.13.0
Crypto and TLS for C&
Botan::calendar_point Class Referencefinal

#include <calendar.h>

Public Member Functions

 calendar_point (const std::chrono::system_clock::time_point &time_point)
 calendar_point (uint32_t y, uint32_t mon, uint32_t d, uint32_t h, uint32_t min, uint32_t sec)
uint32_t day () const
uint32_t hour () const
uint32_t minutes () const
uint32_t month () const
uint32_t seconds () const
int64_t seconds_since_epoch () const
std::chrono::system_clock::time_point to_std_timepoint () const
uint32_t year () const

Detailed Description

Struct representing a particular date and time

Definition at line 20 of file calendar.h.

Constructor & Destructor Documentation

◆ calendar_point() [1/2]

Botan::calendar_point::calendar_point ( uint32_t y,
uint32_t mon,
uint32_t d,
uint32_t h,
uint32_t min,
uint32_t sec )

Initialize a calendar_point

Parameters
ythe year
monthe month
dthe day
hthe hour
minthe minute
secthe second

Definition at line 83 of file calendar.cpp.

83 :
84 m_year(static_cast<uint16_t>(y)),
85 m_month(static_cast<uint8_t>(mon)),
86 m_day(static_cast<uint8_t>(d)),
87 m_hour(static_cast<uint8_t>(h)),
88 m_minutes(static_cast<uint8_t>(min)),
89 m_seconds(static_cast<uint8_t>(sec)) {
90 BOTAN_ARG_CHECK(y <= 9999, "Year is outside representable range");
91 BOTAN_ARG_CHECK(mon >= 1 && mon <= 12, "Month is outside range");
92 BOTAN_ARG_CHECK(d >= 1 && d <= 31, "Day is outside range");
93 BOTAN_ARG_CHECK(h < 24, "Hour is outside range");
94 BOTAN_ARG_CHECK(min < 60, "Minute is outside range");
95 BOTAN_ARG_CHECK(sec < 60, "Seconds is outside range");
96}
#define BOTAN_ARG_CHECK(expr, msg)
Definition assert.h:33

References BOTAN_ARG_CHECK.

◆ calendar_point() [2/2]

Botan::calendar_point::calendar_point ( const std::chrono::system_clock::time_point & time_point)
explicit

Convert a time_point to a calendar_point

Parameters
time_pointa time point from the system clock

Definition at line 128 of file calendar.cpp.

128 {
129 const auto [year, month, day, hour, minute, second] = civil_from_time_point(time_point);
130
131 BOTAN_ARG_CHECK(year <= 9999, "Year is outside representable range");
132
133 m_year = static_cast<uint16_t>(year);
134 m_month = static_cast<uint8_t>(month);
135 m_day = static_cast<uint8_t>(day);
136 m_hour = static_cast<uint8_t>(hour);
137 m_minutes = static_cast<uint8_t>(minute);
138 m_seconds = static_cast<uint8_t>(second);
139}
uint32_t hour() const
Definition calendar.h:32
uint32_t day() const
Definition calendar.h:29
uint32_t month() const
Definition calendar.h:26
uint32_t year() const
Definition calendar.h:23

References BOTAN_ARG_CHECK, day(), hour(), month(), and year().

Member Function Documentation

◆ day()

uint32_t Botan::calendar_point::day ( ) const
inline

The day of the month, 1 through 31

Definition at line 29 of file calendar.h.

29{ return m_day; }

Referenced by calendar_point(), Botan::ASN1_Time::from_time_point(), and seconds_since_epoch().

◆ hour()

uint32_t Botan::calendar_point::hour ( ) const
inline

Hour in 24-hour form, 0 to 23

Definition at line 32 of file calendar.h.

32{ return m_hour; }

Referenced by calendar_point(), Botan::ASN1_Time::from_time_point(), and seconds_since_epoch().

◆ minutes()

uint32_t Botan::calendar_point::minutes ( ) const
inline

Minutes in the hour, 0 to 59

Definition at line 35 of file calendar.h.

35{ return m_minutes; }

Referenced by Botan::ASN1_Time::from_time_point(), and seconds_since_epoch().

◆ month()

uint32_t Botan::calendar_point::month ( ) const
inline

The month, 1 through 12 for Jan to Dec

Definition at line 26 of file calendar.h.

26{ return m_month; }

Referenced by calendar_point(), Botan::ASN1_Time::from_time_point(), and seconds_since_epoch().

◆ seconds()

uint32_t Botan::calendar_point::seconds ( ) const
inline

Seconds in the minute, 0 to 59

Definition at line 38 of file calendar.h.

38{ return m_seconds; }

Referenced by Botan::ASN1_Time::from_time_point(), and seconds_since_epoch().

◆ seconds_since_epoch()

int64_t Botan::calendar_point::seconds_since_epoch ( ) const

Return seconds since epoch

This is negative for dates before 1970

Definition at line 98 of file calendar.cpp.

98 {
99 return (days_since_epoch(year(), month(), day()) * 86400) + (hour() * 60 * 60) + (minutes() * 60) + seconds();
100}
uint32_t seconds() const
Definition calendar.h:38
uint32_t minutes() const
Definition calendar.h:35

References day(), hour(), minutes(), month(), seconds(), and year().

Referenced by Botan::ASN1_Time::time_since_epoch(), and to_std_timepoint().

◆ to_std_timepoint()

std::chrono::system_clock::time_point Botan::calendar_point::to_std_timepoint ( ) const

Returns an STL timepoint object

Note this throws an exception if the time is not representable in the system time_t

Definition at line 102 of file calendar.cpp.

102 {
103 const int64_t seconds_64 = this->seconds_since_epoch();
104
105 /*
106 * The tick of a system_clock varies by implementation, and so also the
107 * largest and smallest representable values vary. Ensure this date is within
108 * range of the clock implementation.
109 */
110 constexpr int64_t max_representable_seconds = static_cast<int64_t>(
111 std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::duration::max()).count());
112 constexpr int64_t min_representable_seconds = static_cast<int64_t>(
113 std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::duration::min()).count());
114
115 if(seconds_64 > max_representable_seconds || seconds_64 < min_representable_seconds) {
116 throw Invalid_Argument("calendar_point::to_std_timepoint time is outside the representable range");
117 }
118
119 const time_t seconds_time_t = static_cast<time_t>(seconds_64);
120
121 if(seconds_64 - seconds_time_t != 0) {
122 throw Invalid_Argument("calendar_point::to_std_timepoint time is outside the representable range");
123 }
124
125 return std::chrono::system_clock::from_time_t(seconds_time_t);
126}
int64_t seconds_since_epoch() const
Definition calendar.cpp:98

References seconds_since_epoch().

Referenced by Botan::ASN1_Time::to_std_timepoint().

◆ year()

uint32_t Botan::calendar_point::year ( ) const
inline

The year, less than or equal to 9999

Definition at line 23 of file calendar.h.

23{ return m_year; }

Referenced by calendar_point(), Botan::ASN1_Time::from_time_point(), and seconds_since_epoch().


The documentation for this class was generated from the following files: