-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathJvTime.cpp
125 lines (110 loc) · 3.32 KB
/
JvTime.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
#include "JvTime.h"
#include "string.h"
JvTime * getNowJvTime
(void)
{
time_t ticks;
ticks = time(NULL);
struct std::tm * my_tm_ptr = gmtime(&ticks);
JvTime * jvT_ptr = new JvTime();
int rc = jvT_ptr->setStdTM(my_tm_ptr);
if (rc != 0)
{
std::cout << "error: failed to set time!" << std::endl;
delete jvT_ptr;
return NULL;
}
return jvT_ptr;
}
JvTime::JvTime
(const char *time_str)
{
const char zero_str[] = "0000-00-00T00:00:00+0000";
this->good = true;
if ((time_str == NULL) || (strlen(time_str) != strlen(zero_str)))
{
this->good = false;
}
if (((time_str[0] < '0') || (time_str[0] > '9')) ||
((time_str[1] < '0') || (time_str[1] > '9')) ||
((time_str[2] < '0') || (time_str[2] > '9')) ||
((time_str[3] < '0') || (time_str[3] > '9')) ||
(time_str[4] != '-') ||
((time_str[5] < '0') || (time_str[5] > '9')) ||
((time_str[6] < '0') || (time_str[6] > '9')) ||
(time_str[7] != '-') ||
((time_str[8] < '0') || (time_str[8] > '9')) ||
((time_str[9] < '0') || (time_str[9] > '9')) ||
(time_str[10] != 'T') ||
((time_str[11] < '0') || (time_str[11] > '9')) ||
((time_str[12] < '0') || (time_str[12] > '9')) ||
(time_str[13] != ':') ||
((time_str[14] < '0') || (time_str[14] > '9')) ||
((time_str[15] < '0') || (time_str[15] > '9')) ||
(time_str[16] != ':') ||
((time_str[17] < '0') || (time_str[17] > '9')) ||
((time_str[18] < '0') || (time_str[18] > '9')) ||
(time_str[19] != '+') ||
((time_str[20] < '0') || (time_str[20] > '9')) ||
((time_str[21] < '0') || (time_str[21] > '9')) ||
((time_str[22] < '0') || (time_str[22] > '9')) ||
((time_str[23] < '0') || (time_str[23] > '9')))
{
this->good = false;
}
if (this->good == true)
{
sscanf(time_str, "%4d-%2d-%2dT%2d:%2d:%2d+%4s",
&(this->year), &(this->month), &(this->day),
&(this->hour), &(this->minute), &(this->second),
this->tail4);
}
else
{
sscanf(zero_str, "%4d-%2d-%2dT%2d:%2d:%2d+%4s",
&(this->year), &(this->month), &(this->day),
&(this->hour), &(this->minute), &(this->second),
this->tail4);
}
return;
}
struct std::tm *
JvTime::getStdTM
(void)
{
struct std::tm * result = (struct std::tm *) malloc(sizeof(struct std::tm));
bzero(result, sizeof(struct std::tm));
result->tm_sec = this->second;
result->tm_min = this->minute;
result->tm_hour = this->hour;
result->tm_mday = this->day;
result->tm_mon = (this->month) - 1;
result->tm_year = (this->year) - 1900;
return result;
}
int
JvTime::setStdTM
(struct std::tm *arg_tm_ptr)
{
if (arg_tm_ptr == NULL) return -1;
this->second = arg_tm_ptr->tm_sec;
this->minute = arg_tm_ptr->tm_min;
this->hour = arg_tm_ptr->tm_hour;
this->day = arg_tm_ptr->tm_mday;
this->month = (arg_tm_ptr->tm_mon) + 1;
this->year = (arg_tm_ptr->tm_year) + 1900;
bzero(this->tail4, 16);
snprintf(this->tail4, strlen("0000") + 1, "0000");
return 0;
}
std::string *
JvTime::getTimeString
(void)
{
struct std::tm * tm_ptr = this->getStdTM();
char buffer[128];
bzero(buffer, 128);
std::strftime(buffer, 32, "%Y-%m-%dT%H:%M:%S+", tm_ptr);
snprintf(buffer, strlen(buffer) + 4 + 1, "%s%s", buffer, this->tail4);
return (new std::string(buffer));
}