HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
NET_Time.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: NET_Time.h
7  *
8  * COMMENTS:
9  * This file has helpers to convert chromiums use of time to other units of
10  * time. Chromium stores their time as microseconds since windows epoc and
11  * most other code stores time as seconds since epoch time so we need helpers
12  * to convert between the two.
13  */
14 
15 #ifndef __NET_TIME_H__
16 #define __NET_TIME_H__
17 
18 #include "NET_API.h"
19 
20 #include <UT/UT_StringHolder.h>
21 
22 #include <limits>
23 #include <time.h>
24 
25 #include <chrono>
26 
27 template <typename Duration>
28 using NET_TimePoint =
29  std::chrono::time_point<std::chrono::system_clock, Duration>;
30 
31 // We store the time as micro seconds since unix epoch time. We store it as
32 // micro seconds as the creation date for cookies are stored as the primary
33 // key. If two cookies are created within the same second (which is likely)
34 // then when we save the cookies to storage its likely we will get a
35 // constraint failure as they will both have the same creation time.
36 class NET_API NET_Time
37 {
38 public:
40  constexpr NET_Time() : myUS(std::chrono::microseconds(0)) {}
41 
42  constexpr bool isNull() const
43  {
44  return myUS == Timestamp(std::chrono::microseconds(0));
45  }
46 
47  constexpr bool isMax() const { return myUS == Timestamp::max(); }
48  constexpr bool isMin() const { return myUS == Timestamp::min(); }
49  static constexpr Timestamp minT()
50  {
51  return Timestamp(std::chrono::microseconds(0));
52  }
53  static constexpr Timestamp maxT() { return Timestamp::max(); }
54  static NET_Time max() { return NET_Time(maxT()); }
55  static NET_Time min() { return NET_Time(minT()); }
56  constexpr bool operator==(const NET_Time &time) const
57  {
58  return myUS == time.myUS;
59  }
60  constexpr bool operator!=(const NET_Time &time) const
61  {
62  return myUS != time.myUS;
63  }
64  constexpr bool operator<(const NET_Time &time) const
65  {
66  return myUS < time.myUS;
67  }
68  constexpr bool operator>(const NET_Time &time) const
69  {
70  return myUS > time.myUS;
71  }
72  constexpr bool operator<=(const NET_Time &time) const
73  {
74  return myUS <= time.myUS;
75  }
76  constexpr bool operator>=(const NET_Time &time) const
77  {
78  return myUS >= time.myUS;
79  }
80 
81  template <class T>
82  bool hasElapsed(const NET_Time &t, const T &elapse)
83  {
84  std::chrono::microseconds us = t.timestamp() - myUS;
85  T dur = std::chrono::duration_cast<T>(us);
86  return dur >= elapse;
87  }
88 
89  // Convert microseconds since windows epoch time to unix time_t
90  static NET_Time fromDeltaSinceWindowsEpoch(std::chrono::microseconds t);
91  // Convert unix time_t to microseconds since windows epoch time
92  std::chrono::microseconds toDeltaSinceWindowsEpoch() const;
93  static NET_Time fromTimeT(time_t t);
94  time_t toTimeT() const;
95 
96  // Get current time
97  static NET_Time now();
98 
99  static bool fromUTCString(const UT_StringRef &time_string, NET_Time &time);
100  static bool fromUTCStringToLocal(const UT_StringRef &time_string,
101  NET_Time &time);
102 
103  UT_StringHolder toUTCString() const;
104 
105  const Timestamp &timestamp() const { return myUS; }
106 
107 private:
108  constexpr NET_Time(Timestamp us) : myUS(us) {}
109  Timestamp myUS;
110 };
111 
112 template <>
113 inline bool
115  const NET_Time &t, const std::chrono::microseconds &us)
116 {
117  std::chrono::microseconds diff = t.timestamp() - myUS;
118  return diff >= us;
119 }
120 
121 #endif // __NET_TIME_H__
122 
vint4 max(const vint4 &a, const vint4 &b)
Definition: simd.h:4703
bool hasElapsed(const NET_Time &t, const T &elapse)
Definition: NET_Time.h:82
constexpr NET_Time()
Definition: NET_Time.h:40
constexpr bool operator!=(const NET_Time &time) const
Definition: NET_Time.h:60
GT_API const UT_StringHolder time
constexpr bool operator>=(const NET_Time &time) const
Definition: NET_Time.h:76
static constexpr Timestamp maxT()
Definition: NET_Time.h:53
constexpr bool isNull() const
Definition: NET_Time.h:42
constexpr bool isMax() const
Definition: NET_Time.h:47
std::chrono::time_point< std::chrono::system_clock, Duration > NET_TimePoint
Definition: NET_Time.h:29
static NET_Time min()
Definition: NET_Time.h:55
NET_TimePoint< std::chrono::microseconds > Timestamp
Definition: NET_Time.h:39
const Timestamp & timestamp() const
Definition: NET_Time.h:105
constexpr bool operator==(const NET_Time &time) const
Definition: NET_Time.h:56
constexpr bool operator>(const NET_Time &time) const
Definition: NET_Time.h:68
constexpr bool operator<(const NET_Time &time) const
Definition: NET_Time.h:64
constexpr bool operator<=(const NET_Time &time) const
Definition: NET_Time.h:72
static constexpr Timestamp minT()
Definition: NET_Time.h:49
vint4 min(const vint4 &a, const vint4 &b)
Definition: simd.h:4694
constexpr bool isMin() const
Definition: NET_Time.h:48
GLdouble GLdouble t
Definition: glew.h:1398
static NET_Time max()
Definition: NET_Time.h:54