-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimestamp.cpp
More file actions
28 lines (25 loc) · 856 Bytes
/
Copy pathTimestamp.cpp
File metadata and controls
28 lines (25 loc) · 856 Bytes
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
#include "Timestamp.h"
#include <sys/time.h>
#include <stdio.h>
Timestamp::Timestamp(int64_t microSecondsSinceEpoch)
: microSecondsSinceEpoch_(microSecondsSinceEpoch) {}
Timestamp Timestamp::now() {
timeval tv;
gettimeofday(&tv, nullptr);
int64_t microSecondsSinceEpoch = tv.tv_sec * kMicroSecondsPerSecond + tv.tv_usec;
return Timestamp(microSecondsSinceEpoch);
}
std::string Timestamp::toString() const {
char buf[64] = {0};
time_t seconds = static_cast<time_t>(microSecondsSinceEpoch_ / kMicroSecondsPerSecond);
tm tm_time;
gmtime_r(&seconds, &tm_time);
snprintf(buf, 64, "%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;
}