/
TimeUtil.cpp
109 lines (100 loc) · 2.47 KB
/
TimeUtil.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
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
//
// Created by masker on 2021/11/9.
//
#include "TimeUtil.h"
TimeUtil::TimeUtil()
{
}
TimeUtil::~TimeUtil()
{
}
void TimeUtil::TimeToString(time_t time1, char *szTime)
{
struct tm *tm1;
tm1 = localtime(&time1);
sprintf(szTime, "%4.4d-%2.2d-%2.2d",
tm1->tm_year + 1900, tm1->tm_mon + 1, tm1->tm_mday);
}
//指定YYYYMMDDHH24MISS型的时间,格式化为time_t型的时间
time_t TimeUtil::StringToTime(char * szTime)
{
struct tm tm1{};
time_t time1 = NULL;
scanf(szTime, "%4d%2d%2d%2d%2d%2d",
&tm1.tm_year,
&tm1.tm_mon,
&tm1.tm_mday,
&tm1.tm_hour,
&tm1.tm_min,
&tm1.tm_sec);
tm1.tm_year -= 1900;
tm1.tm_mon--;
tm1.tm_isdst = -1;
time1 = mktime(&tm1);
return time1;
}
string TimeUtil::AddMonth(time_t &tTime)
{
struct tm *structTime;
structTime = localtime(&tTime);
switch (structTime->tm_mon + 1)
{
case 1:
if (structTime->tm_mday == 29)
{
//判断是否是润年
if ((structTime->tm_year + 1900) / 4 == 0 && (structTime->tm_year + 1900) / 100 != 0 || (structTime->tm_year + 1900) / 400 == 0)
{
structTime->tm_mon ++;
break;
}
else
{
structTime->tm_mon ++;
structTime->tm_mday = 28;
}
}
else if (structTime->tm_mday > 29)
{
structTime->tm_mon++;
structTime->tm_mday = 28;
}
else
{
structTime->tm_mon++;
}
break;
case 3:
case 5:
case 7:
case 8:
case 10:
if (structTime->tm_mday == 31)
{
structTime->tm_mon ++;
structTime->tm_mday = 30;
}
else
{
structTime->tm_mon ++;
}
break;
case 2:
case 4:
case 6:
case 9:
case 11:
structTime->tm_mon ++;
break;
case 12:
structTime->tm_year ++;
structTime->tm_mon = 1;
break;
default:
break;
}
char szTime[16] = {0};
sprintf(szTime, "%4.4d-%2.2d-%2.2d",
structTime->tm_year + 1900, structTime->tm_mon + 1, structTime->tm_mday);
return szTime;
}