-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdate.cpp
More file actions
175 lines (175 loc) · 4.23 KB
/
date.cpp
File metadata and controls
175 lines (175 loc) · 4.23 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include <iostream>
#include "string.cpp"
namespace sjtu
{
const int days[13]={0,31,59,90,120,151,181,212,243,273,304,334,365};
struct DATE
{
int month,day;
DATE(){month=day=0;}
DATE(int month,int day): month(month),day(day) {}
DATE(const std::string &s)
{
month=(s[0]-'0')*10+s[1]-'0';
day=(s[3]-'0')*10+s[4]-'0';
}
template<int M>
DATE(const string<M> &s)
{
month=(s[0]-'0')*10+s[1]-'0';
day=(s[3]-'0')*10+s[4]-'0';
}
bool operator < (const DATE &a) const
{
if(month!=a.month) return month<a.month;
return day<a.day;
}
bool operator == (const DATE &a) const
{
return month==a.month&&day==a.day;
}
int operator - (const DATE &a) const
{
return (days[month-1]+day-days[a.month-1]-a.day)*24*60;
}
DATE& operator ++ (int)
{
int now=days[month]-days[month-1];
if(day==now) month=month%12+1,day=0;
day++;return *this;
}
DATE& operator -- (int)
{
int now=(month==1?31:days[month-1]-days[month-2]);
if(day==1) {month--;if(month==0) month=12;day=now+1;}
day--;return *this;
}
DATE& operator += (const int &a)
{
day+=a;
int now=days[month]-days[month-1];
while(now<day)
{
month=month%12+1,day-=now;
now=days[month]-days[month-1];
}
now=(month==1?31:days[month-1]-days[month-2]);
while(day<=0)
{
month--;if(month==0) month=12;
day+=now;
now=(month==1?31:days[month-1]-days[month-2]);
}
return *this;
}
friend std::ostream& operator << (std::ostream &s, const DATE &a);
};
std::ostream& operator << (std::ostream &s, const DATE &a)
{
if(a.month<10) s<<"0";
s<<a.month<<"-";
if(a.day<10) s<<"0";
s<<a.day;
return s;
}
struct TIME
{
int hour,minute;
TIME(){hour=minute=0;}
TIME(int hour,int minute): hour(hour),minute(minute) {}
TIME(const std::string &s)
{
hour=(s[0]-'0')*10+s[1]-'0';
minute=(s[3]-'0')*10+s[4]-'0';
}
template<int M>
TIME(const string<M> &s)
{
hour=(s[0]-'0')*10+s[1]-'0';
minute=(s[3]-'0')*10+s[4]-'0';
}
bool operator < (const TIME &a) const
{
if(hour!=a.hour) return hour<a.hour;
return minute<a.minute;
}
bool operator == (const TIME &a) const
{
return hour==a.hour&&minute==a.minute;
}
int operator - (const TIME &a) const
{
return (hour-a.hour)*60+minute-a.minute;
}
TIME operator + (const int &a)
{
TIME ret(*this);
ret.minute+=a;
ret.hour+=ret.minute/60;
ret.minute%=60;
if(minute<0) ret.minute+=60,ret.hour--;
return ret;
}
TIME& operator += (const int &a)
{
minute+=a;
hour+=minute/60;
minute%=60;
if(minute<0) minute+=60,hour--;
return *this;
}
friend std::ostream& operator << (std::ostream &s, const TIME &a);
};
std::ostream& operator << (std::ostream &s, const TIME &a)
{
if(a.hour<10) s<<"0";
s<<a.hour<<":";
if(a.minute<10) s<<"0";
s<<a.minute;
return s;
}
struct DATE_TIME
{
DATE date;
TIME time;
DATE_TIME(){}
DATE_TIME(DATE dt,TIME tm): date(dt),time(tm)
{
date+=time.hour/24;
time.hour%=24;
if(time.hour<0) time.hour+=24,date--;
}
DATE_TIME operator + (const int &c) const
{
DATE_TIME ret=*this;
ret.time+=c;
ret.date+=ret.time.hour/24;
ret.time.hour%=24;
if(ret.time.hour<0) ret.time.hour+=24,ret.date--;
return ret;
}
int operator - (const DATE_TIME &a) const
{
return (date-a.date)+(time-a.time);
}
DATE_TIME operator += (const int &c)
{
time+=c;
date+=time.hour/24;
time.hour%=24;
if(time.hour<0) time.hour+=24,date--;
return *this;
}
bool operator < (const DATE_TIME &a) const
{
if(date==a.date) return time<a.time;
return date<a.date;
}
friend std::ostream& operator << (std::ostream &s, const DATE_TIME &a);
};
std::ostream& operator << (std::ostream &s, const DATE_TIME &a)
{
s<<a.date<<" "<<a.time;
return s;
}
}