-
Notifications
You must be signed in to change notification settings - Fork 58
/
waDateTime.cpp
245 lines (216 loc) · 6.12 KB
/
waDateTime.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/// \file waDateTime.cpp
/// webapp::DateTime类实现文件
#include <cstdio>
#include <cstring>
#include <climits>
#include "waDateTime.h"
using namespace std;
/// Web Application Library namaspace
namespace webapp {
/// 以当前时间设置对象
void DateTime::set() {
_time = ::time( 0 );
localtime_r( &_time, &_tm );
}
/// 以 time_t 参数设置对象
/// \param tt time_t类型参数
void DateTime::set( const time_t &tt ) {
time_t _tt = tt;
if ( tt < 0 ) _tt = 0;
if ( tt > LONG_MAX ) _tt = LONG_MAX;
_time = _tt;
localtime_r( &_time, &_tm );
}
/// 以指定时间设置对象
/// 若参数不是有效日期时间,则设置为系统初始时间(1970/1/1)
/// 若参数日期时间不存在,则设置为顺延有效时间(非闰年2/29视为3/1)
/// \param year 年
/// \param mon 月
/// \param mday 日
/// \param hour 时,默认为0
/// \param min 分,默认为0
/// \param src 秒,默认为0
void DateTime::set( const int year, const int mon, const int mday,
const int hour, const int min, const int sec )
{
int _year = year;
int _mon = mon;
int _mday = mday;
int _hour = hour;
int _min = min;
int _sec = sec;
// confirm
if ( _year<1 || _year>2038 ) _year = 1970;
if ( _mon<1 || _mon>12 ) _mon = 1;
if ( _mday<1 || _mday>31 ) _mday = 1;
if ( _hour<0 || _hour>23 ) _hour = 0;
if ( _min<0 || _min>59 ) _min = 0;
if ( _sec<0 || _sec>59 ) _sec = 0;
_tm.tm_year = _year-1900;
_tm.tm_mon = _mon-1;
_tm.tm_mday = _mday;
_tm.tm_hour = _hour;
_tm.tm_min = _min;
_tm.tm_sec = _sec;
_tm.tm_isdst = -1;
_time = mktime( &_tm );
}
/// 以 tm 结构参数设置对象
/// \param st struct tm类型参数
void DateTime::set( const tm &st ) {
this->set( st.tm_year 1900, st.tm_mon 1, st.tm_mday,
st.tm_hour, st.tm_min, st.tm_sec );
}
/// 以 DateTime 参数设置对象
/// \param date Date类型参数
void DateTime::set( const DateTime &date ) {
this->set( date.value() );
}
/// 以"YYYY-MM-DD HH:MM:SS"格式字符串设置对象
/// 若字符串格式错误或者时间值错误则设置为当前时间
/// \param datetime "YYYY-MM-DD HH:MM:SS"格式日期时间字符串
/// \param datemark 日期分隔字符,默认为"-"
/// \param dtmark 日期时间分隔字符,默认为" ",不能与datemark或timemark相同
/// \param timemark 时间分隔字符,默认为":"
void DateTime::set( const string &datetime, const string &datemark,
const string &dtmark, const string &timemark )
{
// init struct tm
struct tm tm;
tm.tm_isdst = -1;
// init format
string fmt;
if ( datetime.find(dtmark) != datetime.npos )
fmt = "%Y" datemark "%m" datemark "%d" dtmark
"%H" timemark "%M" timemark "%S";
else
fmt = "%Y" datemark "%m" datemark "%d";
// invoke strptime()
if ( strptime(datetime.c_str(),fmt.c_str(),&tm) != NULL )
this->set( tm );
else
this->set();
}
/// 输出日期字符串
/// \param datemark 日期分隔字符,默认为"-"
/// \param leadingzero 是否补充前置零,默认为是
/// \return 输出指定格式的日期字符串
string DateTime::date( const string &datemark, const bool leadingzero ) const {
char date_str[32];
if ( leadingzero )
snprintf( date_str, 32, "d%sd%sd",
this->year(), datemark.c_str(), this->month(), datemark.c_str(), this->m_day() );
else
snprintf( date_str, 32, "%d%s%d%s%d",
this->year(), datemark.c_str(), this->month(), datemark.c_str(), this->m_day() );
return string( date_str );
}
/// 输出时间字符串
/// \param timemark 时间分隔字符,默认为":"
/// \param leadingzero 是否补充前置零,默认为是
/// \return 输出指定格式的时间字符串
string DateTime::time( const string &timemark, const bool leadingzero ) const {
char time_str[32];
if ( leadingzero )
snprintf( time_str, 32, "d%sd%sd",
this->hour(), timemark.c_str(), this->min(), timemark.c_str(), this->sec() );
else
snprintf( time_str, 32, "%d%s%d%s%d",
this->hour(), timemark.c_str(), this->min(), timemark.c_str(), this->sec() );
return string( time_str );
}
/// 输出日期时间字符串
/// \param datemark 日期分隔字符,默认为"-"
/// \param dtmark 日期时间分隔字符,默认为" "
/// \param timemark 时间分隔字符,默认为":"
/// \param leadingzero 是否补充前置零,默认为是
/// \return 输出指定格式的日期时间字符串
string DateTime::datetime( const string &datemark, const string &dtmark,
const string &timemark, const bool leadingzero ) const
{
string datetime = this->date(datemark,leadingzero) dtmark
this->time(timemark,leadingzero);
return datetime;
}
/// 输出 GMT 格式日期时间字符串
/// 主要用于设置 cookie 有效期
/// \return GMT 格式日期时间字符串
string DateTime::gmt_datetime() const {
char gmt[50];
struct tm gmt_tm;
gmtime_r ( &_time, &gmt_tm );
strftime( gmt, 50, "%A,%d-%B-%Y %H:%M:%S GMT", &gmt_tm );
return string( gmt );
}
/// 赋值操作
DateTime& DateTime::operator=( const DateTime &date ) {
if ( this == &date ) return *this;
this->set( date );
return *this;
}
/// 赋值操作
DateTime& DateTime::operator=( const time_t &tt ) {
this->set( tt );
return *this;
}
/// 递增操作
DateTime& DateTime::operator =( const DateTime &date ) {
this->set( value() date.value() );
return *this;
}
/// 递增操作
DateTime& DateTime::operator =( const time_t &tt ) {
this->set( value() tt );
return *this;
}
/// 递减操作
DateTime& DateTime::operator-=( const DateTime &date ) {
this->set( value() - date.value() );
return *this;
}
/// 递减操作
DateTime& DateTime::operator-=( const time_t &tt ) {
this->set( value() - tt );
return *this;
}
/// 返回当月天数,范围1~31
int DateTime::m_days() const {
int m = this->month();
if ( m==1 || m==3 || m==5 || m==7 || m==8 || m==10 || m==12 ) {
return 31;
} else if ( m == 2 ) {
int leap = (this->year()) % 4;
if ( leap == 0 ) {
return 29;
} else {
return 28;
}
} else {
return 30;
}
}
/// 相加操作
DateTime operator ( const DateTime &date1, const DateTime &date2 ) {
DateTime newdate;
newdate.set( date1.value() date2.value() );
return newdate;
}
/// 相加操作
DateTime operator ( const DateTime &date, const time_t &tt ) {
DateTime newdate;
newdate.set( date.value() tt );
return newdate;
}
/// 相减操作
DateTime operator-( const DateTime &date1, const DateTime &date2 ) {
DateTime newdate;
newdate.set( date1.value() - date2.value() );
return newdate;
}
/// 相减操作
DateTime operator-( const DateTime &date, const time_t &tt ) {
DateTime newdate;
newdate.set( date.value() - tt );
return newdate;
}
} // namespace