blob: b578f6be9f2ae24d0efbb7a152d1ceacf7be0559 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
/*
* Calendar Functions
* (C) 1999-2010 Jack Lloyd
*
* Botan is released under the Simplified BSD License (see license.txt)
*/
#include <botan/calendar.h>
#include <botan/exceptn.h>
#include <ctime>
namespace Botan {
namespace {
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;
}
}
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.");
std::tm tm;
tm.tm_sec = seconds;
tm.tm_min = minutes;
tm.tm_hour = hour;
tm.tm_mday = day;
tm.tm_mon = month - 1;
tm.tm_year = year - 1900;
// Convert std::tm to std::time_t
// http://stackoverflow.com/questions/16647819/timegm-cross-platform
#if defined(BOTAN_TARGET_OS_IS_WINDOWS)
#define timegm _mkgmtime
#endif
std::time_t tt = timegm(&tm);
return std::chrono::system_clock::from_time_t(tt);
}
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);
}
}
|