-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtime.hpp
110 lines (97 loc) · 1.97 KB
/
time.hpp
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
#ifndef TIME_HPP
#define TIME_HPP
#include "string.hpp"
#include <string>
using namespace std;
struct mytime
{
int hour, minute, date;
mytime()
{
date = hour = minute = 0;
}
void set(string s)
{
if (s[0] == 'X' || s[0] == 'x') {
date = hour = minute = 0;
return;
}
date = 0;
hour = (s[0] - '0') * 10 + s[1] - '0';
minute = (s[3] - '0') * 10 + s[4] - '0';
}
bool operator<(const mytime &other)
{
int x = date * 1440 + hour * 60 + minute, y = other.date * 1440 + other.hour * 60 + other.minute;
return x < y;
}
mytime &operator = (const mytime &other)
{
date = other.date;
hour = other.hour;
minute = other.minute;
return *this;
}
int operator-(const mytime &other)
{
int x = date * 1440 + hour * 60 + minute, y = other.date * 1440 + other.hour * 60 + other.minute;
return x - y;
}
};
class mydate
{
private:
int year, month, day;
public:
mydate()
{
year = month = day = 0;
}
void set(string s)
{
if (s[0] == 'X' || s[0] == 'x') {
year = month = day = 0;
return;
}
year = (s[0] - '0') * 1000 + (s[1] - '0') * 100 + (s[2] - '0') * 10 + s[3] - '0';
month = (s[5] - '0') * 10 + s[6] - '0';
day = (s[8] - '0') * 10 + s[9] - '0';
}
mydate operator++()
{
if (day == 30 && (month == 4 || month == 6 || month == 9 || month == 11)) {
++month;
day = 1;
}
else if (day == 31 && (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10)) {
++month;
day = 1;
}
else if (year % 4 == 0 && month == 2 && day == 29) {
++month;
day = 1;
}
else if (year % 4 != 0 && month == 2 && day == 28) {
++month;
day = 1;
}
else
++day;
return *this;
}
string tos()
{
string str;
str += to_string(year);
str += '-';
if (month < 10)
str += '0';
str += to_string(month);
str += '-';
if (day < 10)
str += '0';
str += to_string(day);
return str;
}
};
#endif