source: trunk/lib/Date.cc @ 978

Last change on this file since 978 was 978, checked in by Peter Johansson, 11 years ago

refs #429. Convert copyright statements to UTF-8

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.2 KB
Line 
1// $Id: Date.cc 978 2009-12-12 20:09:41Z 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 <algorithm>
26#include <cassert>
27#include <ctime>
28#include <sstream>
29#include <stdexcept>
30#include <string>
31
32namespace theplu {
33namespace svndigest {
34
35  Date::Date(void)
36  {
37    std::time(&time_);
38  }
39
40
41  Date::Date(const Date& other)
42    : time_(other.time_)
43  {
44  }
45
46
47  Date::Date(std::string str)
48  {
49    std::time(&time_);
50    svntime(str);
51  }
52
53
54  std::string Date::difftime(const Date& other) const
55  {
56    std::stringstream ss;
57    time_t t0 = std::min(seconds(), other.seconds());
58    time_t t1 = std::max(seconds(), other.seconds());
59    struct tm* last = std::gmtime(&t1);
60
61    unsigned int year=0;
62    while (t0<=t1) {
63      ++year;
64      --last->tm_year;
65      t1=mktime(last);
66    }
67    --year;
68    ++last->tm_year;
69    t1=mktime(last);
70    if (year) {
71      ss << year << " year";
72      if (year>1)
73        ss << "s";
74      ss << " ";
75    }
76
77    unsigned int month=0;
78    while (t0<=t1) {
79      ++month;
80      --last->tm_mon;
81      t1=mktime(last);
82    }
83    --month;
84    ++last->tm_mon;
85    t1=mktime(last);
86    if (month || year) {
87      ss << month << " month";
88      if (month>1 || (year && !month))
89        ss << "s";
90      ss << " and ";
91    }
92   
93
94    unsigned int day = (t1-t0)/24/60/60;
95    ss << day << " day";
96    if (day!=1)
97      ss << "s";
98    ss << " ";
99
100    return ss.str();
101  }
102
103
104  void Date::svntime(std::string str)
105  {
106    std::stringstream sstream(str);
107    struct tm* timeinfo = std::localtime(&time_);
108    time_t timezone_correction = timeinfo->tm_gmtoff;
109
110    unsigned int year, month, day, hour, minute, second;
111    std::string tmp;
112    std::getline(sstream,tmp,'-');
113    year=atoi(tmp.c_str());
114    timeinfo->tm_year = year - 1900;
115
116    std::getline(sstream,tmp,'-');
117    month=atoi(tmp.c_str());
118    timeinfo->tm_mon = month - 1;
119
120    std::getline(sstream,tmp,'T');
121    day=atoi(tmp.c_str());
122    timeinfo->tm_mday = day;
123
124    std::getline(sstream,tmp,':');
125    hour=atoi(tmp.c_str());
126    timeinfo->tm_hour = hour;
127
128    std::getline(sstream,tmp,':');
129    minute=atoi(tmp.c_str());
130    timeinfo->tm_min = minute;
131
132    std::getline(sstream,tmp,'.');
133    second=atoi(tmp.c_str());
134    timeinfo->tm_sec = second;
135
136    time_ = mktime(timeinfo);
137    time_ += timezone_correction;
138  }
139
140
141  std::string Date::operator()(std::string format) const
142  {
143    std::stringstream ss;
144    struct tm* timeinfo = std::gmtime(&time_);
145    char buffer[80]; 
146    size_t res = std::strftime(buffer, 80, format.c_str(), timeinfo);
147    if (!res) {
148      throw std::runtime_error("svndigest::Date::operator() failed");
149    }
150    return buffer;
151  }
152
153
154  const Date& Date::operator=(const Date& rhs) 
155  {
156    time_ = rhs.time_;
157    return *this;
158  }
159
160
161}}
Note: See TracBrowser for help on using the repository browser.