-
Notifications
You must be signed in to change notification settings - Fork 1
/
Timestamp.cpp
56 lines (45 loc) · 1.07 KB
/
Timestamp.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
#include <time.h>
#include "Timestamp.h"
Timestamp::Timestamp()
{
secsinceepoch_=time(0); // 取系统当前时间。
}
Timestamp::Timestamp(int64_t secsinceepoch): secsinceepoch_(secsinceepoch)
{
}
// 当前时间。
Timestamp Timestamp::now()
{
return Timestamp(); // 返回当前时间。
}
time_t Timestamp::toint() const
{
return secsinceepoch_;
}
std::string Timestamp::tostring() const
{
char buf[32] = {0};
tm *tm_time = localtime(&secsinceepoch_);
snprintf(buf, 20, "%4d-%02d-%02d %02d:%02d:%02d",
tm_time->tm_year + 1900,
tm_time->tm_mon + 1,
tm_time->tm_mday,
tm_time->tm_hour,
tm_time->tm_min,
tm_time->tm_sec);
return buf;
}
/*
#include <unistd.h>
#include <iostream>
int main()
{
Timestamp ts;
std::cout << ts.toint() << std::endl;
std::cout << ts.tostring() << std::endl;
sleep(1);
std::cout << Timestamp::now().toint() << std::endl;
std::cout << Timestamp::now().tostring() << std::endl;
}
// g++ -o test Timestamp.cpp
*/