-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathNow.hpp
41 lines (32 loc) · 855 Bytes
/
Now.hpp
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
#ifndef NOW_DOT_HPP
#define NOW_DOT_HPP
#include <cstring>
#include <string_view>
#include <sys/time.h>
#include <glog/logging.h>
class Now {
public:
Now();
auto sec() const { return tv_.tv_sec; }
auto usec() const { return tv_.tv_usec; }
const char* c_str() const { return c_str_; }
std::string_view as_string_view() const
{
return std::string_view{c_str(), strlen(c_str())};
}
private:
timeval tv_;
char c_str_[32]; // RFC 5322 date-time section 3.3.
friend std::ostream& operator<<(std::ostream& s, Now const& now)
{
return s << now.c_str_;
}
};
inline Now::Now()
{
PCHECK(gettimeofday(&tv_, nullptr) == 0);
tm* ptm = CHECK_NOTNULL(localtime(&tv_.tv_sec));
CHECK_EQ(strftime(c_str_, sizeof c_str_, "%a, %d %b %Y %H:%M:%S %z", ptm),
sizeof(c_str_) - 1);
}
#endif // NOW_DOT_HPP