-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDate.cpp
More file actions
executable file
·162 lines (145 loc) · 3.67 KB
/
Copy pathDate.cpp
File metadata and controls
executable file
·162 lines (145 loc) · 3.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/***************************************************************************
* Copyright (C) 2003 by eddiedu *
* eddiedu@scale.com.br *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
***************************************************************************/
#include "Date.h"
#include <string>
#include <algorithm> // for swap()
#include <ctime>
#include <cassert>
#include <sstream>
#include <iomanip>
using namespace std;
namespace {
const int daysInMonth[][13] = {
{0,31,28,31,30,31,30,31,31,30,31,30,31},
{0,31,29,31,30,31,30,31,31,30,31,30,31}};
inline bool isleap(int y) {
return ((y%4 == 0) && (y%100 != 0)) || (y%400 == 0);
}
}
Date::Date() {
// Get current date
time_t tval = time(0);
struct tm *now = localtime(&tval);
year = now->tm_year + 1900;
month = now->tm_mon + 1;
day = now->tm_mday;
}
Date::Date(int yr, int mon, int dy) {
assert(1 <= mon && mon <= 12);
assert(1 <= dy &&
dy <= daysInMonth[isleap(year)][mon]);
year = yr;
month = mon;
day = dy;
}
Date::Date(const std::string& s) {
// Assume YYYYMMDD format
istringstream is(s);
is >> setw(4) >> year;
is >> setw(2) >> month;
is >> setw(2) >> day;
}
int Date::getYear() const {
return year;
}
int Date::getMonth() const {
return month;
}
int Date::getDay() const {
return day;
}
string Date::toString() const {
ostringstream os;
os << setw(4) << year
<< setw(2) << month
<< setw(2) << day;
return os.str();
}
int Date::compare(const Date& d2) const {
int result = year - d2.year;
if (result == 0) {
result = month - d2.month;
if (result == 0)
result = day - d2.day;
}
return result;
}
int Date::daysInPrevMonth(int year, int month) {
if (month == 1) {
--year;
month = 12;
}
else
--month;
return daysInMonth[isleap(year)][month];
}
bool operator<(const Date& d1, const Date& d2) {
return d1.compare(d2) < 0;
}
bool operator<=(const Date& d1, const Date& d2) {
return d1.compare(d2) <= 0;
}
bool operator>(const Date& d1, const Date& d2) {
return d1.compare(d2) >= 0;
}
bool operator>=(const Date& d1, const Date& d2) {
return d1.compare(d2) >= 0;
}
bool operator==(const Date& d1, const Date& d2) {
return d1.compare(d2) == 0;
}
bool operator!=(const Date& d1, const Date& d2) {
return d1.compare(d2) != 0;
}
Duration
duration(const Date& date1, const Date& date2) {
int y1 = date1.year;
int y2 = date2.year;
int m1 = date1.month;
int m2 = date2.month;
int d1 = date1.day;
int d2 = date2.day;
// Compute the compare
int order = date1.compare(date2);
if (order == 0)
return Duration(0,0,0);
else if (order > 0) {
// Make date1 precede date2 locally
using std::swap;
swap(y1, y2);
swap(m1, m2);
swap(d1, d2);
}
int years = y2 - y1;
int months = m2 - m1;
int days = d2 - d1;
assert(years > 0 ||
(years == 0 && months > 0) ||
(years == 0 && months == 0 && days > 0) );
// Do the obvious corrections (must adjust days
// before months!) - This is a loop in case the
// previous month is February, and days < -28.
int lastMonth = m2;
int lastYear = y2;
while (days < 0) {
// Borrow from month
assert(months > 0);
days += Date::daysInPrevMonth(
lastYear, lastMonth--);
--months;
}
if (months < 0) {
// Borrow from year
assert(years > 0);
months += 12;
--years;
}
return Duration(years, months, days);
}