/src/wt/src/web/DateUtils.C
Line | Count | Source |
1 | | #include "DateUtils.h" |
2 | | |
3 | | #include "Wt/cpp20/date.hpp" |
4 | | #include "Wt/WStringStream.h" |
5 | | |
6 | | namespace { |
7 | 0 | inline void pad2(Wt::WStringStream& buf, int value) { |
8 | 0 | if (value < 10) |
9 | 0 | buf << '0'; |
10 | 0 | buf << value; |
11 | 0 | } |
12 | | } |
13 | | |
14 | | namespace Wt { |
15 | | namespace DateUtils { |
16 | | void httpDateBuf(const std::chrono::system_clock::time_point& tp, |
17 | | Wt::WStringStream& buf) |
18 | 0 | { |
19 | 0 | const auto days = Wt::cpp20::date::floor<Wt::cpp20::date::days>(tp); |
20 | 0 | const auto ymd = Wt::cpp20::date::year_month_day(days); |
21 | 0 | const auto weekday = Wt::cpp20::date::weekday(days); |
22 | 0 | const auto timeOfDay = Wt::cpp20::date::floor<std::chrono::seconds>(tp - days); |
23 | 0 | const auto hms = Wt::cpp20::date::hh_mm_ss<std::chrono::seconds>(timeOfDay); |
24 | |
|
25 | 0 | static const char dayOfWeekStr[7][4] |
26 | 0 | = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }; |
27 | 0 | static const char monthStr[12][4] |
28 | 0 | = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", |
29 | 0 | "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; |
30 | | |
31 | | // Wed, 15 Jan 2014 21:20:01 GMT |
32 | 0 | buf << dayOfWeekStr[weekday.c_encoding()] << ", "; |
33 | 0 | pad2(buf, static_cast<unsigned>(ymd.day())); |
34 | 0 | buf << ' '; |
35 | 0 | buf << monthStr[static_cast<unsigned>(ymd.month()) - 1] << ' ' |
36 | 0 | << static_cast<int>(ymd.year()) << ' '; |
37 | |
|
38 | 0 | pad2(buf, hms.hours().count()); |
39 | 0 | buf << ':'; |
40 | 0 | pad2(buf, hms.minutes().count()); |
41 | 0 | buf << ':'; |
42 | 0 | pad2(buf, hms.seconds().count()); |
43 | 0 | buf << " GMT"; |
44 | 0 | } |
45 | | |
46 | | std::string httpDate(const std::chrono::system_clock::time_point& tp) |
47 | 0 | { |
48 | 0 | Wt::WStringStream ss; |
49 | 0 | httpDateBuf(tp, ss); |
50 | 0 | return ss.str(); |
51 | 0 | } |
52 | | } |
53 | | } |