Botan 3.7.1
Crypto and TLS for C&
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
 
uint64_t seconds_since_epoch () 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 86 of file calendar.cpp.

86 {
87 std::tm tm = do_gmtime(std::chrono::system_clock::to_time_t(time_point));
88
89 m_year = tm.tm_year + 1900;
90 m_month = tm.tm_mon + 1;
91 m_day = tm.tm_mday;
92 m_hour = tm.tm_hour;
93 m_minutes = tm.tm_min;
94 m_seconds = tm.tm_sec;
95}

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(), seconds_since_epoch(), 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(), seconds_since_epoch(), 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(), seconds_since_epoch(), 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(), seconds_since_epoch(), 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(), seconds_since_epoch(), and to_string().

◆ seconds_since_epoch()

uint64_t Botan::calendar_point::seconds_since_epoch ( ) const

Return seconds since epoch

Definition at line 62 of file calendar.cpp.

62 {
63 return (days_since_epoch(year(), month(), day()) * 86400) + (hour() * 60 * 60) + (minutes() * 60) + seconds();
64}
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::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 66 of file calendar.cpp.

66 {
67 const uint64_t seconds_64 = this->seconds_since_epoch();
68 const time_t seconds_time_t = static_cast<time_t>(seconds_64);
69
70 if(seconds_64 - seconds_time_t != 0) {
71 throw Invalid_Argument("calendar_point::to_std_timepoint time_t overflow");
72 }
73
74 return std::chrono::system_clock::from_time_t(seconds_time_t);
75}
uint64_t seconds_since_epoch() const
Definition calendar.cpp:62

References seconds_since_epoch().

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

77 {
78 // desired format: <YYYY>-<MM>-<dd>T<HH>:<mm>:<ss>
79 std::stringstream output;
80 output << std::setfill('0') << std::setw(4) << year() << "-" << std::setw(2) << month() << "-" << std::setw(2)
81 << day() << "T" << std::setw(2) << hour() << ":" << std::setw(2) << minutes() << ":" << std::setw(2)
82 << seconds();
83 return output.str();
84}

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(), seconds_since_epoch(), and to_string().


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