1 | // $Id: Date.cc 685 2008-08-04 14:53:58Z peter $ |
---|
2 | |
---|
3 | /* |
---|
4 | Copyright (C) 2007 Peter Johansson |
---|
5 | Copyright (C) 2008 Jari Häkkinen |
---|
6 | |
---|
7 | This file is part of svndigest, http://dev.thep.lu.se/svndigest |
---|
8 | |
---|
9 | svndigest is free software; you can redistribute it and/or modify it |
---|
10 | under the terms of the GNU General Public License as published by |
---|
11 | the Free Software Foundation; either version 2 of the License, or |
---|
12 | (at your option) any later version. |
---|
13 | |
---|
14 | svndigest is distributed in the hope that it will be useful, but |
---|
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
17 | General Public License for more details. |
---|
18 | |
---|
19 | You should have received a copy of the GNU General Public License |
---|
20 | along with this program; if not, write to the Free Software |
---|
21 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
---|
22 | 02111-1307, USA. |
---|
23 | */ |
---|
24 | |
---|
25 | #include "Date.h" |
---|
26 | |
---|
27 | #include <algorithm> |
---|
28 | #include <sstream> |
---|
29 | #include <string> |
---|
30 | |
---|
31 | namespace theplu { |
---|
32 | namespace svndigest { |
---|
33 | |
---|
34 | static const char* wdays_[7] = { |
---|
35 | "Sunday","Monday", "Tuesday", "Wednesday", "Thursday", "Friday", |
---|
36 | "Saturday" |
---|
37 | }; |
---|
38 | |
---|
39 | |
---|
40 | static const char* month_[12] = { |
---|
41 | "January", "February", "March", "April", "May", "June", "July", "August", |
---|
42 | "September", "October", "November", "December" |
---|
43 | }; |
---|
44 | |
---|
45 | |
---|
46 | Date::Date(void) |
---|
47 | { |
---|
48 | std::time(&time_); |
---|
49 | } |
---|
50 | |
---|
51 | |
---|
52 | Date::Date(const Date& other) |
---|
53 | : time_(other.time_) |
---|
54 | { |
---|
55 | } |
---|
56 | |
---|
57 | |
---|
58 | Date::Date(std::string str) |
---|
59 | { |
---|
60 | std::time(&time_); |
---|
61 | svntime(str); |
---|
62 | } |
---|
63 | |
---|
64 | |
---|
65 | std::string Date::difftime(const Date& other) const |
---|
66 | { |
---|
67 | std::stringstream ss; |
---|
68 | time_t t0 = std::min(seconds(), other.seconds()); |
---|
69 | time_t t1 = std::max(seconds(), other.seconds()); |
---|
70 | struct tm* last = std::gmtime(&t1); |
---|
71 | |
---|
72 | unsigned int year=0; |
---|
73 | while (t0<=t1) { |
---|
74 | ++year; |
---|
75 | --last->tm_year; |
---|
76 | t1=mktime(last); |
---|
77 | } |
---|
78 | --year; |
---|
79 | ++last->tm_year; |
---|
80 | t1=mktime(last); |
---|
81 | if (year) { |
---|
82 | ss << year << " year"; |
---|
83 | if (year>1) |
---|
84 | ss << "s"; |
---|
85 | ss << " "; |
---|
86 | } |
---|
87 | |
---|
88 | unsigned int month=0; |
---|
89 | while (t0<=t1) { |
---|
90 | ++month; |
---|
91 | --last->tm_mon; |
---|
92 | t1=mktime(last); |
---|
93 | } |
---|
94 | --month; |
---|
95 | ++last->tm_mon; |
---|
96 | t1=mktime(last); |
---|
97 | if (month || year) { |
---|
98 | ss << month << " month"; |
---|
99 | if (month>1 || (year && !month)) |
---|
100 | ss << "s"; |
---|
101 | ss << " and "; |
---|
102 | } |
---|
103 | |
---|
104 | |
---|
105 | unsigned int day = (t1-t0)/24/60/60; |
---|
106 | ss << day << " day"; |
---|
107 | if (day!=1) |
---|
108 | ss << "s"; |
---|
109 | ss << " "; |
---|
110 | |
---|
111 | return ss.str(); |
---|
112 | } |
---|
113 | |
---|
114 | |
---|
115 | void Date::svntime(std::string str) |
---|
116 | { |
---|
117 | std::stringstream sstream(str); |
---|
118 | struct tm* timeinfo = std::localtime(&time_); |
---|
119 | |
---|
120 | unsigned int year, month, day, hour, minute, second; |
---|
121 | std::string tmp; |
---|
122 | std::getline(sstream,tmp,'-'); |
---|
123 | year=atoi(tmp.c_str()); |
---|
124 | timeinfo->tm_year = year - 1900; |
---|
125 | |
---|
126 | std::getline(sstream,tmp,'-'); |
---|
127 | month=atoi(tmp.c_str()); |
---|
128 | timeinfo->tm_mon = month - 1; |
---|
129 | |
---|
130 | std::getline(sstream,tmp,'T'); |
---|
131 | day=atoi(tmp.c_str()); |
---|
132 | timeinfo->tm_mday = day; |
---|
133 | |
---|
134 | std::getline(sstream,tmp,':'); |
---|
135 | hour=atoi(tmp.c_str()); |
---|
136 | timeinfo->tm_hour = hour; |
---|
137 | |
---|
138 | std::getline(sstream,tmp,':'); |
---|
139 | minute=atoi(tmp.c_str()); |
---|
140 | timeinfo->tm_min = minute; |
---|
141 | |
---|
142 | std::getline(sstream,tmp,'.'); |
---|
143 | second=atoi(tmp.c_str()); |
---|
144 | timeinfo->tm_sec = second; |
---|
145 | |
---|
146 | time_ = mktime(timeinfo); |
---|
147 | time_ += timeinfo->tm_gmtoff; |
---|
148 | } |
---|
149 | |
---|
150 | |
---|
151 | std::string Date::operator()(std::string format) const |
---|
152 | { |
---|
153 | std::stringstream ss; |
---|
154 | struct tm* timeinfo = std::gmtime(&time_); |
---|
155 | for (std::string::iterator i=format.begin(); i!=format.end(); ++i) { |
---|
156 | if (*i == '%' && ++i !=format.end()) { |
---|
157 | if (*i == 'a') |
---|
158 | ss << std::string(wdays_[timeinfo->tm_wday]).substr(0,3); |
---|
159 | else if (*i == 'A') |
---|
160 | ss << wdays_[timeinfo->tm_wday]; |
---|
161 | else if (*i == 'b') |
---|
162 | ss << std::string(month_[timeinfo->tm_mon]).substr(0,3); |
---|
163 | else if (*i == 'B') |
---|
164 | ss << month_[timeinfo->tm_mon]; |
---|
165 | else if (*i == 'd') |
---|
166 | ss << timeinfo->tm_mday; |
---|
167 | else if (*i == 'e') { |
---|
168 | if (timeinfo->tm_mday<10) |
---|
169 | ss << "0"; |
---|
170 | ss << timeinfo->tm_mday; |
---|
171 | } |
---|
172 | else if (*i == 'H') { |
---|
173 | if (timeinfo->tm_hour<10) |
---|
174 | ss << "0"; |
---|
175 | ss << timeinfo->tm_hour; |
---|
176 | } |
---|
177 | else if (*i == 'I') { |
---|
178 | int tmp = (timeinfo->tm_hour + 11) % 12 + 1; |
---|
179 | if (tmp<10) |
---|
180 | ss << "0"; |
---|
181 | ss << tmp; |
---|
182 | } |
---|
183 | else if (*i == 'j') |
---|
184 | ss << timeinfo->tm_yday+1; |
---|
185 | else if (*i == 'm') { |
---|
186 | if (timeinfo->tm_mon<9) |
---|
187 | ss << "0"; |
---|
188 | ss << timeinfo->tm_mon+1; |
---|
189 | } |
---|
190 | else if (*i == 'M') { |
---|
191 | if (timeinfo->tm_min<10) |
---|
192 | ss << "0"; |
---|
193 | ss << timeinfo->tm_min; |
---|
194 | } |
---|
195 | else if (*i == 'P') |
---|
196 | if (timeinfo->tm_hour<12) |
---|
197 | ss << "AM"; |
---|
198 | else |
---|
199 | ss << "PM"; |
---|
200 | else if (*i == 'S') { |
---|
201 | if (timeinfo->tm_sec<10) |
---|
202 | ss << "0"; |
---|
203 | ss << timeinfo->tm_sec; |
---|
204 | } |
---|
205 | else if (*i == 'w') |
---|
206 | if (timeinfo->tm_wday==0) |
---|
207 | ss << "7"; |
---|
208 | else |
---|
209 | ss << timeinfo->tm_wday; |
---|
210 | else if (*i == 'y') { |
---|
211 | int y(timeinfo->tm_year % 100); |
---|
212 | if (y<10) |
---|
213 | ss << "0"; |
---|
214 | ss << y; |
---|
215 | } |
---|
216 | else if (*i == 'Y') |
---|
217 | ss << timeinfo->tm_year+1900; |
---|
218 | else if (*i == 'Z') |
---|
219 | ss << timeinfo->tm_zone; |
---|
220 | else { |
---|
221 | ss << '%'; |
---|
222 | --i; |
---|
223 | } |
---|
224 | } |
---|
225 | else |
---|
226 | ss << *i; |
---|
227 | } |
---|
228 | |
---|
229 | return ss.str(); |
---|
230 | } |
---|
231 | |
---|
232 | |
---|
233 | const Date& Date::operator=(const Date& rhs) |
---|
234 | { |
---|
235 | time_ = rhs.time_; |
---|
236 | return *this; |
---|
237 | } |
---|
238 | |
---|
239 | |
---|
240 | }} |
---|