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.
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  template <typename Duration>
98  {
99  return NET_Time(tp);
100  }
101 
102  // Get current time
103  static NET_Time now();
104 
105  static bool fromUTCString(const UT_StringRef &time_string, NET_Time &time);
106  static bool fromUTCStringToLocal(const UT_StringRef &time_string,
107  NET_Time &time);
108 
109  UT_StringHolder toUTCString() const;
110 
111  const Timestamp &timestamp() const { return myUS; }
112 
113 private:
114  constexpr NET_Time(Timestamp us) : myUS(us) {}
115  Timestamp myUS;
116 };
117 
118 template <>
119 inline bool
121  const NET_Time &t, const std::chrono::microseconds &us)
122 {
123  std::chrono::microseconds diff = t.timestamp() - myUS;
124  return diff >= us;
125 }
126 
127 #endif // __NET_TIME_H__
128 
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
ImageBuf OIIO_API min(Image_or_Const A, Image_or_Const B, ROI roi={}, int nthreads=0)
constexpr bool isMax() const
Definition: NET_Time.h:47
static NET_Time fromTimestamp(const NET_TimePoint< Duration > &tp)
Definition: NET_Time.h:97
std::chrono::time_point< std::chrono::system_clock, Duration > NET_TimePoint
Definition: NET_Time.h:29
#define NET_API
Definition: NET_API.h:9
std::chrono::time_point< std::chrono::system_clock > Timestamp
Definition: logging.h:57
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:111
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
GLdouble t
Definition: glad.h:2397
ImageBuf OIIO_API max(Image_or_Const A, Image_or_Const B, ROI roi={}, int nthreads=0)
static constexpr Timestamp minT()
Definition: NET_Time.h:49
constexpr bool isMin() const
Definition: NET_Time.h:48
static NET_Time max()
Definition: NET_Time.h:54