diff options
author | Jack Lloyd <[email protected]> | 2020-11-08 05:08:22 -0500 |
---|---|---|
committer | Jack Lloyd <[email protected]> | 2020-11-08 05:09:16 -0500 |
commit | fe0270b367b8c016578a4b25a23a97473c4e911a (patch) | |
tree | 2c5516021f85fe3bae41825b0a206760fb8b1030 | |
parent | 9ebdba973c9c86c53e42cc2636e6f373d5e5bc98 (diff) |
More calendar cleanups
Use m_ prefix, etc
-rw-r--r-- | src/cli/roughtime.cpp | 4 | ||||
-rw-r--r-- | src/lib/asn1/asn1_time.cpp | 16 | ||||
-rw-r--r-- | src/lib/utils/calendar.cpp | 36 | ||||
-rw-r--r-- | src/lib/utils/calendar.h | 43 | ||||
-rw-r--r-- | src/tests/test_utils.cpp | 30 | ||||
-rw-r--r-- | src/tests/unit_x509.cpp | 2 |
6 files changed, 63 insertions, 68 deletions
diff --git a/src/cli/roughtime.cpp b/src/cli/roughtime.cpp index 239143b7a..11ac6990f 100644 --- a/src/cli/roughtime.cpp +++ b/src/cli/roughtime.cpp @@ -47,7 +47,7 @@ class RoughtimeCheck final : public Command if(flag_set("raw-time")) { output() << Botan::Roughtime::Response::sys_microseconds64(response.utc_midpoint()).time_since_epoch().count(); } else - { output() << Botan::calendar_point_from_time_point(response.utc_midpoint()).to_string(); } + { output() << Botan::calendar_point(response.utc_midpoint()).to_string(); } output() << " (+-" << Botan::Roughtime::Response::microseconds32(response.utc_radius()).count() << "us)\n"; } } @@ -116,7 +116,7 @@ class Roughtime final : public Command if(flag_set("raw-time")) { output() << "UTC " << Botan::Roughtime::Response::sys_microseconds64(response.utc_midpoint()).time_since_epoch().count(); } else - { output() << "UTC " << Botan::calendar_point_from_time_point(response.utc_midpoint()).to_string(); } + { output() << "UTC " << Botan::calendar_point(response.utc_midpoint()).to_string(); } output() << " (+-" << Botan::Roughtime::Response::microseconds32(response.utc_radius()).count() << "us)"; if(!response.validate(public_key)) { diff --git a/src/lib/asn1/asn1_time.cpp b/src/lib/asn1/asn1_time.cpp index a49e92d1b..7acaed5d8 100644 --- a/src/lib/asn1/asn1_time.cpp +++ b/src/lib/asn1/asn1_time.cpp @@ -18,14 +18,14 @@ namespace Botan { ASN1_Time::ASN1_Time(const std::chrono::system_clock::time_point& time) { - calendar_point cal = calendar_point_from_time_point(time); - - m_year = cal.get_year(); - m_month = cal.get_month(); - m_day = cal.get_day(); - m_hour = cal.get_hour(); - m_minute = cal.get_minutes(); - m_second = cal.get_seconds(); + calendar_point cal(time); + + m_year = cal.year(); + m_month = cal.month(); + m_day = cal.day(); + m_hour = cal.hour(); + m_minute = cal.minutes(); + m_second = cal.seconds(); m_tag = (m_year >= 2050) ? GENERALIZED_TIME : UTC_TIME; } diff --git a/src/lib/utils/calendar.cpp b/src/lib/utils/calendar.cpp index b45f2b4bf..71ffe655d 100644 --- a/src/lib/utils/calendar.cpp +++ b/src/lib/utils/calendar.cpp @@ -59,7 +59,7 @@ size_t days_since_epoch(uint32_t year, uint32_t month, uint32_t day) std::chrono::system_clock::time_point calendar_point::to_std_timepoint() const { - if(get_year() < 1970) + if(year() < 1970) throw Invalid_Argument("calendar_point::to_std_timepoint() does not support years before 1970"); // 32 bit time_t ends at January 19, 2038 @@ -68,20 +68,20 @@ std::chrono::system_clock::time_point calendar_point::to_std_timepoint() const BOTAN_IF_CONSTEXPR(sizeof(std::time_t) == 4) { - if(get_year() > 2037) + if(year() > 2037) { throw Invalid_Argument("calendar_point::to_std_timepoint() does not support years after 2037 on this system"); } } // This upper bound is completely arbitrary - if(get_year() >= 2400) + if(year() >= 2400) { throw Invalid_Argument("calendar_point::to_std_timepoint() does not support years after 2400"); } - const uint64_t seconds_64 = (days_since_epoch(get_year(), get_month(), get_day()) * 86400) + - (get_hour() * 60 * 60) + (get_minutes() * 60) + get_seconds(); + const uint64_t seconds_64 = (days_since_epoch(year(), month(), day()) * 86400) + + (hour() * 60 * 60) + (minutes() * 60) + seconds(); const time_t seconds_time_t = static_cast<time_t>(seconds_64); @@ -98,25 +98,25 @@ std::string calendar_point::to_string() const // desired format: <YYYY>-<MM>-<dd>T<HH>:<mm>:<ss> std::stringstream output; output << std::setfill('0') - << std::setw(4) << get_year() << "-" - << std::setw(2) << get_month() << "-" - << std::setw(2) << get_day() << "T" - << std::setw(2) << get_hour() << ":" - << std::setw(2) << get_minutes() << ":" - << std::setw(2) << get_seconds(); + << std::setw(4) << year() << "-" + << std::setw(2) << month() << "-" + << std::setw(2) << day() << "T" + << std::setw(2) << hour() << ":" + << std::setw(2) << minutes() << ":" + << std::setw(2) << seconds(); return output.str(); } -calendar_point calendar_point_from_time_point(const std::chrono::system_clock::time_point& time_point) +calendar_point::calendar_point(const std::chrono::system_clock::time_point& time_point) { std::tm tm = do_gmtime(std::chrono::system_clock::to_time_t(time_point)); - return calendar_point(tm.tm_year + 1900, - tm.tm_mon + 1, - tm.tm_mday, - tm.tm_hour, - tm.tm_min, - tm.tm_sec); + m_year = tm.tm_year + 1900; + m_month = tm.tm_mon + 1; + m_day = tm.tm_mday; + m_hour = tm.tm_hour; + m_minutes = tm.tm_min; + m_seconds = tm.tm_sec; } } diff --git a/src/lib/utils/calendar.h b/src/lib/utils/calendar.h index f58836aba..130cac4d8 100644 --- a/src/lib/utils/calendar.h +++ b/src/lib/utils/calendar.h @@ -23,24 +23,24 @@ class BOTAN_TEST_API calendar_point public: /** The year */ - uint32_t get_year() const { return year; } + uint32_t year() const { return m_year; } /** The month, 1 through 12 for Jan to Dec */ - uint32_t get_month() const { return month; } + uint32_t month() const { return m_month; } /** The day of the month, 1 through 31 (or 28 or 30 based on month */ - uint32_t get_day() const { return day; } + uint32_t day() const { return m_day; } /** Hour in 24-hour form, 0 to 23 */ - uint32_t get_hour() const { return hour; } + uint32_t hour() const { return m_hour; } /** Minutes in the hour, 0 to 60 */ - uint32_t get_minutes() const { return minutes; } + uint32_t minutes() const { return m_minutes; } /** Seconds in the minute, 0 to 60, but might be slightly larger to deal with leap seconds on some systems */ - uint32_t get_seconds() const { return seconds; } + uint32_t seconds() const { return m_seconds; } /** * Initialize a calendar_point @@ -52,7 +52,13 @@ class BOTAN_TEST_API calendar_point * @param sec the second */ calendar_point(uint32_t y, uint32_t mon, uint32_t d, uint32_t h, uint32_t min, uint32_t sec) : - year(y), month(mon), day(d), hour(h), minutes(min), seconds(sec) {} + m_year(y), m_month(mon), m_day(d), m_hour(h), m_minutes(min), m_seconds(sec) {} + + /** + * Convert a time_point to a calendar_point + * @param time_point a time point from the system clock + */ + calendar_point(const std::chrono::system_clock::time_point& time_point); /** * Returns an STL timepoint object @@ -66,25 +72,14 @@ class BOTAN_TEST_API calendar_point std::string to_string() const; private: - /* - The member variables are public for historical reasons. Use the get_xxx() functions - defined above. These members will be made private in a future major release. - */ - uint32_t year; - uint32_t month; - uint32_t day; - uint32_t hour; - uint32_t minutes; - uint32_t seconds; + uint32_t m_year; + uint32_t m_month; + uint32_t m_day; + uint32_t m_hour; + uint32_t m_minutes; + uint32_t m_seconds; }; -/** -* Convert a time_point to a calendar_point -* @param time_point a time point from the system clock -* @return calendar_point object representing this time point -*/ -calendar_point BOTAN_TEST_API calendar_point_from_time_point(const std::chrono::system_clock::time_point& time_point); - } #endif diff --git a/src/tests/test_utils.cpp b/src/tests/test_utils.cpp index 9ca76f6cd..0bd44a968 100644 --- a/src/tests/test_utils.cpp +++ b/src/tests/test_utils.cpp @@ -438,26 +438,26 @@ class Date_Format_Tests final : public Text_Based_Test if(type == "valid" || type == "valid.not_std" || type == "valid.64_bit_time_t") { Botan::calendar_point c(d[0], d[1], d[2], d[3], d[4], d[5]); - result.test_is_eq(date_str + " year", c.get_year(), d[0]); - result.test_is_eq(date_str + " month", c.get_month(), d[1]); - result.test_is_eq(date_str + " day", c.get_day(), d[2]); - result.test_is_eq(date_str + " hour", c.get_hour(), d[3]); - result.test_is_eq(date_str + " minute", c.get_minutes(), d[4]); - result.test_is_eq(date_str + " second", c.get_seconds(), d[5]); - - if(type == "valid.not_std" || (type == "valid.64_bit_time_t" && c.get_year() > 2037 && sizeof(std::time_t) == 4)) + result.test_is_eq(date_str + " year", c.year(), d[0]); + result.test_is_eq(date_str + " month", c.month(), d[1]); + result.test_is_eq(date_str + " day", c.day(), d[2]); + result.test_is_eq(date_str + " hour", c.hour(), d[3]); + result.test_is_eq(date_str + " minute", c.minutes(), d[4]); + result.test_is_eq(date_str + " second", c.seconds(), d[5]); + + if(type == "valid.not_std" || (type == "valid.64_bit_time_t" && c.year() > 2037 && sizeof(std::time_t) == 4)) { result.test_throws("valid but out of std::timepoint range", [c]() { c.to_std_timepoint(); }); } else { - Botan::calendar_point c2 = Botan::calendar_point_from_time_point(c.to_std_timepoint()); - result.test_is_eq(date_str + " year", c2.get_year(), d[0]); - result.test_is_eq(date_str + " month", c2.get_month(), d[1]); - result.test_is_eq(date_str + " day", c2.get_day(), d[2]); - result.test_is_eq(date_str + " hour", c2.get_hour(), d[3]); - result.test_is_eq(date_str + " minute", c2.get_minutes(), d[4]); - result.test_is_eq(date_str + " second", c2.get_seconds(), d[5]); + Botan::calendar_point c2(c.to_std_timepoint()); + result.test_is_eq(date_str + " year", c2.year(), d[0]); + result.test_is_eq(date_str + " month", c2.month(), d[1]); + result.test_is_eq(date_str + " day", c2.day(), d[2]); + result.test_is_eq(date_str + " hour", c2.hour(), d[3]); + result.test_is_eq(date_str + " minute", c2.minutes(), d[4]); + result.test_is_eq(date_str + " second", c2.seconds(), d[5]); } } else if(type == "invalid") diff --git a/src/tests/unit_x509.cpp b/src/tests/unit_x509.cpp index 98343ebcf..28b4da56a 100644 --- a/src/tests/unit_x509.cpp +++ b/src/tests/unit_x509.cpp @@ -30,7 +30,7 @@ namespace { Botan::X509_Time from_date(const int y, const int m, const int d) { - const size_t this_year = Botan::calendar_point_from_time_point(std::chrono::system_clock::now()).get_year(); + const size_t this_year = Botan::calendar_point(std::chrono::system_clock::now()).year(); Botan::calendar_point t(static_cast<uint32_t>(this_year + y), m, d, 0, 0, 0); return Botan::X509_Time(t.to_std_timepoint()); |