14#ifndef OPENVPN_TIME_TIMESTR_H
15#define OPENVPN_TIME_TIMESTR_H
28#if defined(OPENVPN_PLATFORM_WIN)
36#if defined(OPENVPN_PLATFORM_WIN)
38inline std::string
date_time(
const time_t now)
40 struct tm *lt = localtime(&now);
41 char *
ret = asctime(lt);
42 const size_t len = std::strlen(
ret);
43 if (len > 0 &&
ret[len - 1] ==
'\n')
50 const time_t now = time(NULL);
67 std::memset(<, 0,
sizeof(lt));
68 if (!localtime_r(&t, <))
69 return "LOCALTIME_ERROR";
70 if (!asctime_r(<, buf))
71 return "ASCTIME_ERROR";
72 const size_t len = std::strlen(buf);
73 if (len > 0 && buf[len - 1] ==
'\n')
75 return std::string(buf);
83 std::memset(<, 0,
sizeof(lt));
84 if (!gmtime_r(&t, <))
85 return "GMTIME_ERROR";
86 if (!asctime_r(<, buf))
87 return "ASCTIME_ERROR";
88 const size_t len = std::strlen(buf);
89 if (len > 0 && buf[len - 1] ==
'\n')
91 return std::string(buf);
96inline std::string
date_time(
const struct timeval *tv,
const bool msecs)
98 const std::string dt =
date_time(tv->tv_sec);
102 const size_t pos = dt.find_last_of(
':');
103 if (pos != std::string::npos
104 && pos + 3 < dt.length()
110 ::snprintf(ms,
sizeof(ms),
".%03u",
static_cast<unsigned int>(tv->tv_usec / 1000));
111 return dt.substr(0, pos + 3) + ms + dt.substr(pos + 3);
119 const std::uint64_t sec = ns_time / std::uint64_t(1000000000);
120 const std::uint64_t ns = ns_time % std::uint64_t(1000000000);
125 const size_t pos = dt.find_last_of(
':');
126 if (pos != std::string::npos
127 && pos + 3 < dt.length()
133 ::snprintf(ms,
sizeof(ms),
".%09u", (
unsigned int)ns);
134 return dt.substr(0, pos + 3) + ms + dt.substr(pos + 3);
142 if (::gettimeofday(&tv,
nullptr) < 0)
153 if (::gettimeofday(&tv,
nullptr) < 0)
169#if defined(OPENVPN_PLATFORM_WIN)
170 if (gmtime_s(<, &t))
174 if (!strftime(buf,
sizeof(buf),
"%a, %d %b %Y %H:%M:%S GMT", <))
177 if (!gmtime_r(&t, <))
179 if (!strftime(buf,
sizeof(buf),
"%a, %d %b %Y %T %Z", <))
182 return std::string(buf);
bool is_digit(const char c)
bool is_space(const char c)
Support deferred server-side state creation when client connects.
std::string nanosec_time_to_string(const std::uint64_t ns_time)
std::string date_time_store_time_t(time_t &save)
std::string date_time_rfc822()
std::string date_time_utc(const time_t t)