1 | // $Id: Date.cc 1482 2012-07-19 01:26:16Z peter $ |
---|
2 | |
---|
3 | /* |
---|
4 | Copyright (C) 2007, 2008 Jari Häkkinen, Peter Johansson |
---|
5 | Copyright (C) 2009 Peter Johansson |
---|
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 3 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 svndigest. If not, see <http://www.gnu.org/licenses/>. |
---|
21 | */ |
---|
22 | |
---|
23 | #include "Date.h" |
---|
24 | |
---|
25 | #include "yat/utility.h" |
---|
26 | |
---|
27 | #include <algorithm> |
---|
28 | #include <ctime> |
---|
29 | #include <sstream> |
---|
30 | #include <stdexcept> |
---|
31 | #include <string> |
---|
32 | |
---|
33 | namespace theplu { |
---|
34 | namespace svndigest { |
---|
35 | |
---|
36 | Date::Date(void) |
---|
37 | { |
---|
38 | std::time(&time_); |
---|
39 | } |
---|
40 | |
---|
41 | |
---|
42 | Date::Date(const Date& other) |
---|
43 | : time_(other.time_) |
---|
44 | { |
---|
45 | } |
---|
46 | |
---|
47 | |
---|
48 | Date::Date(std::string str) |
---|
49 | { |
---|
50 | std::time(&time_); |
---|
51 | svntime(str); |
---|
52 | } |
---|
53 | |
---|
54 | |
---|
55 | std::string Date::difftime(const Date& other) const |
---|
56 | { |
---|
57 | std::stringstream ss; |
---|
58 | time_t t0 = std::min(seconds(), other.seconds()); |
---|
59 | time_t t1 = std::max(seconds(), other.seconds()); |
---|
60 | struct tm* last = std::gmtime(&t1); |
---|
61 | |
---|
62 | unsigned int year=0; |
---|
63 | while (t0<=t1) { |
---|
64 | ++year; |
---|
65 | --last->tm_year; |
---|
66 | t1=mktime(last); |
---|
67 | } |
---|
68 | --year; |
---|
69 | ++last->tm_year; |
---|
70 | t1=mktime(last); |
---|
71 | if (year) { |
---|
72 | ss << year << " year"; |
---|
73 | if (year>1) |
---|
74 | ss << "s"; |
---|
75 | ss << " "; |
---|
76 | } |
---|
77 | |
---|
78 | unsigned int month=0; |
---|
79 | while (t0<=t1) { |
---|
80 | ++month; |
---|
81 | --last->tm_mon; |
---|
82 | t1=mktime(last); |
---|
83 | } |
---|
84 | --month; |
---|
85 | ++last->tm_mon; |
---|
86 | t1=mktime(last); |
---|
87 | if (month || year) { |
---|
88 | ss << month << " month"; |
---|
89 | if (month>1 || (year && !month)) |
---|
90 | ss << "s"; |
---|
91 | ss << " and "; |
---|
92 | } |
---|
93 | |
---|
94 | |
---|
95 | unsigned int day = (t1-t0)/24/60/60; |
---|
96 | ss << day << " day"; |
---|
97 | if (day!=1) |
---|
98 | ss << "s"; |
---|
99 | ss << " "; |
---|
100 | |
---|
101 | return ss.str(); |
---|
102 | } |
---|
103 | |
---|
104 | |
---|
105 | void Date::svntime(std::string str) |
---|
106 | { |
---|
107 | std::stringstream sstream(str); |
---|
108 | struct tm* timeinfo = std::localtime(&time_); |
---|
109 | time_t timezone_correction = timeinfo->tm_gmtoff; |
---|
110 | |
---|
111 | std::string tmp; |
---|
112 | std::getline(sstream,tmp,'-'); |
---|
113 | using yat::utility::convert; |
---|
114 | timeinfo->tm_year = convert<unsigned int>(tmp) - 1900; |
---|
115 | |
---|
116 | std::getline(sstream,tmp,'-'); |
---|
117 | timeinfo->tm_mon = convert<unsigned int>(tmp) - 1; |
---|
118 | |
---|
119 | std::getline(sstream,tmp,'T'); |
---|
120 | timeinfo->tm_mday = convert<unsigned int>(tmp); |
---|
121 | |
---|
122 | std::getline(sstream,tmp,':'); |
---|
123 | timeinfo->tm_hour = convert<unsigned int>(tmp); |
---|
124 | |
---|
125 | std::getline(sstream,tmp,':'); |
---|
126 | timeinfo->tm_min = convert<unsigned int>(tmp); |
---|
127 | |
---|
128 | std::getline(sstream,tmp,'.'); |
---|
129 | timeinfo->tm_sec = convert<unsigned int>(tmp); |
---|
130 | |
---|
131 | time_ = mktime(timeinfo); |
---|
132 | time_ += timezone_correction; |
---|
133 | } |
---|
134 | |
---|
135 | |
---|
136 | std::string Date::operator()(std::string format) const |
---|
137 | { |
---|
138 | struct tm* timeinfo = std::gmtime(&time_); |
---|
139 | char buffer[80]; |
---|
140 | size_t res = std::strftime(buffer, 80, format.c_str(), timeinfo); |
---|
141 | if (!res) { |
---|
142 | throw std::runtime_error("svndigest::Date::operator() failed"); |
---|
143 | } |
---|
144 | return buffer; |
---|
145 | } |
---|
146 | |
---|
147 | |
---|
148 | const Date& Date::operator=(const Date& rhs) |
---|
149 | { |
---|
150 | time_ = rhs.time_; |
---|
151 | return *this; |
---|
152 | } |
---|
153 | |
---|
154 | |
---|
155 | }} |
---|