diff options
author | Simon Warta <[email protected]> | 2015-07-16 10:40:12 +0200 |
---|---|---|
committer | Simon Warta <[email protected]> | 2015-07-16 15:19:22 +0200 |
commit | e9ef1088bced02c0ed10a460dc0fbc296b999a07 (patch) | |
tree | 6edebd24be2a611fddd94a9391c21595af484791 /src/lib/utils | |
parent | 9bca04a5999060f098221615ab4ce5f89ea67c8a (diff) |
Fix time range issue for 32 bit platforms
Diffstat (limited to 'src/lib/utils')
-rw-r--r-- | src/lib/utils/calendar.cpp | 26 | ||||
-rw-r--r-- | src/lib/utils/calendar.h | 19 |
2 files changed, 39 insertions, 6 deletions
diff --git a/src/lib/utils/calendar.cpp b/src/lib/utils/calendar.cpp index b578f6be9..0ef8be356 100644 --- a/src/lib/utils/calendar.cpp +++ b/src/lib/utils/calendar.cpp @@ -1,6 +1,7 @@ /* * Calendar Functions * (C) 1999-2010 Jack Lloyd +* (C) 2015 Simon Warta (Kullo GmbH) * * Botan is released under the Simplified BSD License (see license.txt) */ @@ -8,6 +9,8 @@ #include <botan/calendar.h> #include <botan/exceptn.h> #include <ctime> +#include <sstream> +#include <iomanip> namespace Botan { @@ -38,6 +41,13 @@ std::chrono::system_clock::time_point calendar_point::to_std_timepoint() if (year < 1900) throw Invalid_Argument("calendar_point::to_std_timepoint() does not support years before 1990."); + // 32 bit time_t ends at January 19, 2038 + // https://msdn.microsoft.com/en-us/library/2093ets1.aspx + // For consistency reasons, throw after 2037 as long as + // no other implementation is available. + if (year > 2037) + throw Invalid_Argument("calendar_point::to_std_timepoint() does not support years after 2037."); + std::tm tm; tm.tm_sec = seconds; tm.tm_min = minutes; @@ -52,10 +62,26 @@ std::chrono::system_clock::time_point calendar_point::to_std_timepoint() #define timegm _mkgmtime #endif std::time_t tt = timegm(&tm); + if (tt == -1) + throw Invalid_Argument("calendar_point couldn't be converted: " + to_string()); return std::chrono::system_clock::from_time_t(tt); } +std::string calendar_point::to_string() const + { + // desired format: <YYYY>-<MM>-<dd>T<HH>:<mm>:<ss> + std::stringstream output; + { + using namespace std; + output << setfill('0') + << setw(4) << year << "-" << setw(2) << month << "-" << setw(2) << day + << "T" + << setw(2) << hour << ":" << setw(2) << minutes << ":" << setw(2) << seconds; + } + return output.str(); + } + calendar_point calendar_value( const std::chrono::system_clock::time_point& time_point) diff --git a/src/lib/utils/calendar.h b/src/lib/utils/calendar.h index 4f882e49f..aa34d8448 100644 --- a/src/lib/utils/calendar.h +++ b/src/lib/utils/calendar.h @@ -1,6 +1,7 @@ /* * Calendar Functions * (C) 1999-2009 Jack Lloyd +* (C) 2015 Simon Warta (Kullo GmbH) * * Botan is released under the Simplified BSD License (see license.txt) */ @@ -22,21 +23,21 @@ struct BOTAN_DLL calendar_point u32bit year; /** The month, 1 through 12 for Jan to Dec */ - byte month; + u32bit month; /** The day of the month, 1 through 31 (or 28 or 30 based on month */ - byte day; + u32bit day; /** Hour in 24-hour form, 0 to 23 */ - byte hour; + u32bit hour; /** Minutes in the hour, 0 to 60 */ - byte minutes; + u32bit minutes; /** Seconds in the minute, 0 to 60, but might be slightly larger to deal with leap seconds on some systems */ - byte seconds; + u32bit seconds; /** * Initialize a calendar_point @@ -47,13 +48,19 @@ struct BOTAN_DLL calendar_point * @param min the minute * @param sec the second */ - calendar_point(u32bit y, byte mon, byte d, byte h, byte min, byte sec) : + calendar_point(u32bit y, u32bit mon, u32bit d, u32bit h, u32bit min, u32bit sec) : year(y), month(mon), day(d), hour(h), minutes(min), seconds(sec) {} /** * Returns an STL timepoint object */ std::chrono::system_clock::time_point to_std_timepoint(); + + /** + * Returns a human readable string of the struct's components. + * Formatting might change over time. Currently it is RFC339 'iso-date-time'. + */ + std::string to_string() const; }; /** |