proxygen
Time.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015-present, Facebook, Inc.
3  * All rights reserved.
4  *
5  * This source code is licensed under the BSD-style license found in the
6  * LICENSE file in the root directory of this source tree. An additional grant
7  * of patent rights can be found in the PATENTS file in the same directory.
8  *
9  */
10 #pragma once
11 
12 #include <algorithm>
13 #include <chrono>
14 #include <cinttypes>
15 #include <string>
16 
17 #include <folly/portability/Time.h>
18 
19 #include <openssl/ossl_typ.h>
20 
21 namespace proxygen {
22 
23 using SteadyClock = std::chrono::steady_clock;
24 using SystemClock = std::chrono::system_clock;
25 using TimePoint = SteadyClock::time_point;
26 using SystemTimePoint = SystemClock::time_point;
27 
28 template <typename T>
29 bool durationInitialized(const T& duration) {
30  static T zero(0);
31  return duration != T::max() && duration >= zero;
32 }
33 
34 template <typename T>
35 bool timePointInitialized(const T& time) {
36  static T epoch;
37  return time > epoch;
38 }
39 
40 template <typename ClockType = SteadyClock>
41 inline std::chrono::time_point<ClockType> getCurrentTime() {
42  return ClockType::now();
43 }
44 
45 inline std::chrono::system_clock::time_point
48  std::chrono::duration_cast<std::chrono::system_clock::duration>(
49  t - SteadyClock::now());
50 }
51 
52 inline time_t toTimeT(TimePoint t) {
53  return std::chrono::system_clock::to_time_t(toSystemTimePoint(t));
54 }
55 
56 inline std::chrono::milliseconds millisecondsSinceEpoch() {
57  return std::chrono::duration_cast<std::chrono::milliseconds>(
58  std::chrono::system_clock::now().time_since_epoch());
59 }
60 
61 inline std::chrono::seconds secondsSinceEpoch() {
62  return std::chrono::duration_cast<std::chrono::seconds>(
63  std::chrono::system_clock::now().time_since_epoch());
64 }
65 
66 inline std::chrono::milliseconds millisecondsSinceEpoch(TimePoint t) {
67  return std::chrono::duration_cast<std::chrono::milliseconds>(
68  toSystemTimePoint(t).time_since_epoch());
69 }
70 
71 inline std::chrono::seconds secondsSinceEpoch(TimePoint t) {
72  return std::chrono::duration_cast<std::chrono::seconds>(
73  toSystemTimePoint(t).time_since_epoch());
74 }
75 
76 template <typename ClockType = SteadyClock>
77 inline std::chrono::microseconds microsecondsBetween(
78  std::chrono::time_point<ClockType> finish,
79  std::chrono::time_point<ClockType> start) {
80  return std::chrono::duration_cast<std::chrono::microseconds>(
81  finish - start);
82 }
83 
84 template <typename ClockType = SteadyClock>
85 inline std::chrono::milliseconds millisecondsBetween(
86  std::chrono::time_point<ClockType> finish,
87  std::chrono::time_point<ClockType> start) {
88  return std::chrono::duration_cast<std::chrono::milliseconds>(
89  finish - start);
90 }
91 
92 template <typename ClockType = SteadyClock>
93 inline std::chrono::seconds secondsBetween(
94  std::chrono::time_point<ClockType> finish,
95  std::chrono::time_point<ClockType> start) {
96  return std::chrono::duration_cast<std::chrono::seconds>(
97  finish - start);
98 }
99 
100 template <typename ClockType = SteadyClock>
101 inline std::chrono::milliseconds millisecondsSince(
102  std::chrono::time_point<ClockType> t) {
103  return millisecondsBetween(getCurrentTime<ClockType>(), t);
104 }
105 
106 template <typename ClockType = SteadyClock>
107 inline std::chrono::seconds secondsSince(std::chrono::time_point<ClockType> t) {
108  return secondsBetween(getCurrentTime<ClockType>(), t);
109 }
110 
114 inline void getDateTimeStr(char datebuf[32], char timebuf[32]) {
115  time_t now = toTimeT(getCurrentTime<SteadyClock>());
116  struct tm now_tm;
117  localtime_r(&now, &now_tm);
118  if (datebuf) {
119  strftime(datebuf, sizeof(char) * 32, "%Y-%m-%d", &now_tm);
120  }
121  if (timebuf) {
122  strftime(timebuf, sizeof(char) * 32, "%H:%M:%S", &now_tm);
123  }
124 }
125 
129 inline void getDateOffsetStr(char datebuf[32], int dayOffset) {
130  time_t t = toTimeT(getCurrentTime<SteadyClock>());
131  t += dayOffset * 24 * 60 * 60;
132  struct tm final_tm;
133  localtime_r(&t, &final_tm);
134  strftime(datebuf, sizeof(char) * 32, "%Y-%m-%d", &final_tm);
135 }
136 
146  time_t t = toTimeT(tp);
147  struct tm final_tm;
148  gmtime_r(&t, &final_tm);
149  char buf[256];
150  if (strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%S %z", &final_tm) > 0) {
151  return std::string(buf);
152  }
153  return "";
154 }
155 
164 std::string getDateTimeStr(const ASN1_TIME* const time);
165 
169 template <typename ClockType = SteadyClock>
171  public:
172  virtual ~TimeUtilGeneric() {}
173 
174  virtual std::chrono::time_point<ClockType> now() const {
175  return getCurrentTime<ClockType>();
176  }
177 
178  static const std::chrono::time_point<ClockType>& getZeroTimePoint() {
179  const static std::chrono::time_point<ClockType> kZeroTimePoint{};
180  return kZeroTimePoint;
181  }
182 
187  virtual uint64_t msSinceEpoch() {
188  return millisecondsSinceEpoch().count();
189  }
190 };
191 
192 // Typedef so as to not disrupting callers who use 'TimeUtil' before we
193 // made it TimeUtilGeneric
195 
196 }
#define T(v)
Definition: http_parser.c:233
std::chrono::milliseconds millisecondsBetween(std::chrono::time_point< ClockType > finish, std::chrono::time_point< ClockType > start)
Definition: Time.h:85
virtual uint64_t msSinceEpoch()
Definition: Time.h:187
time_t toTimeT(TimePoint t)
Definition: Time.h:52
std::chrono::seconds secondsBetween(std::chrono::time_point< ClockType > finish, std::chrono::time_point< ClockType > start)
Definition: Time.h:93
LogLevel max
Definition: LogLevel.cpp:31
bool durationInitialized(const T &duration)
Definition: Time.h:29
std::chrono::steady_clock::time_point now()
void getDateOffsetStr(char datebuf[32], int dayOffset)
Definition: Time.h:129
std::chrono::steady_clock SteadyClock
Definition: Time.h:23
virtual std::chrono::time_point< ClockType > now() const
Definition: Time.h:174
std::string getDateTimeStr(const ASN1_TIME *const time)
Definition: Time.cpp:17
std::chrono::microseconds microsecondsBetween(std::chrono::time_point< ClockType > finish, std::chrono::time_point< ClockType > start)
Definition: Time.h:77
auto start
SteadyClock::time_point TimePoint
Definition: Time.h:25
std::chrono::seconds secondsSinceEpoch()
Definition: Time.h:61
std::chrono::milliseconds millisecondsSince(std::chrono::time_point< ClockType > t)
Definition: Time.h:101
SystemClock::time_point SystemTimePoint
Definition: Time.h:26
const char * string
Definition: Conv.cpp:212
virtual ~TimeUtilGeneric()
Definition: Time.h:172
static const std::chrono::time_point< ClockType > & getZeroTimePoint()
Definition: Time.h:178
std::chrono::time_point< ClockType > getCurrentTime()
Definition: Time.h:41
std::chrono::milliseconds millisecondsSinceEpoch()
Definition: Time.h:56
std::chrono::system_clock::time_point toSystemTimePoint(TimePoint t)
Definition: Time.h:46
bool timePointInitialized(const T &time)
Definition: Time.h:35
std::chrono::nanoseconds time()
std::chrono::system_clock SystemClock
Definition: Time.h:24
std::chrono::seconds secondsSince(std::chrono::time_point< ClockType > t)
Definition: Time.h:107