1 #include <mbgl/util/chrono.hpp>
2 
3 #include <parsedate/parsedate.hpp>
4 
5 #include <cstdio>
6 #include <ctime>
7 
8 #if defined(_WINDOWS)
9 #define _gmtime(t, i) gmtime_s(i, t)
10 #else
11 #define _gmtime(t, i) gmtime_r(t, i)
12 #endif
13 
14 namespace mbgl {
15 namespace util {
16 
17 static const char *week[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
18 static const char *months[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
19                                "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
20 
rfc1123(Timestamp timestamp)21 std::string rfc1123(Timestamp timestamp) {
22     std::time_t time = std::chrono::system_clock::to_time_t(timestamp);
23     std::tm info;
24     _gmtime(&time, &info);
25     char buffer[30];
26     snprintf(buffer, 30, "%s, %02d %s %4d %02d:%02d:%02d GMT", week[info.tm_wday], info.tm_mday,
27              months[info.tm_mon], 1900 + info.tm_year, info.tm_hour, info.tm_min, info.tm_sec);
28     return buffer;
29 }
30 
iso8601(Timestamp timestamp)31 std::string iso8601(Timestamp timestamp) {
32     std::time_t time = std::chrono::system_clock::to_time_t(timestamp);
33     std::tm info;
34     _gmtime(&time, &info);
35     char buffer[30];
36     std::strftime(buffer, sizeof(buffer), "%F %T", &info);
37     return buffer;
38 }
39 
parseTimestamp(const char * timestamp)40 Timestamp parseTimestamp(const char* timestamp) {
41     return std::chrono::time_point_cast<Seconds>(std::chrono::system_clock::from_time_t(parse_date(timestamp)));
42 }
43 
parseTimestamp(const int32_t timestamp)44 Timestamp parseTimestamp(const int32_t timestamp) {
45     return std::chrono::time_point_cast<Seconds>(std::chrono::system_clock::from_time_t(timestamp));
46 }
47 
48 } // namespace util
49 
50 } // namespace mbgl
51