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/calendar.cpp | |
parent | 9bca04a5999060f098221615ab4ce5f89ea67c8a (diff) |
Fix time range issue for 32 bit platforms
Diffstat (limited to 'src/lib/utils/calendar.cpp')
-rw-r--r-- | src/lib/utils/calendar.cpp | 26 |
1 files changed, 26 insertions, 0 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) |