aboutsummaryrefslogtreecommitdiffstats
path: root/src/utils/calendar.cpp
diff options
context:
space:
mode:
authorlloyd <[email protected]>2012-02-20 21:12:29 +0000
committerlloyd <[email protected]>2012-02-20 21:12:29 +0000
commit49f333282279cc22fa8af7423447973b9dcfeee9 (patch)
tree574a19e310aeb158dcc08016b3ce91a9d0ac0f81 /src/utils/calendar.cpp
parent73e7730306b524aeee6fcfe8dd9f41b9673cf31b (diff)
Remove get_nanoseconds_clock as we'll rely on std::chrono's high
resolution clock for this in C++11. Now that the only remaining function in time.h is calendar_point, rename the header to calendar.h. Hopefully that last use will go away once a TR2 datetime library becomes available. Use std::chrono inside the library benchmark code.
Diffstat (limited to 'src/utils/calendar.cpp')
-rw-r--r--src/utils/calendar.cpp64
1 files changed, 64 insertions, 0 deletions
diff --git a/src/utils/calendar.cpp b/src/utils/calendar.cpp
new file mode 100644
index 000000000..a51ef876c
--- /dev/null
+++ b/src/utils/calendar.cpp
@@ -0,0 +1,64 @@
+/*
+* Calendar Functions
+* (C) 1999-2010 Jack Lloyd
+*
+* Distributed under the terms of the Botan license
+*/
+
+#include <botan/calendar.h>
+#include <botan/exceptn.h>
+#include <ctime>
+
+namespace Botan {
+
+namespace {
+
+/*
+* Combine a two time values into a single one
+*/
+u64bit combine_timers(u32bit seconds, u32bit parts, u32bit parts_hz)
+ {
+ static const u64bit NANOSECONDS_UNITS = 1000000000;
+
+ u64bit res = seconds * NANOSECONDS_UNITS;
+ res += parts * (NANOSECONDS_UNITS / parts_hz);
+ return res;
+ }
+
+std::tm do_gmtime(std::time_t time_val)
+ {
+ std::tm tm;
+
+#if defined(BOTAN_TARGET_OS_HAS_GMTIME_S)
+ gmtime_s(&tm, &time_val); // Windows
+#elif defined(BOTAN_TARGET_OS_HAS_GMTIME_R)
+ gmtime_r(&time_val, &tm); // Unix/SUSv2
+#else
+ std::tm* tm_p = std::gmtime(&time_val);
+ if (tm_p == 0)
+ throw Encoding_Error("time_t_to_tm could not convert");
+ tm = *tm_p;
+#endif
+
+ return tm;
+ }
+
+}
+
+/*
+* Convert a time_point to a calendar_point
+*/
+calendar_point calendar_value(
+ 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);
+ }
+
+}