BeTimeUtilities.h
Go to the documentation of this file.
1 /*--------------------------------------------------------------------------------------+
2 |
3 | Supplied under applicable software license agreement.
4 |
5 | Copyright (c) 2018 Bentley Systems, Incorporated. All rights reserved.
6 |
7 +---------------------------------------------------------------------------------------*/
8 #pragma once
9 
12 
13 #include "Bentley.h"
14 #include "WString.h"
15 #include <time.h>
16 #include <chrono>
17 
18 struct _FILETIME;
19 struct tm;
20 
22 
25 
26 /*=================================================================================**/
34 struct BeTimeUtilities
35  {
38 
41  BENTLEYDLL_EXPORT static UInt64 QueryMillisecondsCounter ();
42 
46  BENTLEYDLL_EXPORT static double QuerySecondsCounter () {return QueryMillisecondsCounter() / 1000.0;}
47 
53  BENTLEYDLL_EXPORT static UInt32 QueryMillisecondsCounterUInt32 () {return (UInt32)((0xffffffff & QueryMillisecondsCounter()));}
54 
56 
59 
61  BENTLEYDLL_EXPORT static UInt64 GetCurrentTimeAsUnixMillis ();
62 
64  static double GetCurrentTimeAsUnixMillisDouble () {return (double)(Int64)GetCurrentTimeAsUnixMillis();}
65 
68  BENTLEYDLL_EXPORT static double GetCurrentTimeAsUnixMillisDoubleWithDelay ();
69 
72  BENTLEYDLL_EXPORT static UInt64 GetCurrentTimeAsUnixMillisWithDelay ();
74 
77 
82  BENTLEYDLL_EXPORT static BentleyStatus UnixMillisToString (WStringP timeStrP, WStringP dateStrP, double inputTime);
83 
87  BENTLEYDLL_EXPORT static UInt64 ConvertFiletimeToUnixMillis (_FILETIME const& f);
88 
92  static double ConvertFiletimeToUnixMillisDouble (_FILETIME const& f) {return (double)(Int64)ConvertFiletimeToUnixMillis(f);}
93 
97  BENTLEYDLL_EXPORT static void ConvertUnixMillisToFiletime (_FILETIME& f, UInt64 u);
98 
102  BENTLEYDLL_EXPORT static void ConvertUnixTimeToFiletime (_FILETIME& f, time_t t);
103 
107  static void ConvertUnixMillisToFiletime (_FILETIME& f, double u) {return ConvertUnixMillisToFiletime(f, (UInt64)u);}
108 
113  BENTLEYDLL_EXPORT static UInt64 ConvertTmToUnixMillis (tm const& t);
114 
120  BENTLEYDLL_EXPORT static BentleyStatus ConvertUnixMillisToTm (struct tm& t, UInt64 umillis);
121 
126  static double ConvertTmToUnixMillisDouble (tm const& t) {return (double)(Int64)ConvertTmToUnixMillis(t);}
127 
132  BENTLEYDLL_EXPORT static BentleyStatus AdjustUnixMillisForLocalTime (UInt64& u);
133 
138  BENTLEYDLL_EXPORT static BentleyStatus ConvertUnixMillisToLocalTime (struct tm& localTime, UInt64 unixMilliseconds);
139 
140  // Superficial type conversions. These functions do not rebase or convert the time value, they just cast it to an equivalent type.
141  BENTLEYDLL_EXPORT static void UInt64AsFileTime (_FILETIME&, UInt64 i);
142  BENTLEYDLL_EXPORT static void Int64AsFileTime (_FILETIME&, Int64 i);
143  BENTLEYDLL_EXPORT static UInt64 FileTimeAsUInt64 (_FILETIME const& f);
144  BENTLEYDLL_EXPORT static Int64 FileTimeAsInt64 (_FILETIME const& f);
146  };
147 
148 
150 /*=================================================================================**/
154 struct StopWatch : NonCopyableClass
155  {
156 private:
157  WString m_description;
158  UInt64 m_start;
159  UInt64 m_stop;
160  UInt64 m_frequency;
161 
162 protected:
164  BENTLEYDLL_EXPORT void SetFrequency();
165 
166 public:
169  BENTLEYDLL_EXPORT static UInt64 Now();
171  void Init(bool start) {m_start=m_stop=0; SetFrequency(); if (start) Start();}
175  explicit StopWatch (WCharCP description = L"", bool startImmediately=false) : m_description (description) {Init(startImmediately);}
179  StopWatch (Utf8CP description, bool startImmediately) : m_description (description, true) {Init(startImmediately);}
180  StopWatch (bool startImmediately) : m_description (L"") {Init(startImmediately);}
182  WString GetDescription() {return m_description;}
183 
185  void Start(){m_start = Now();}
187  void Stop() {m_stop = Now();}
188 
190  double GetCurrentSeconds() {return (Now() - m_start) / (double)m_frequency;}
191 
193  double GetElapsedSeconds() {return (m_stop - m_start) / (double)m_frequency;}
194  };
195 
196 //=======================================================================================
199 // @bsiclass Bentley Systems
200 //=======================================================================================
201 struct BeDuration : std::chrono::steady_clock::duration
202 {
203  DEFINE_T_SUPER(std::chrono::steady_clock::duration)
204  typedef std::chrono::nanoseconds Nanoseconds;
205  typedef std::chrono::milliseconds Milliseconds;
206  typedef std::chrono::seconds Seconds;
207 
208  constexpr BeDuration() : T_Super(0) {}
209  constexpr BeDuration(Seconds val) : T_Super(val) {} // allow implicit conversion
210  constexpr BeDuration(Milliseconds val) : T_Super(val) {} // allow implicit conversion
211  constexpr BeDuration(Nanoseconds val) : T_Super(val) {} // allow implicit conversion
212  constexpr BeDuration(std::chrono::duration<double,BeDuration::period> val) : T_Super(std::chrono::duration_cast<T_Super>(val)) {}
213  BeDuration(int) = delete; // Note: you must specifiy units!
214  BeDuration(double) = delete; // Note: you must specifiy units!
215 
217  constexpr static BeDuration FromSeconds(double val) {return BeDuration(std::chrono::duration_cast<T_Super>(std::chrono::duration<double>(val)));}
218 
220  constexpr static BeDuration FromMilliseconds(int64_t val) {return BeDuration(Milliseconds(val));}
221 
223  constexpr operator double() const {return std::chrono::duration_cast<std::chrono::duration<double>>(*this).count();}
224 
226  constexpr double ToSeconds() const {return (double) *this;}
227 
228  constexpr operator Milliseconds() const {return std::chrono::duration_cast<Milliseconds>(*this);}
229  constexpr operator Seconds() const {return std::chrono::duration_cast<Seconds>(*this);}
230 
232  bool IsZero() const {return 0 == count();}
233 
235  bool IsTowardsFuture() const {return 0 < count();}
236 
238  bool IsTowardsPast() const {return 0 > count();}
239 
241  BENTLEYDLL_EXPORT void Sleep();
242 };
243 
244 //=======================================================================================
247 // @bsiclass Bentley Systems
248 //=======================================================================================
249 struct BeTimePoint : std::chrono::steady_clock::time_point
250 {
251  DEFINE_T_SUPER(std::chrono::steady_clock::time_point)
252  using T_Super::time_point;
253 
255  static BeTimePoint Now() {return std::chrono::steady_clock::now();}
256 
259  static BeTimePoint FromNow(BeDuration val) {return Now()+val;}
260 
263  static BeTimePoint BeforeNow(BeDuration val) {return Now()-val;}
264 
265  BeTimePoint::rep GetTicks() const {return time_since_epoch().count();}
266 
268  bool IsValid() const {return 0 != GetTicks();}
269 
272  bool IsInFuture() const {return IsValid() && (Now() < *this);}
273 
276  bool IsInPast() const {return IsValid() && (Now() > *this);}
277 };
278 
280 
281 
282 
283 
285 
287 
#define BENTLEYDLL_EXPORT
Definition: Bentley.h:249
long long int64_t
Definition: Bentley.r.h:94
size_type count(const key_type &__x) const
Definition: stdcxx/bstdmap.h:277
wchar_t const * WCharCP
Definition: Bentley.h:224
struct Bentley::WString * WStringP
Definition: Bentley.h:239
uint32_t UInt32
Definition: Bentley.r.h:128
#define BEGIN_BENTLEY_NAMESPACE
Definition: Bentley.r.h:24
Curve's start point.
BentleyStatus
Definition: Bentley.h:208
Defines typedefs and constants that can be used across other namespaces. All Bentley-authored C++ sou...
#define DEFINE_T_SUPER(B)
Definition: Bentley.h:167
uint64_t UInt64
Definition: Bentley.r.h:131
Utf8Char const * Utf8CP
Definition: Bentley.h:229
#define END_BENTLEY_NAMESPACE
Definition: Bentley.r.h:25
int64_t Int64
Definition: Bentley.r.h:130

Copyright © 2017 Bentley Systems, Incorporated. All rights reserved.