-
Notifications
You must be signed in to change notification settings - Fork 0
/
time_decode.cpp
77 lines (72 loc) · 2.54 KB
/
time_decode.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
70
71
72
73
74
75
76
77
#include <string>
#include <ctime>
#include <cctype>
#include "http.hpp"
namespace http {
static int
time_decode_name (std::string::const_iterator& s, std::string::const_iterator const eos, std::string const& list)
{
std::string::const_iterator const s0 = s;
for (std::size_t i = 0; i < 3; ++i)
if (s >= eos || ! std::isalpha (*s++))
return -1;
std::string name (s0, s);
name.push_back (' ');
std::string::size_type i = list.find (name);
if (i == std::string::npos)
return -1;
return i / 4;
}
static int
time_decode_decimal (std::string::const_iterator& s, std::string::const_iterator const eos, std::size_t const n)
{
int v = 0;
for (std::size_t i = 0; i < n; ++i) {
if (s >= eos || ! std::isdigit (*s))
return -1;
v = v * 10 + *s++ - '0';
}
return v;
}
std::time_t
time_decode (std::string const& fmt, std::string const& datime)
{
struct tm dt;
static const std::string a ("Sun Mon Tue Wed Thu Fri Sat ");
static const std::string b ("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec ");
std::string::const_iterator pat = fmt.cbegin ();
std::string::const_iterator const eot = fmt.cend ();
std::string::const_iterator str = datime.cbegin ();
std::string::const_iterator const eos = datime.cend ();
while (pat < eot) {
int c = *pat++;
if ('%' != c) {
if (str >= eos || c != *str++)
return -1;
}
else if (pat < eot) {
c = *pat++;
int e = 0;
switch (c) {
case 'a': dt.tm_wday = e = time_decode_name (str, eos, a); break;
case 'b': dt.tm_mon = e = time_decode_name (str, eos, b); break;
case 'Y': dt.tm_year = e = time_decode_decimal (str, eos, 4) - 1900; break;
case 'm': dt.tm_mon = e = time_decode_decimal (str, eos, 2) - 1; break;
case 'd': dt.tm_mday = e = time_decode_decimal (str, eos, 2); break;
case 'H': dt.tm_hour = e = time_decode_decimal (str, eos, 2); break;
case 'M': dt.tm_min = e = time_decode_decimal (str, eos, 2); break;
case 'S': dt.tm_sec = e = time_decode_decimal (str, eos, 2); break;
default:
if (str >= eos || c != *str++)
return -1;
}
if (e < 0)
return -1;
}
}
if (datime.find ("GMT") != std::string::npos
|| datime.find ("UTC") != std::string::npos)
return timegm (&dt);
return timelocal (&dt);
}
}//namespace http