-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDateType.h
More file actions
87 lines (87 loc) · 2.88 KB
/
DateType.h
File metadata and controls
87 lines (87 loc) · 2.88 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
#include<iostream>
typedef int ll;
const ll days[14]={0,365,334,306,275,245,214,184,153,122,92,61,31,0};
struct Date{
Date():year(0),month(0),day(0){};
Date(ll y,ll m,ll d):
year(y),month(m),day(d){if(month>12||day>days[month]-days[month+1]+(month==2)*isleap())month=0,year=0,day=0;}
Date(const char*s){
ll pos[2]={0,0};
for(ll i=0,k=0;s[i]&&k<2;i++)if(s[i]=='-')pos[k++]=i;
if(!pos[0]||!pos[1]||pos[1]==pos[0]+1||pos[1]-pos[0]>3||strlen(s)-pos[1]>3||pos[0]>4)return;
for(ll i=0;i<pos[0];i++)year=year*10+s[i]-48;
for(ll i=pos[0]+1;i<pos[1];i++)month=month*10+s[i]-48;
for(ll i=pos[1]+1;s[i];i++)day=day*10+s[i]-48;
if(month>12||day>days[month]-days[month+1]+(month==2)*isleap())month=0,year=0,day=0;
}
ll year=0,month=0,day=0;
bool isleap(){return (!(year%4)&&year%100)||!(year%400);}
void print(){std::cout<<year<<'-'<<month<<'-'<<day<<std::endl;}
ll convert(){return year*365+year/4-year/100+year/400-days[month]-(month<=2)*isleap()+day;}
ll operator[](ll st){
if(st==0)return year;
if(st==1)return month;
if(st==2)return day;
return -1;
}
};
Date DISCONV(ll num){//turn a day count into Date object
Date t={0,12,31};
if(num<=0||num>3652059)return t;
if(num>=146097)t.year+=num/146097*400;
num%=146097;
while(num>0)t.year++,num-=(365+t.isleap());
while(num+days[t.month]<=0)t.month--;
t.day=num+days[t.month]+(t.month<=2)*t.isleap();
return t;
}
ll CONVERT(Date t){
return t.year*365+t.year/4-t.year/100+t.year/400-days[t.month]-(t.month<=2)*t.isleap()+t.day;
}
bool operator>(Date a,Date b){
if(a.year!=b.year)return a.year>b.year;
if(a.month!=b.month)return a.month>b.month;
return a.day>b.day;
}
bool operator<(Date a,Date b){
if(a.year!=b.year)return a.year<b.year;
if(a.month!=b.month)return a.month<b.month;
return a.day<b.day;
}
bool operator==(Date a,Date b){
return a.year==b.year&&a.month==b.month&&a.day==b.day;
}
bool operator!=(Date a,Date b){
return a.year!=b.year||a.month!=b.month||a.day!=b.day;
}
bool operator>=(Date a,Date b){
if(a.year!=b.year)return a.year>b.year;
if(a.month!=b.month)return a.month>b.month;
return a.day>=b.day;
}
bool operator<=(Date a,Date b){
if(a.year!=b.year)return a.year<b.year;
if(a.month!=b.month)return a.month<b.month;
return a.day<=b.day;
}
Date operator+(Date a,ll b){
return DISCONV(a.convert()+b);
}
Date operator-(Date a,ll b){
return DISCONV(a.convert()-b);
}
ll operator-(Date a,Date b){
return a.convert()-b.convert();
}
std::string transstr(long long t){
std::string tem="";
while(t)tem=(char)(t%10+48)+tem,t/=10;
return tem;
}
std::string str(Date t){
return transstr(t.year)+'-'+transstr(t.month)+'-'+transstr(t.day);
}
std::ostream& operator<<(std::ostream& ost,Date a){
ost<<a.year<<'-'<<a.month<<'-'<<a.day;
return ost;
}