source: trunk/lib/Date.cc @ 1431

Last change on this file since 1431 was 1431, checked in by Peter Johansson, 9 years ago

prefer yat's convert function and avoid tmp variables

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