-
Notifications
You must be signed in to change notification settings - Fork 49
/
DateUtils.cpp
69 lines (61 loc) · 1.9 KB
/
DateUtils.cpp
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
#include "DateUtils.h"
#include "StringUtils.h"
#include <sstream>
#include <iomanip>
namespace ApprovalTests
{
std::tm
DateUtils::createTm(int year, int month, int day, int hour, int minute, int second)
{
std::tm timeinfo = tm();
timeinfo.tm_year = year - 1900;
timeinfo.tm_mon = month - 1;
timeinfo.tm_mday = day;
timeinfo.tm_hour = hour;
timeinfo.tm_min = minute;
timeinfo.tm_sec = second;
return timeinfo;
}
std::chrono::system_clock::time_point
DateUtils::createUtcDateTime(int year,
int month,
int day,
int hour,
int minute,
int second) // these are UTC values
{
tm timeinfo = createTm(year, month, day, hour, minute, second);
time_t tt = toUtc(timeinfo);
return std::chrono::system_clock::from_time_t(tt);
}
std::string DateUtils::toString(const std::chrono::system_clock::time_point& dateTime)
{
return toString(dateTime, "%a %Y-%m-%d %H:%M:%S UTC");
}
std::string DateUtils::toString(const std::chrono::system_clock::time_point& dateTime,
const std::string& format)
{
time_t tt = std::chrono::system_clock::to_time_t(dateTime);
tm tm_value = toUtc(tt);
return StringUtils::toString(std::put_time(&tm_value, format.c_str()));
}
time_t DateUtils::toUtc(std::tm& timeinfo)
{
#ifdef _WIN32
std::time_t tt = _mkgmtime(&timeinfo);
#else
time_t tt = timegm(&timeinfo);
#endif
return tt;
}
tm DateUtils::toUtc(time_t& tt)
{
#ifdef _MSC_VER // Visual Studio compiler
std::tm tm_value = {};
gmtime_s(&tm_value, &tt);
#else
tm tm_value = *gmtime(&tt);
#endif
return tm_value;
}
}