2 * Copyright (C) 2009 by Aiwota Programmer
3 * aiwotaprog@tetteke.tk
5 * This file is part of Dialektos.
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.
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.
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/>.
22 #include "http_date.hxx"
24 #include <boost/xpressive/xpressive.hpp>
25 #include <boost/format.hpp>
26 #include <boost/date_time/posix_time/posix_time.hpp>
34 boost::posix_time::ptime rfc1123_to_ptime(const std::string& rfc1123) {
35 using namespace boost::xpressive;
37 std::map<std::string, std::string> months;
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)) >> ' '
62 if (!regex_match(rfc1123, what, regex)) throw HTTPDateError();
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];
72 if (months.find(_month) == months.end()) throw HTTPDateError();
74 const std::string month = months[_month];
77 ss << boost::format("%1%-%2%-%3% %4%:%5%:%6%")
78 % year % month % day % hour % min % sec << std::flush;
81 boost::posix_time::ptime ptime =
82 boost::posix_time::time_from_string(ss.str());
84 } catch (const std::out_of_range& e) {
85 throw HTTPDateError();
91 } // namespace dialektos