-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNow.cpp
115 lines (97 loc) · 1.7 KB
/
Now.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
#include "Now.h"
#include <stdio.h>
#include <string.h>
Now::Now():
dbg("Now")
{
updateTime();
srandom( currentTv.tv_usec );
}
Now::~Now()
{
}
void Now::updateTime()
{
memcpy( &lastTs, ¤tTs, sizeof( struct tm ));
lastTv.tv_sec = currentTv.tv_sec;
lastTv.tv_usec = currentTv.tv_usec;
gettimeofday( ¤tTv, &tz );
localtime_r( ¤tTv.tv_sec, ¤tTs );
}
time_t Now::unixtime()
{
return currentTv.tv_sec;
}
size_t Now::RFC2822( char *s, size_t max )
{
return strftime( s, max, "%a, %d %b %Y %H:%M:%S %z", ¤tTs );
}
size_t Now::RFC1123( char *s, size_t max )
{
return strftime( s, max, "%a, %d %b %Y %T %Z", ¤tTs );
}
size_t Now::mySQL( char *s, size_t max )
{
return strftime( s, max, "%Y-%m-%d %H:%M:%S", ¤tTs );
}
float Now::timeSinceLast()
{
struct timeval tv;
struct timezone tz;
gettimeofday( &tv, &tz );
float rval = (tv.tv_sec - currentTv.tv_sec) + ( tv.tv_usec - currentTv.tv_usec )/1000000.0;
return rval;
}
bool Now::isNewMinute( )
{
if ( lastTs.tm_min != currentTs.tm_min )
{
return true;
}
else
{
return false;
}
}
bool Now::isNewHour( )
{
if ( lastTs.tm_hour != currentTs.tm_hour )
{
return true;
}
else
{
return false;
}
}
bool Now::isNewDay( )
{
if ( lastTs.tm_mday != currentTs.tm_mday )
{
return true;
}
else
{
return false;
}
}
int Now::getMinute()
{
return currentTs.tm_min;
}
int Now::getHour()
{
return currentTs.tm_hour;
}
int Now::getDayOfYear()
{
return currentTs.tm_yday+1;
}
int Now::isDST()
{
return currentTs.tm_isdst;
}
double Now::getDecimalTime()
{
return (currentTs.tm_hour + currentTs.tm_min / 60.0);
}