235 |
21 Apr 07 |
peter |
// $Id$ |
235 |
21 Apr 07 |
peter |
2 |
|
235 |
21 Apr 07 |
peter |
3 |
/* |
1635 |
30 Mar 23 |
peter |
Copyright (C) 2007 Peter Johansson |
1635 |
30 Mar 23 |
peter |
Copyright (C) 2008 Jari Häkkinen, Peter Johansson |
1635 |
30 Mar 23 |
peter |
Copyright (C) 2009, 2011, 2023 Peter Johansson |
235 |
21 Apr 07 |
peter |
7 |
|
687 |
04 Aug 08 |
peter |
This file is part of svndigest, http://dev.thep.lu.se/svndigest |
235 |
21 Apr 07 |
peter |
9 |
|
235 |
21 Apr 07 |
peter |
svndigest is free software; you can redistribute it and/or modify it |
235 |
21 Apr 07 |
peter |
under the terms of the GNU General Public License as published by |
693 |
11 Sep 08 |
jari |
the Free Software Foundation; either version 3 of the License, or |
235 |
21 Apr 07 |
peter |
(at your option) any later version. |
235 |
21 Apr 07 |
peter |
14 |
|
235 |
21 Apr 07 |
peter |
svndigest is distributed in the hope that it will be useful, but |
235 |
21 Apr 07 |
peter |
WITHOUT ANY WARRANTY; without even the implied warranty of |
235 |
21 Apr 07 |
peter |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
235 |
21 Apr 07 |
peter |
General Public License for more details. |
235 |
21 Apr 07 |
peter |
19 |
|
235 |
21 Apr 07 |
peter |
You should have received a copy of the GNU General Public License |
693 |
11 Sep 08 |
jari |
along with svndigest. If not, see <http://www.gnu.org/licenses/>. |
235 |
21 Apr 07 |
peter |
22 |
*/ |
235 |
21 Apr 07 |
peter |
23 |
|
1619 |
12 Mar 23 |
peter |
24 |
#include <config.h> |
1619 |
12 Mar 23 |
peter |
25 |
|
235 |
21 Apr 07 |
peter |
26 |
#include "Date.h" |
235 |
21 Apr 07 |
peter |
27 |
|
1431 |
17 Dec 11 |
peter |
28 |
#include "yat/utility.h" |
1431 |
17 Dec 11 |
peter |
29 |
|
235 |
21 Apr 07 |
peter |
30 |
#include <algorithm> |
751 |
14 Jan 09 |
peter |
31 |
#include <ctime> |
235 |
21 Apr 07 |
peter |
32 |
#include <sstream> |
754 |
14 Jan 09 |
peter |
33 |
#include <stdexcept> |
235 |
21 Apr 07 |
peter |
34 |
#include <string> |
235 |
21 Apr 07 |
peter |
35 |
|
235 |
21 Apr 07 |
peter |
36 |
namespace theplu { |
235 |
21 Apr 07 |
peter |
37 |
namespace svndigest { |
235 |
21 Apr 07 |
peter |
38 |
|
235 |
21 Apr 07 |
peter |
39 |
Date::Date(void) |
235 |
21 Apr 07 |
peter |
40 |
{ |
235 |
21 Apr 07 |
peter |
41 |
std::time(&time_); |
235 |
21 Apr 07 |
peter |
42 |
} |
235 |
21 Apr 07 |
peter |
43 |
|
235 |
21 Apr 07 |
peter |
44 |
|
235 |
21 Apr 07 |
peter |
45 |
Date::Date(const Date& other) |
235 |
21 Apr 07 |
peter |
46 |
: time_(other.time_) |
235 |
21 Apr 07 |
peter |
47 |
{ |
235 |
21 Apr 07 |
peter |
48 |
} |
235 |
21 Apr 07 |
peter |
49 |
|
235 |
21 Apr 07 |
peter |
50 |
|
235 |
21 Apr 07 |
peter |
51 |
Date::Date(std::string str) |
235 |
21 Apr 07 |
peter |
52 |
{ |
235 |
21 Apr 07 |
peter |
53 |
std::time(&time_); |
235 |
21 Apr 07 |
peter |
54 |
svntime(str); |
235 |
21 Apr 07 |
peter |
55 |
} |
235 |
21 Apr 07 |
peter |
56 |
|
235 |
21 Apr 07 |
peter |
57 |
|
235 |
21 Apr 07 |
peter |
58 |
std::string Date::difftime(const Date& other) const |
235 |
21 Apr 07 |
peter |
59 |
{ |
235 |
21 Apr 07 |
peter |
60 |
std::stringstream ss; |
235 |
21 Apr 07 |
peter |
61 |
time_t t0 = std::min(seconds(), other.seconds()); |
235 |
21 Apr 07 |
peter |
62 |
time_t t1 = std::max(seconds(), other.seconds()); |
235 |
21 Apr 07 |
peter |
63 |
struct tm* last = std::gmtime(&t1); |
235 |
21 Apr 07 |
peter |
64 |
|
589 |
12 Apr 08 |
jari |
65 |
unsigned int year=0; |
235 |
21 Apr 07 |
peter |
66 |
while (t0<=t1) { |
235 |
21 Apr 07 |
peter |
67 |
++year; |
235 |
21 Apr 07 |
peter |
68 |
--last->tm_year; |
235 |
21 Apr 07 |
peter |
69 |
t1=mktime(last); |
235 |
21 Apr 07 |
peter |
70 |
} |
235 |
21 Apr 07 |
peter |
71 |
--year; |
235 |
21 Apr 07 |
peter |
72 |
++last->tm_year; |
235 |
21 Apr 07 |
peter |
73 |
t1=mktime(last); |
235 |
21 Apr 07 |
peter |
74 |
if (year) { |
235 |
21 Apr 07 |
peter |
75 |
ss << year << " year"; |
235 |
21 Apr 07 |
peter |
76 |
if (year>1) |
235 |
21 Apr 07 |
peter |
77 |
ss << "s"; |
235 |
21 Apr 07 |
peter |
78 |
ss << " "; |
235 |
21 Apr 07 |
peter |
79 |
} |
235 |
21 Apr 07 |
peter |
80 |
|
589 |
12 Apr 08 |
jari |
81 |
unsigned int month=0; |
235 |
21 Apr 07 |
peter |
82 |
while (t0<=t1) { |
235 |
21 Apr 07 |
peter |
83 |
++month; |
235 |
21 Apr 07 |
peter |
84 |
--last->tm_mon; |
235 |
21 Apr 07 |
peter |
85 |
t1=mktime(last); |
235 |
21 Apr 07 |
peter |
86 |
} |
235 |
21 Apr 07 |
peter |
87 |
--month; |
235 |
21 Apr 07 |
peter |
88 |
++last->tm_mon; |
235 |
21 Apr 07 |
peter |
89 |
t1=mktime(last); |
235 |
21 Apr 07 |
peter |
90 |
if (month || year) { |
235 |
21 Apr 07 |
peter |
91 |
ss << month << " month"; |
235 |
21 Apr 07 |
peter |
92 |
if (month>1 || (year && !month)) |
235 |
21 Apr 07 |
peter |
93 |
ss << "s"; |
235 |
21 Apr 07 |
peter |
94 |
ss << " and "; |
235 |
21 Apr 07 |
peter |
95 |
} |
235 |
21 Apr 07 |
peter |
96 |
|
1431 |
17 Dec 11 |
peter |
97 |
|
589 |
12 Apr 08 |
jari |
98 |
unsigned int day = (t1-t0)/24/60/60; |
235 |
21 Apr 07 |
peter |
99 |
ss << day << " day"; |
235 |
21 Apr 07 |
peter |
100 |
if (day!=1) |
235 |
21 Apr 07 |
peter |
101 |
ss << "s"; |
235 |
21 Apr 07 |
peter |
102 |
ss << " "; |
235 |
21 Apr 07 |
peter |
103 |
|
235 |
21 Apr 07 |
peter |
104 |
return ss.str(); |
235 |
21 Apr 07 |
peter |
105 |
} |
235 |
21 Apr 07 |
peter |
106 |
|
235 |
21 Apr 07 |
peter |
107 |
|
235 |
21 Apr 07 |
peter |
108 |
void Date::svntime(std::string str) |
235 |
21 Apr 07 |
peter |
109 |
{ |
235 |
21 Apr 07 |
peter |
110 |
std::stringstream sstream(str); |
396 |
27 Jun 07 |
peter |
111 |
struct tm* timeinfo = std::localtime(&time_); |
504 |
27 Nov 07 |
peter |
112 |
time_t timezone_correction = timeinfo->tm_gmtoff; |
235 |
21 Apr 07 |
peter |
113 |
|
235 |
21 Apr 07 |
peter |
114 |
std::string tmp; |
235 |
21 Apr 07 |
peter |
115 |
std::getline(sstream,tmp,'-'); |
1431 |
17 Dec 11 |
peter |
116 |
using yat::utility::convert; |
1431 |
17 Dec 11 |
peter |
117 |
timeinfo->tm_year = convert<unsigned int>(tmp) - 1900; |
235 |
21 Apr 07 |
peter |
118 |
|
235 |
21 Apr 07 |
peter |
119 |
std::getline(sstream,tmp,'-'); |
1431 |
17 Dec 11 |
peter |
120 |
timeinfo->tm_mon = convert<unsigned int>(tmp) - 1; |
235 |
21 Apr 07 |
peter |
121 |
|
235 |
21 Apr 07 |
peter |
122 |
std::getline(sstream,tmp,'T'); |
1431 |
17 Dec 11 |
peter |
123 |
timeinfo->tm_mday = convert<unsigned int>(tmp); |
235 |
21 Apr 07 |
peter |
124 |
|
235 |
21 Apr 07 |
peter |
125 |
std::getline(sstream,tmp,':'); |
1431 |
17 Dec 11 |
peter |
126 |
timeinfo->tm_hour = convert<unsigned int>(tmp); |
235 |
21 Apr 07 |
peter |
127 |
|
235 |
21 Apr 07 |
peter |
128 |
std::getline(sstream,tmp,':'); |
1431 |
17 Dec 11 |
peter |
129 |
timeinfo->tm_min = convert<unsigned int>(tmp); |
235 |
21 Apr 07 |
peter |
130 |
|
235 |
21 Apr 07 |
peter |
131 |
std::getline(sstream,tmp,'.'); |
1431 |
17 Dec 11 |
peter |
132 |
timeinfo->tm_sec = convert<unsigned int>(tmp); |
235 |
21 Apr 07 |
peter |
133 |
|
235 |
21 Apr 07 |
peter |
134 |
time_ = mktime(timeinfo); |
503 |
27 Nov 07 |
peter |
135 |
time_ += timezone_correction; |
235 |
21 Apr 07 |
peter |
136 |
} |
235 |
21 Apr 07 |
peter |
137 |
|
235 |
21 Apr 07 |
peter |
138 |
|
235 |
21 Apr 07 |
peter |
139 |
std::string Date::operator()(std::string format) const |
235 |
21 Apr 07 |
peter |
140 |
{ |
235 |
21 Apr 07 |
peter |
141 |
struct tm* timeinfo = std::gmtime(&time_); |
1431 |
17 Dec 11 |
peter |
142 |
char buffer[80]; |
751 |
14 Jan 09 |
peter |
143 |
size_t res = std::strftime(buffer, 80, format.c_str(), timeinfo); |
753 |
14 Jan 09 |
peter |
144 |
if (!res) { |
753 |
14 Jan 09 |
peter |
145 |
throw std::runtime_error("svndigest::Date::operator() failed"); |
753 |
14 Jan 09 |
peter |
146 |
} |
751 |
14 Jan 09 |
peter |
147 |
return buffer; |
235 |
21 Apr 07 |
peter |
148 |
} |
235 |
21 Apr 07 |
peter |
149 |
|
235 |
21 Apr 07 |
peter |
150 |
|
1431 |
17 Dec 11 |
peter |
151 |
const Date& Date::operator=(const Date& rhs) |
235 |
21 Apr 07 |
peter |
152 |
{ |
235 |
21 Apr 07 |
peter |
153 |
time_ = rhs.time_; |
235 |
21 Apr 07 |
peter |
154 |
return *this; |
235 |
21 Apr 07 |
peter |
155 |
} |
235 |
21 Apr 07 |
peter |
156 |
|
235 |
21 Apr 07 |
peter |
157 |
|
235 |
21 Apr 07 |
peter |
158 |
}} |