OSDN Git Service

Change idx sub dir's time stamp when saving idx.
[fukui-no-namari/dialektos.git] / src / http_date.cxx
1 /*
2  * Copyright (C) 2009 by Aiwota Programmer
3  * aiwotaprog@tetteke.tk
4  *
5  * This file is part of Dialektos.
6  *
7  * Dialektos is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * Dialektos is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with Dialektos.  If not, see <http://www.gnu.org/licenses/>.
19  */
20
21
22 #include "http_date.hxx"
23
24 #include <boost/xpressive/xpressive.hpp>
25 #include <boost/format.hpp>
26 #include <boost/date_time/posix_time/posix_time.hpp>
27 #include <sstream>
28 #include <string>
29 #include <map>
30
31
32 namespace dialektos {
33
34 boost::posix_time::ptime rfc1123_to_ptime(const std::string& rfc1123) {
35   using namespace boost::xpressive;
36
37   std::map<std::string, std::string> months;
38   months["Jan"] = "01";
39   months["Feb"] = "02";
40   months["Mar"] = "03";
41   months["Apr"] = "04";
42   months["May"] = "05";
43   months["Jun"] = "06";
44   months["Jul"] = "07";
45   months["Aug"] = "08";
46   months["Sep"] = "09";
47   months["Oct"] = "10";
48   months["Nov"] = "11";
49   months["Dec"] = "12";
50
51   // Sun, 01 Jan 1999 09:59:59 GMT   RFC1123
52   const sregex regex = (s1=-repeat<3>(alpha)) >> ", "
53   >> (s2=-repeat<2>(_d)) >> ' '
54   >> (s3=-repeat<3>(alpha)) >> ' '
55   >> (s4=-repeat<4>(_d)) >> ' '
56   >> (s5=-repeat<2>(_d)) >> ':'
57   >> (s6=-repeat<2>(_d)) >> ':'
58   >> (s7=-repeat<2>(_d)) >> ' '
59   >> "GMT";
60
61   smatch what;
62   if (!regex_match(rfc1123, what, regex)) throw HTTPDateError();
63
64 //  const std::string day_of_week = what[1];
65   const std::string day = what[2];
66   const std::string _month = what[3];
67   const std::string year = what[4];
68   const std::string hour = what[5];
69   const std::string min = what[6];
70   const std::string sec = what[7];
71
72   if (months.find(_month) == months.end()) throw HTTPDateError();
73
74   const std::string month = months[_month];
75
76   std::stringstream ss;
77   ss << boost::format("%1%-%2%-%3% %4%:%5%:%6%")
78   % year % month % day % hour % min % sec << std::flush;
79
80   try {
81     boost::posix_time::ptime ptime =
82       boost::posix_time::time_from_string(ss.str());
83     return ptime;
84   } catch (const std::out_of_range& e) {
85     throw HTTPDateError();
86   }
87 }
88
89
90
91 } // namespace dialektos