better with obs forms
[ghsmtp.git] / Now.hpp
blobbebb388491b80eb0834241cdc0f05e2485066d7d
1 #ifndef NOW_DOT_HPP
2 #define NOW_DOT_HPP
4 #include <cstring>
5 #include <string_view>
6 #include <sys/time.h>
8 #include <glog/logging.h>
10 class Now {
11 public:
12 Now();
14 auto sec() const { return tv_.tv_sec; }
15 auto usec() const { return tv_.tv_usec; }
16 const char* c_str() const { return c_str_; }
18 std::string_view as_string_view() const
20 return std::string_view{c_str(), strlen(c_str())};
23 private:
24 timeval tv_;
25 char c_str_[32]; // RFC 5322 date-time section 3.3.
27 friend std::ostream& operator<<(std::ostream& s, Now const& now)
29 return s << now.c_str_;
33 inline Now::Now()
35 PCHECK(gettimeofday(&tv_, nullptr) == 0);
36 tm* ptm = CHECK_NOTNULL(localtime(&tv_.tv_sec));
37 CHECK_EQ(strftime(c_str_, sizeof c_str_, "%a, %d %b %Y %H:%M:%S %z", ptm),
38 sizeof(c_str_) - 1);
41 #endif // NOW_DOT_HPP