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 Apache License, Version 2.0 (the "Apache License")
5 // with the following modification; you may not use this file except in
6 // compliance with the Apache License and the following modification to it:
7 // Section 6. Trademarks. is deleted and replaced with:
8 //
9 // 6. Trademarks. This License does not grant permission to use the trade
10 // names, trademarks, service marks, or product names of the Licensor
11 // and its affiliates, except as required to comply with Section 4(c) of
12 // the License and to reproduce the content of the NOTICE file.
13 //
14 // You may obtain a copy of the Apache License at
15 //
16 // http://www.apache.org/licenses/LICENSE-2.0
17 //
18 // Unless required by applicable law or agreed to in writing, software
19 // distributed under the Apache License with the above modification is
20 // distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21 // KIND, either express or implied. See the Apache License for the specific
22 // language governing permissions and limitations under the Apache License.
23 //
24 #ifndef PXR_USD_AR_TIMESTAMP_H
25 #define PXR_USD_AR_TIMESTAMP_H
26 
27 /// \file ar/timestamp.h
28 
29 #include "pxr/pxr.h"
30 #include "pxr/usd/ar/api.h"
31 
32 #include "pxr/base/arch/hints.h"
33 #include "pxr/base/tf/hash.h"
34 
35 #include <limits>
36 
38 
39 /// \class ArTimestamp
40 /// Represents a timestamp for an asset. Timestamps are represented by
41 /// Unix time, the number of seconds elapsed since 00:00:00 UTC 1/1/1970.
43 {
44 public:
45  /// Create an invalid timestamp.
47  : _time(std::numeric_limits<double>::quiet_NaN())
48  {
49  }
50 
51  /// Create a timestamp at \p time, which must be a Unix time value.
52  explicit ArTimestamp(double time)
53  : _time(time)
54  {
55  }
56 
57  /// Return true if this timestamp is valid, false otherwise.
58  bool IsValid() const
59  {
60  return !std::isnan(_time);
61  }
62 
63  /// Return the time represented by this timestamp as a double.
64  /// If this timestamp is invalid, issue a coding error and
65  /// return a quiet NaN value.
66  double GetTime() const
67  {
68  if (ARCH_UNLIKELY(!IsValid())) {
69  _IssueInvalidGetTimeError();
70  }
71  return _time;
72  }
73 
74  /// Comparison operators
75  /// Note that invalid timestamps are considered less than all
76  /// other timestamps.
77  /// @{
78 
79  friend bool operator==(const ArTimestamp& lhs, const ArTimestamp& rhs)
80  {
81  return (!lhs.IsValid() && !rhs.IsValid()) ||
82  (lhs.IsValid() && rhs.IsValid() && lhs._time == rhs._time);
83  }
84 
85  friend bool operator!=(const ArTimestamp& lhs, const ArTimestamp& rhs)
86  {
87  return !(lhs == rhs);
88  }
89 
90  friend bool operator<(const ArTimestamp& lhs, const ArTimestamp& rhs)
91  {
92  return (!lhs.IsValid() && rhs.IsValid()) ||
93  (lhs.IsValid() && rhs.IsValid() && lhs._time < rhs._time);
94  }
95 
96  friend bool operator>=(const ArTimestamp& lhs, const ArTimestamp& rhs)
97  {
98  return !(lhs < rhs);
99  }
100 
101  friend bool operator<=(const ArTimestamp& lhs, const ArTimestamp& rhs)
102  {
103  return !lhs.IsValid() || (rhs.IsValid() && lhs._time <= rhs._time);
104  }
105 
106  friend bool operator>(const ArTimestamp& lhs, const ArTimestamp& rhs)
107  {
108  return !(lhs <= rhs);
109  }
110 
111  /// @}
112 
113 private:
114  AR_API
115  void _IssueInvalidGetTimeError() const;
116 
117  // TfHash support.
118  template <class HashState>
119  friend void TfHashAppend(HashState& h, const ArTimestamp& t)
120  {
121  h.Append(t._time);
122  }
123 
124  double _time;
125 };
126 
128 
129 #endif
ArTimestamp()
Create an invalid timestamp.
Definition: timestamp.h:46
GT_API const UT_StringHolder time
friend bool operator!=(const ArTimestamp &lhs, const ArTimestamp &rhs)
Definition: timestamp.h:85
friend bool operator>=(const ArTimestamp &lhs, const ArTimestamp &rhs)
Definition: timestamp.h:96
#define AR_API
Definition: api.h:40
#define ARCH_UNLIKELY(x)
Definition: hints.h:47
friend bool operator>(const ArTimestamp &lhs, const ArTimestamp &rhs)
Definition: timestamp.h:106
double GetTime() const
Definition: timestamp.h:66
bool IsValid() const
Return true if this timestamp is valid, false otherwise.
Definition: timestamp.h:58
friend void TfHashAppend(HashState &h, const ArTimestamp &t)
Definition: timestamp.h:119
GLdouble t
Definition: glad.h:2397
friend bool operator==(const ArTimestamp &lhs, const ArTimestamp &rhs)
Definition: timestamp.h:79
friend bool operator<(const ArTimestamp &lhs, const ArTimestamp &rhs)
Definition: timestamp.h:90
GLfloat GLfloat GLfloat GLfloat h
Definition: glcorearb.h:2002
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1441
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
ArTimestamp(double time)
Create a timestamp at time, which must be a Unix time value.
Definition: timestamp.h:52
friend bool operator<=(const ArTimestamp &lhs, const ArTimestamp &rhs)
Definition: timestamp.h:101