forked from Dashark/hello-world
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatetime-1.cpp
61 lines (58 loc) · 1.23 KB
/
datetime-1.cpp
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
#include <iostream>
using namespace std;
class DateTime {
private:
int seconds;
public:
DateTime();
~DateTime();
void showTime();
void showSeconds();
int year,month,day,hour,minute,second;
};
int main() {
DateTime dt;
dt.showTime();
dt.showSeconds();
return 0;
}
DateTime::DateTime()
{
year = 2020; month = 3; day = 20;
hour = 11; minute = 27; second = 55;
}
DateTime::~DateTime()
{
cout << " Go die, Ha~Ha~" << endl;
}
void DateTime::showTime()
{
printf("当前时间%d/%d/%d %d:%d:%d\n", year, month, day, hour, minute, second);
}
void DateTime::showSeconds(){
int days=0;
for (int i=1970;i<year;i++){
if ((i%4) == 0) {
days+=366;
}
else days+=365;
}
for (int j=1;j<month;j++){
if (j==1||j==3||j==5||j==7||j==8||j==10||j==12){
days+=31;
}
else if (j==4||j==6||j==9||j==11){
days+=30;
}
else if (j==2 && (year%4) == 0){
days+=29;
}
else {
days+=28;
}
days += day;
seconds = day * 24 *3600;
seconds = seconds + hour*3600 + minute*60 + second;
}
cout << "距离1970.1.1 "<< seconds << "秒" << endl;
}