source: trunk/lib/Date.cc @ 815

Last change on this file since 815 was 768, checked in by Peter Johansson, 13 years ago

Merged patch release 0.6.7 to trunk. Delta 0.6.7 - 0.6.6

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.2 KB
Line 
1// $Id: Date.cc 768 2009-01-31 21:30:37Z peter $
2
3/*
4  Copyright (C) 2007 Peter Johansson
5  Copyright (C) 2008 Jari Häkkinen, Peter Johansson
6  Copyright (C) 2009 Peter Johansson
7
8  This file is part of svndigest, http://dev.thep.lu.se/svndigest
9
10  svndigest is free software; you can redistribute it and/or modify it
11  under the terms of the GNU General Public License as published by
12  the Free Software Foundation; either version 3 of the License, or
13  (at your option) any later version.
14
15  svndigest is distributed in the hope that it will be useful, but
16  WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  General Public License for more details.
19
20  You should have received a copy of the GNU General Public License
21  along with svndigest. If not, see <http://www.gnu.org/licenses/>.
22*/
23
24#include "Date.h"
25
26#include <algorithm>
27#include <cassert>
28#include <ctime>
29#include <sstream>
30#include <stdexcept>
31#include <string>
32
33namespace theplu {
34namespace 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    unsigned int year, month, day, hour, minute, second;
112    std::string tmp;
113    std::getline(sstream,tmp,'-');
114    year=atoi(tmp.c_str());
115    timeinfo->tm_year = year - 1900;
116
117    std::getline(sstream,tmp,'-');
118    month=atoi(tmp.c_str());
119    timeinfo->tm_mon = month - 1;
120
121    std::getline(sstream,tmp,'T');
122    day=atoi(tmp.c_str());
123    timeinfo->tm_mday = day;
124
125    std::getline(sstream,tmp,':');
126    hour=atoi(tmp.c_str());
127    timeinfo->tm_hour = hour;
128
129    std::getline(sstream,tmp,':');
130    minute=atoi(tmp.c_str());
131    timeinfo->tm_min = minute;
132
133    std::getline(sstream,tmp,'.');
134    second=atoi(tmp.c_str());
135    timeinfo->tm_sec = second;
136
137    time_ = mktime(timeinfo);
138    time_ += timezone_correction;
139  }
140
141
142  std::string Date::operator()(std::string format) const
143  {
144    std::stringstream ss;
145    struct tm* timeinfo = std::gmtime(&time_);
146    char buffer[80]; 
147    size_t res = std::strftime(buffer, 80, format.c_str(), timeinfo);
148    if (!res) {
149      throw std::runtime_error("svndigest::Date::operator() failed");
150    }
151    return buffer;
152  }
153
154
155  const Date& Date::operator=(const Date& rhs) 
156  {
157    time_ = rhs.time_;
158    return *this;
159  }
160
161
162}}
Note: See TracBrowser for help on using the repository browser.