Botan 3.0.0-alpha0
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 54 of file calendar.h.

54 :
55 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 109 of file calendar.cpp.

110 {
111 std::tm tm = do_gmtime(std::chrono::system_clock::to_time_t(time_point));
112
113 m_year = tm.tm_year + 1900;
114 m_month = tm.tm_mon + 1;
115 m_day = tm.tm_mday;
116 m_hour = tm.tm_hour;
117 m_minutes = tm.tm_min;
118 m_seconds = tm.tm_sec;
119 }

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 32 of file calendar.h.

32{ 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 35 of file calendar.h.

35{ 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 38 of file calendar.h.

38{ 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 29 of file calendar.h.

29{ 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 43 of file calendar.h.

43{ 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.

60 {
61 if(year() < 1970)
62 throw Invalid_Argument("calendar_point::to_std_timepoint() does not support years before 1970");
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 {
70 if(year() > 2037)
71 {
72 throw Invalid_Argument("calendar_point::to_std_timepoint() does not support years after 2037 on this system");
73 }
74 }
75
76 // This upper bound is completely arbitrary
77 if(year() >= 2400)
78 {
79 throw Invalid_Argument("calendar_point::to_std_timepoint() does not support years after 2400");
80 }
81
82 const uint64_t seconds_64 = (days_since_epoch(year(), month(), day()) * 86400) +
83 (hour() * 60 * 60) + (minutes() * 60) + seconds();
84
85 const time_t seconds_time_t = static_cast<time_t>(seconds_64);
86
87 if(seconds_64 - seconds_time_t != 0)
88 {
89 throw Invalid_Argument("calendar_point::to_std_timepoint time_t overflow");
90 }
91
92 return std::chrono::system_clock::from_time_t(seconds_time_t);
93 }
uint32_t hour() const
Definition: calendar.h:35
uint32_t seconds() const
Definition: calendar.h:43
uint32_t day() const
Definition: calendar.h:32
uint32_t minutes() const
Definition: calendar.h:38
uint32_t month() const
Definition: calendar.h:29
uint32_t year() const
Definition: calendar.h:26

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 95 of file calendar.cpp.

96 {
97 // desired format: <YYYY>-<MM>-<dd>T<HH>:<mm>:<ss>
98 std::stringstream output;
99 output << std::setfill('0')
100 << std::setw(4) << year() << "-"
101 << std::setw(2) << month() << "-"
102 << std::setw(2) << day() << "T"
103 << std::setw(2) << hour() << ":"
104 << std::setw(2) << minutes() << ":"
105 << std::setw(2) << seconds();
106 return output.str();
107 }

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

◆ year()

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

The year

Definition at line 26 of file calendar.h.

26{ 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: