Botan 3.3.0
Crypto and TLS for C&
Public Member Functions | List of all members
Botan::calendar_point Class Reference

#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
 
std::chrono::system_clock::time_point to_std_timepoint () const
 
std::string to_string () const
 
uint32_t year () const
 

Detailed Description

Struct representing a particular date and time

Definition at line 21 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 )
inline

Initialize a calendar_point

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

Definition at line 52 of file calendar.h.

52 :
53 m_year(y), m_month(mon), m_day(d), m_hour(h), m_minutes(min), m_seconds(sec) {}

◆ calendar_point() [2/2]

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

Convert a time_point to a calendar_point

Parameters
time_pointa time point from the system clock

Definition at line 100 of file calendar.cpp.

100 {
101 std::tm tm = do_gmtime(std::chrono::system_clock::to_time_t(time_point));
102
103 m_year = tm.tm_year + 1900;
104 m_month = tm.tm_mon + 1;
105 m_day = tm.tm_mday;
106 m_hour = tm.tm_hour;
107 m_minutes = tm.tm_min;
108 m_seconds = tm.tm_sec;
109}

Member Function Documentation

◆ day()

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

The day of the month, 1 through 31 (or 28 or 30 based on month

Definition at line 30 of file calendar.h.

30{ return m_day; }

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

◆ hour()

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

Hour in 24-hour form, 0 to 23

Definition at line 33 of file calendar.h.

33{ return m_hour; }

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

◆ minutes()

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

Minutes in the hour, 0 to 60

Definition at line 36 of file calendar.h.

36{ return m_minutes; }

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

◆ month()

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

The month, 1 through 12 for Jan to Dec

Definition at line 27 of file calendar.h.

27{ return m_month; }

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

◆ seconds()

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

Seconds in the minute, 0 to 60, but might be slightly larger to deal with leap seconds on some systems

Definition at line 41 of file calendar.h.

41{ return m_seconds; }

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

◆ to_std_timepoint()

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

Returns an STL timepoint object

Definition at line 59 of file calendar.cpp.

59 {
60 if(year() < 1970) {
61 throw Invalid_Argument("calendar_point::to_std_timepoint() does not support years before 1970");
62 }
63
64 // 32 bit time_t ends at January 19, 2038
65 // https://msdn.microsoft.com/en-us/library/2093ets1.aspx
66 // Throw after 2037 if 32 bit time_t is used
67
68 if constexpr(sizeof(std::time_t) == 4) {
69 if(year() > 2037) {
70 throw Invalid_Argument("calendar_point::to_std_timepoint() does not support years after 2037 on this system");
71 }
72 }
73
74 // This upper bound is completely arbitrary
75 if(year() >= 2400) {
76 throw Invalid_Argument("calendar_point::to_std_timepoint() does not support years after 2400");
77 }
78
79 const uint64_t seconds_64 =
80 (days_since_epoch(year(), month(), day()) * 86400) + (hour() * 60 * 60) + (minutes() * 60) + seconds();
81
82 const time_t seconds_time_t = static_cast<time_t>(seconds_64);
83
84 if(seconds_64 - seconds_time_t != 0) {
85 throw Invalid_Argument("calendar_point::to_std_timepoint time_t overflow");
86 }
87
88 return std::chrono::system_clock::from_time_t(seconds_time_t);
89}
uint32_t hour() const
Definition calendar.h:33
uint32_t seconds() const
Definition calendar.h:41
uint32_t day() const
Definition calendar.h:30
uint32_t minutes() const
Definition calendar.h:36
uint32_t month() const
Definition calendar.h:27
uint32_t year() const
Definition calendar.h:24

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

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

◆ to_string()

std::string Botan::calendar_point::to_string ( ) const

Returns a human readable string of the struct's components. Formatting might change over time. Currently it is RFC339 'iso-date-time'.

Definition at line 91 of file calendar.cpp.

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

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

◆ year()

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

The year

Definition at line 24 of file calendar.h.

24{ return m_year; }

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


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