HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
timestamp.h
Go to the documentation of this file.
1 //
2 // Copyright 2021 Pixar
3 //
4 // Licensed under the terms set forth in the LICENSE.txt file available at
5 // https://openusd.org/license.
6 //
7 #ifndef PXR_USD_AR_TIMESTAMP_H
8 #define PXR_USD_AR_TIMESTAMP_H
9 
10 /// \file ar/timestamp.h
11 
12 #include "pxr/pxr.h"
13 #include "pxr/usd/ar/api.h"
14 
15 #include "pxr/base/arch/hints.h"
16 #include "pxr/base/tf/hash.h"
17 
18 #include <limits>
19 
21 
22 /// \class ArTimestamp
23 /// Represents a timestamp for an asset. Timestamps are represented by
24 /// Unix time, the number of seconds elapsed since 00:00:00 UTC 1/1/1970.
26 {
27 public:
28  /// Create an invalid timestamp.
30  : _time(std::numeric_limits<double>::quiet_NaN())
31  {
32  }
33 
34  /// Create a timestamp at \p time, which must be a Unix time value.
35  explicit ArTimestamp(double time)
36  : _time(time)
37  {
38  }
39 
40  /// Return true if this timestamp is valid, false otherwise.
41  bool IsValid() const
42  {
43  return !std::isnan(_time);
44  }
45 
46  /// Return the time represented by this timestamp as a double.
47  /// If this timestamp is invalid, issue a coding error and
48  /// return a quiet NaN value.
49  double GetTime() const
50  {
51  if (ARCH_UNLIKELY(!IsValid())) {
52  _IssueInvalidGetTimeError();
53  }
54  return _time;
55  }
56 
57  /// Comparison operators
58  /// Note that invalid timestamps are considered less than all
59  /// other timestamps.
60  /// @{
61 
62  friend bool operator==(const ArTimestamp& lhs, const ArTimestamp& rhs)
63  {
64  return (!lhs.IsValid() && !rhs.IsValid()) ||
65  (lhs.IsValid() && rhs.IsValid() && lhs._time == rhs._time);
66  }
67 
68  friend bool operator!=(const ArTimestamp& lhs, const ArTimestamp& rhs)
69  {
70  return !(lhs == rhs);
71  }
72 
73  friend bool operator<(const ArTimestamp& lhs, const ArTimestamp& rhs)
74  {
75  return (!lhs.IsValid() && rhs.IsValid()) ||
76  (lhs.IsValid() && rhs.IsValid() && lhs._time < rhs._time);
77  }
78 
79  friend bool operator>=(const ArTimestamp& lhs, const ArTimestamp& rhs)
80  {
81  return !(lhs < rhs);
82  }
83 
84  friend bool operator<=(const ArTimestamp& lhs, const ArTimestamp& rhs)
85  {
86  return !lhs.IsValid() || (rhs.IsValid() && lhs._time <= rhs._time);
87  }
88 
89  friend bool operator>(const ArTimestamp& lhs, const ArTimestamp& rhs)
90  {
91  return !(lhs <= rhs);
92  }
93 
94  /// @}
95 
96 private:
97  AR_API
98  void _IssueInvalidGetTimeError() const;
99 
100  // TfHash support.
101  template <class HashState>
102  friend void TfHashAppend(HashState& h, const ArTimestamp& t)
103  {
104  h.Append(t._time);
105  }
106 
107  double _time;
108 };
109 
111 
112 #endif
ArTimestamp()
Create an invalid timestamp.
Definition: timestamp.h:29
GT_API const UT_StringHolder time
friend bool operator!=(const ArTimestamp &lhs, const ArTimestamp &rhs)
Definition: timestamp.h:68
friend bool operator>=(const ArTimestamp &lhs, const ArTimestamp &rhs)
Definition: timestamp.h:79
#define AR_API
Definition: api.h:23
#define ARCH_UNLIKELY(x)
Definition: hints.h:30
friend bool operator>(const ArTimestamp &lhs, const ArTimestamp &rhs)
Definition: timestamp.h:89
double GetTime() const
Definition: timestamp.h:49
bool IsValid() const
Return true if this timestamp is valid, false otherwise.
Definition: timestamp.h:41
friend void TfHashAppend(HashState &h, const ArTimestamp &t)
Definition: timestamp.h:102
GLdouble t
Definition: glad.h:2397
friend bool operator==(const ArTimestamp &lhs, const ArTimestamp &rhs)
Definition: timestamp.h:62
friend bool operator<(const ArTimestamp &lhs, const ArTimestamp &rhs)
Definition: timestamp.h:73
GLfloat GLfloat GLfloat GLfloat h
Definition: glcorearb.h:2002
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
ArTimestamp(double time)
Create a timestamp at time, which must be a Unix time value.
Definition: timestamp.h:35
friend bool operator<=(const ArTimestamp &lhs, const ArTimestamp &rhs)
Definition: timestamp.h:84