This repository has been archived by the owner on Feb 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathdate.h
executable file
·129 lines (106 loc) · 2.98 KB
/
date.h
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
#ifndef _DATE_H
#define _DATE_H
#include <string>
#include <cstdio>
#include <ctime>
using namespace std;
typedef enum { YEAR, MONTH, DAY, HOUR, MINUTE, SECOND, WEEK } TimeUnit;
class Date {
private:
const int Compare(const Date &OtherDate, TimeUnit FromPart = YEAR) const {
for (;FromPart <= SECOND; FromPart = (TimeUnit)(FromPart+1)) {
if (Data[FromPart] < OtherDate.Data[FromPart])
return -1;
else if (Data[FromPart] > OtherDate.Data[FromPart])
return 1;
}
return 0;
};
short Data[6];
class DatePart;
public:
Date() {
Clear();
}
bool IsLeapYear(short Year) const {
return ((Year%4 == 0 && Year%100 != 0) || Year%400 == 0);
}
short DaysInMonth(short Month = 0) const {
switch (Month == 0 ? Data[MONTH] : Month) {
case 1: return 31;
case 2: return IsLeapYear(Data[YEAR]) ? 29 : 28;
case 3: return 31;
case 4: return 30;
case 5: return 31;
case 6: return 30;
case 7: return 31;
case 8: return 31;
case 9: return 30;
case 10: return 31;
case 11: return 30;
case 12: return 31;
}
return 0;
}
string Format() const;
operator string() const;
Date &operator =(const string &Text);
friend ostream &operator <<(ostream &stream, const Date &Date) {
stream << Date.operator string();
return stream;
}
bool operator <=(const Date &OtherDate) const { return (Compare(OtherDate) <= 0); }
bool operator >=(const Date &OtherDate) const { return (Compare(OtherDate) >= 0); }
bool operator <(const Date &OtherDate) const { return (Compare(OtherDate) < 0); }
bool operator >(const Date &OtherDate) const { return (Compare(OtherDate) > 0); }
bool operator ==(const Date &OtherDate) const { return (Compare(OtherDate) == 0); }
DatePart operator [](TimeUnit PartName);
unsigned long Difference(Date &OtherDate, TimeUnit Unit, bool RoundUp = true) const;
void SetToNow();
bool IsEmpty() const { return (Data[YEAR] == 0); }
void Clear(bool OnlyTime = false) {
if (!OnlyTime)
Data[YEAR] = Data[MONTH] = Data[DAY] = 0;
Data[HOUR] = Data[MINUTE] = Data[SECOND] = 0;
WithTime = false;
}
bool WithTime;
};
class Date::DatePart {
private:
Date &BaseDate;
TimeUnit Part;
public:
DatePart(Date &BaseDate, TimeUnit Part): BaseDate(BaseDate), Part(Part) {}
operator short() const {
if (Part <= SECOND)
return BaseDate.Data[Part];
else if (Part == WEEK) {
// actually, this case is unused
short Value = 0;
for (char i=1; i<BaseDate.Data[MONTH]; ++i)
Value += BaseDate.DaysInMonth(i);
Value += BaseDate.Data[DAY];
return (Value-1)/7 + 1;
}
return 0;
}
DatePart &operator =(short Value) {
if (Part <= SECOND)
BaseDate.Data[Part] = Value;
return *this;
}
DatePart &operator =(const DatePart &DatePart) {
return operator =((short)DatePart);
}
DatePart &operator +=(short Value);
DatePart &operator -=(short Value);
DatePart &operator ++() {
operator +=(1);
return *this;
}
};
inline Date::DatePart Date::operator [](TimeUnit Part) {
return DatePart(*this, Part);
}
#endif // _DATE_H