-
Notifications
You must be signed in to change notification settings - Fork 4
/
Time.cpp
111 lines (86 loc) · 2.35 KB
/
Time.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
/*
* File: CTime.cpp
* Author: Administrator
*
* Created on 2014年1月26日, 下午4:49
*/
#include <bits/time.h>
#include <sys/time.h>
#include "Time.h"
namespace CommLib {
Time::Time(time_t tt) : tt_(tt) {
localtime_r(&tt, &tm_);
}
Time::Time() : tt_(0) {
}
unsigned long mktime(unsigned int year, unsigned int mon,
unsigned int day, unsigned int hour,
unsigned int min, unsigned int sec) {
if (0 >= (int) (mon -= 2)) { /* 1..12 -> 11,12,1..10 */
mon += 12; /* Puts Feb last since it has leap day */
year -= 1;
}
return (((
(unsigned long) (year / 4 - year / 100 + year / 400 + 367 * mon / 12 + day) +
year * 365 - 719499
)*24 + hour /* now have hours */
)*60 + min /* now have minutes */
)*60 + sec; /* finally seconds */
}
Time::Time(int nYear, int nMonth, int nDay, int nHour, int nMin, int nSec) {
tt_ = mktime(nYear, nMonth, nDay, nHour, nMin, nSec) + __timezone;
localtime_r(&tt_, &tm_);
}
Time::Time(const Time& orig) : tm_(orig.tm_), tt_(orig.tt_) {
}
Time::~Time() {
}
Time Time::GetCurrentTime() {
time_t _tt = time(NULL);
return Time(_tt);
}
long Time::GetCurrTick() {
timeval tm;
gettimeofday(&tm, NULL);
return ( tm.tv_sec * 1000000 + tm.tv_usec);
}
int Time::GetDay() {
return tm_.tm_yday + 1;
}
int Time::GetDayOfWeek() {
return tm_.tm_wday;
}
int Time::GetHour() {
return tm_.tm_hour;
}
int Time::GetMinute() {
return tm_.tm_min;
}
int Time::GetMonth() {
return tm_.tm_mon + 1;
}
int Time::GetSecond() {
return tm_.tm_sec;
}
time_t Time::GetTime() {
return tt_;
}
int Time::GetYear() {
return tm_.tm_year + 1900;
}
Time Time::operator +(TimeSpan &span) {
return Time(tt_ + span.GetTimeSpan());
}
Time Time::operator -(TimeSpan &span) {
return Time(tt_ - span.GetTimeSpan());
}
TimeSpan Time::operator -(Time &tm) {
return TimeSpan(tt_ - tm.tt_);
}
Time Time::operator +(time_t tt) {
return Time(tt_ + tt);
}
Time Time::operator -(time_t tt) {
return Time(tt_ - tt);
}
}