1 /* <!-- copyright */
2 /*
3 * aria2 - The high speed download utility
4 *
5 * Copyright (C) 2015 Tatsuhiro Tsujikawa
6 *
7 * This program 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 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program 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 this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 *
21 * In addition, as a special exception, the copyright holders give
22 * permission to link the code of portions of this program with the
23 * OpenSSL library under certain conditions as described in each
24 * individual source file, and distribute linked combinations
25 * including the two.
26 * You must obey the GNU General Public License in all respects
27 * for all of the code used other than OpenSSL. If you modify
28 * file(s) with this exception, you may extend this exception to your
29 * version of the file(s), but you are not obligated to do so. If you
30 * do not wish to do so, delete this exception statement from your
31 * version. If you delete this exception statement from all source
32 * files in the program, then also delete it here.
33 */
34 /* copyright --> */
35
36 #include "TimeA2.h"
37
38 #include <cstring>
39
40 #include "util.h"
41 #include "array_fun.h"
42
43 namespace aria2 {
44
Time()45 Time::Time() : tp_(Clock::now()), good_(true) {}
46
Time(time_t t)47 Time::Time(time_t t) : tp_(Clock::from_time_t(t)), good_(true) {}
48
reset()49 void Time::reset()
50 {
51 tp_ = Clock::now();
52 good_ = true;
53 }
54
difference() const55 Time::Clock::duration Time::difference() const { return Clock::now() - tp_; }
56
difference(const Time & time) const57 Time::Clock::duration Time::difference(const Time& time) const
58 {
59 return time.tp_ - tp_;
60 }
61
setTimeFromEpoch(time_t t)62 void Time::setTimeFromEpoch(time_t t)
63 {
64 tp_ = Clock::from_time_t(t);
65 good_ = true;
66 }
67
toHTTPDate() const68 std::string Time::toHTTPDate() const
69 {
70 char buf[32];
71 time_t t = getTimeFromEpoch();
72 struct tm* tms = gmtime(&t); // returned struct is statically allocated.
73 size_t r = strftime(buf, sizeof(buf), "%a, %d %b %Y %H:%M:%S GMT", tms);
74 return std::string(&buf[0], &buf[r]);
75 }
76
parse(const std::string & datetime,const std::string & format)77 Time Time::parse(const std::string& datetime, const std::string& format)
78 {
79 struct tm tm;
80 memset(&tm, 0, sizeof(tm));
81 char* r = strptime(datetime.c_str(), format.c_str(), &tm);
82 if (r != datetime.c_str() + datetime.size()) {
83 return Time::null();
84 }
85 time_t thetime = timegm(&tm);
86 if (thetime == -1) {
87 if (tm.tm_year >= 2038 - 1900) {
88 thetime = INT32_MAX;
89 }
90 }
91 return Time(thetime);
92 }
93
parseRFC1123(const std::string & datetime)94 Time Time::parseRFC1123(const std::string& datetime)
95 {
96 return parse(datetime, "%a, %d %b %Y %H:%M:%S GMT");
97 }
98
parseRFC1123Alt(const std::string & datetime)99 Time Time::parseRFC1123Alt(const std::string& datetime)
100 {
101 return parse(datetime, "%a, %d %b %Y %H:%M:%S +0000");
102 }
103
parseRFC850(const std::string & datetime)104 Time Time::parseRFC850(const std::string& datetime)
105 {
106 return parse(datetime, "%a, %d-%b-%y %H:%M:%S GMT");
107 }
108
parseRFC850Ext(const std::string & datetime)109 Time Time::parseRFC850Ext(const std::string& datetime)
110 {
111 return parse(datetime, "%a, %d-%b-%Y %H:%M:%S GMT");
112 }
113
parseAsctime(const std::string & datetime)114 Time Time::parseAsctime(const std::string& datetime)
115 {
116 return parse(datetime, "%a %b %d %H:%M:%S %Y");
117 }
118
parseHTTPDate(const std::string & datetime)119 Time Time::parseHTTPDate(const std::string& datetime)
120 {
121 Time (*funcs[])(const std::string&) = {
122 &parseRFC1123, &parseRFC1123Alt, &parseRFC850,
123 &parseAsctime, &parseRFC850Ext,
124 };
125 for (auto func : funcs) {
126 Time t = func(datetime);
127 if (t.good()) {
128 return t;
129 }
130 }
131 return Time::null();
132 }
133
134 } // namespace aria2
135