• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===-- TimeValue.h - Declare OS TimeValue Concept --------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 //  This header file declares the operating system TimeValue concept.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/Support/DataTypes.h"
15 #include <string>
16 
17 #ifndef LLVM_SYSTEM_TIMEVALUE_H
18 #define LLVM_SYSTEM_TIMEVALUE_H
19 
20 namespace llvm {
21 namespace sys {
22   /// This class is used where a precise fixed point in time is required. The
23   /// range of TimeValue spans many hundreds of billions of years both past and
24   /// present.  The precision of TimeValue is to the nanosecond. However, the
25   /// actual precision of its values will be determined by the resolution of
26   /// the system clock. The TimeValue class is used in conjunction with several
27   /// other lib/System interfaces to specify the time at which a call should
28   /// timeout, etc.
29   /// @since 1.4
30   /// @brief Provides an abstraction for a fixed point in time.
31   class TimeValue {
32 
33   /// @name Constants
34   /// @{
35   public:
36 
37     /// A constant TimeValue representing the smallest time
38     /// value permissible by the class. MinTime is some point
39     /// in the distant past, about 300 billion years BCE.
40     /// @brief The smallest possible time value.
41     static const TimeValue MinTime;
42 
43     /// A constant TimeValue representing the largest time
44     /// value permissible by the class. MaxTime is some point
45     /// in the distant future, about 300 billion years AD.
46     /// @brief The largest possible time value.
47     static const TimeValue MaxTime;
48 
49     /// A constant TimeValue representing the base time,
50     /// or zero time of 00:00:00 (midnight) January 1st, 2000.
51     /// @brief 00:00:00 Jan 1, 2000 UTC.
52     static const TimeValue ZeroTime;
53 
54     /// A constant TimeValue for the Posix base time which is
55     /// 00:00:00 (midnight) January 1st, 1970.
56     /// @brief 00:00:00 Jan 1, 1970 UTC.
57     static const TimeValue PosixZeroTime;
58 
59     /// A constant TimeValue for the Win32 base time which is
60     /// 00:00:00 (midnight) January 1st, 1601.
61     /// @brief 00:00:00 Jan 1, 1601 UTC.
62     static const TimeValue Win32ZeroTime;
63 
64   /// @}
65   /// @name Types
66   /// @{
67   public:
68     typedef int64_t SecondsType;    ///< Type used for representing seconds.
69     typedef int32_t NanoSecondsType;///< Type used for representing nanoseconds.
70 
71     enum TimeConversions {
72       NANOSECONDS_PER_SECOND = 1000000000,  ///< One Billion
73       MICROSECONDS_PER_SECOND = 1000000,    ///< One Million
74       MILLISECONDS_PER_SECOND = 1000,       ///< One Thousand
75       NANOSECONDS_PER_MICROSECOND = 1000,   ///< One Thousand
76       NANOSECONDS_PER_MILLISECOND = 1000000,///< One Million
77       NANOSECONDS_PER_POSIX_TICK = 100,     ///< Posix tick is 100 Hz (10ms)
78       NANOSECONDS_PER_WIN32_TICK = 100      ///< Win32 tick is 100 Hz (10ms)
79     };
80 
81   /// @}
82   /// @name Constructors
83   /// @{
84   public:
85     /// Caller provides the exact value in seconds and nanoseconds. The
86     /// \p nanos argument defaults to zero for convenience.
87     /// @brief Explicit constructor
88     explicit TimeValue (SecondsType seconds, NanoSecondsType nanos = 0)
seconds_(seconds)89       : seconds_( seconds ), nanos_( nanos ) { this->normalize(); }
90 
91     /// Caller provides the exact value as a double in seconds with the
92     /// fractional part representing nanoseconds.
93     /// @brief Double Constructor.
TimeValue(double new_time)94     explicit TimeValue( double new_time )
95       : seconds_( 0 ) , nanos_ ( 0 ) {
96       SecondsType integer_part = static_cast<SecondsType>( new_time );
97       seconds_ = integer_part;
98       nanos_ = static_cast<NanoSecondsType>( (new_time -
99                static_cast<double>(integer_part)) * NANOSECONDS_PER_SECOND );
100       this->normalize();
101     }
102 
103     /// This is a static constructor that returns a TimeValue that represents
104     /// the current time.
105     /// @brief Creates a TimeValue with the current time (UTC).
106     static TimeValue now();
107 
108   /// @}
109   /// @name Operators
110   /// @{
111   public:
112     /// Add \p that to \p this.
113     /// @returns this
114     /// @brief Incrementing assignment operator.
115     TimeValue& operator += (const TimeValue& that ) {
116       this->seconds_ += that.seconds_  ;
117       this->nanos_ += that.nanos_ ;
118       this->normalize();
119       return *this;
120     }
121 
122     /// Subtract \p that from \p this.
123     /// @returns this
124     /// @brief Decrementing assignment operator.
125     TimeValue& operator -= (const TimeValue &that ) {
126       this->seconds_ -= that.seconds_ ;
127       this->nanos_ -= that.nanos_ ;
128       this->normalize();
129       return *this;
130     }
131 
132     /// Determine if \p this is less than \p that.
133     /// @returns True iff *this < that.
134     /// @brief True if this < that.
135     int operator < (const TimeValue &that) const { return that > *this; }
136 
137     /// Determine if \p this is greather than \p that.
138     /// @returns True iff *this > that.
139     /// @brief True if this > that.
140     int operator > (const TimeValue &that) const {
141       if ( this->seconds_ > that.seconds_ ) {
142           return 1;
143       } else if ( this->seconds_ == that.seconds_ ) {
144           if ( this->nanos_ > that.nanos_ ) return 1;
145       }
146       return 0;
147     }
148 
149     /// Determine if \p this is less than or equal to \p that.
150     /// @returns True iff *this <= that.
151     /// @brief True if this <= that.
152     int operator <= (const TimeValue &that) const { return that >= *this; }
153 
154     /// Determine if \p this is greater than or equal to \p that.
155     /// @returns True iff *this >= that.
156     int operator >= (const TimeValue &that) const {
157       if ( this->seconds_ > that.seconds_ ) {
158           return 1;
159       } else if ( this->seconds_ == that.seconds_ ) {
160           if ( this->nanos_ >= that.nanos_ ) return 1;
161       }
162       return 0;
163     }
164 
165     /// Determines if two TimeValue objects represent the same moment in time.
166     /// @returns True iff *this == that.
167     int operator == (const TimeValue &that) const {
168       return (this->seconds_ == that.seconds_) &&
169              (this->nanos_ == that.nanos_);
170     }
171 
172     /// Determines if two TimeValue objects represent times that are not the
173     /// same.
174     /// @returns True iff *this != that.
175     int operator != (const TimeValue &that) const { return !(*this == that); }
176 
177     /// Adds two TimeValue objects together.
178     /// @returns The sum of the two operands as a new TimeValue
179     /// @brief Addition operator.
180     friend TimeValue operator + (const TimeValue &tv1, const TimeValue &tv2);
181 
182     /// Subtracts two TimeValue objects.
183     /// @returns The difference of the two operands as a new TimeValue
184     /// @brief Subtraction operator.
185     friend TimeValue operator - (const TimeValue &tv1, const TimeValue &tv2);
186 
187   /// @}
188   /// @name Accessors
189   /// @{
190   public:
191 
192     /// Returns only the seconds component of the TimeValue. The nanoseconds
193     /// portion is ignored. No rounding is performed.
194     /// @brief Retrieve the seconds component
seconds()195     SecondsType seconds() const { return seconds_; }
196 
197     /// Returns only the nanoseconds component of the TimeValue. The seconds
198     /// portion is ignored.
199     /// @brief Retrieve the nanoseconds component.
nanoseconds()200     NanoSecondsType nanoseconds() const { return nanos_; }
201 
202     /// Returns only the fractional portion of the TimeValue rounded down to the
203     /// nearest microsecond (divide by one thousand).
204     /// @brief Retrieve the fractional part as microseconds;
microseconds()205     uint32_t microseconds() const {
206       return nanos_ / NANOSECONDS_PER_MICROSECOND;
207     }
208 
209     /// Returns only the fractional portion of the TimeValue rounded down to the
210     /// nearest millisecond (divide by one million).
211     /// @brief Retrieve the fractional part as milliseconds;
milliseconds()212     uint32_t milliseconds() const {
213       return nanos_ / NANOSECONDS_PER_MILLISECOND;
214     }
215 
216     /// Returns the TimeValue as a number of microseconds. Note that the value
217     /// returned can overflow because the range of a uint64_t is smaller than
218     /// the range of a TimeValue. Nevertheless, this is useful on some operating
219     /// systems and is therefore provided.
220     /// @brief Convert to a number of microseconds (can overflow)
usec()221     uint64_t usec() const {
222       return seconds_ * MICROSECONDS_PER_SECOND +
223              ( nanos_ / NANOSECONDS_PER_MICROSECOND );
224     }
225 
226     /// Returns the TimeValue as a number of milliseconds. Note that the value
227     /// returned can overflow because the range of a uint64_t is smaller than
228     /// the range of a TimeValue. Nevertheless, this is useful on some operating
229     /// systems and is therefore provided.
230     /// @brief Convert to a number of milliseconds (can overflow)
msec()231     uint64_t msec() const {
232       return seconds_ * MILLISECONDS_PER_SECOND +
233              ( nanos_ / NANOSECONDS_PER_MILLISECOND );
234     }
235 
236     /// Converts the TimeValue into the corresponding number of "ticks" for
237     /// Posix, correcting for the difference in Posix zero time.
238     /// @brief Convert to unix time (100 nanoseconds since 12:00:00a Jan 1,1970)
toPosixTime()239     uint64_t toPosixTime() const {
240       uint64_t result = seconds_ - PosixZeroTime.seconds_;
241       result += nanos_ / NANOSECONDS_PER_POSIX_TICK;
242       return result;
243     }
244 
245     /// Converts the TimeValue into the corresponding number of seconds
246     /// since the epoch (00:00:00 Jan 1,1970).
toEpochTime()247     uint64_t toEpochTime() const {
248       return seconds_ - PosixZeroTime.seconds_;
249     }
250 
251     /// Converts the TimeValue into the corresponding number of "ticks" for
252     /// Win32 platforms, correcting for the difference in Win32 zero time.
253     /// @brief Convert to windows time (seconds since 12:00:00a Jan 1, 1601)
toWin32Time()254     uint64_t toWin32Time() const {
255       uint64_t result = seconds_ - Win32ZeroTime.seconds_;
256       result += nanos_ / NANOSECONDS_PER_WIN32_TICK;
257       return result;
258     }
259 
260     /// Provides the seconds and nanoseconds as results in its arguments after
261     /// correction for the Posix zero time.
262     /// @brief Convert to timespec time (ala POSIX.1b)
getTimespecTime(uint64_t & seconds,uint32_t & nanos)263     void getTimespecTime( uint64_t& seconds, uint32_t& nanos ) const {
264       seconds = seconds_ - PosixZeroTime.seconds_;
265       nanos = nanos_;
266     }
267 
268     /// Provides conversion of the TimeValue into a readable time & date.
269     /// @returns std::string containing the readable time value
270     /// @brief Convert time to a string.
271     std::string str() const;
272 
273   /// @}
274   /// @name Mutators
275   /// @{
276   public:
277     /// The seconds component of the TimeValue is set to \p sec without
278     /// modifying the nanoseconds part.  This is useful for whole second
279     /// arithmetic.
280     /// @brief Set the seconds component.
seconds(SecondsType sec)281     void seconds (SecondsType sec ) {
282       this->seconds_ = sec;
283       this->normalize();
284     }
285 
286     /// The nanoseconds component of the TimeValue is set to \p nanos without
287     /// modifying the seconds part. This is useful for basic computations
288     /// involving just the nanoseconds portion. Note that the TimeValue will be
289     /// normalized after this call so that the fractional (nanoseconds) portion
290     /// will have the smallest equivalent value.
291     /// @brief Set the nanoseconds component using a number of nanoseconds.
nanoseconds(NanoSecondsType nanos)292     void nanoseconds ( NanoSecondsType nanos ) {
293       this->nanos_ = nanos;
294       this->normalize();
295     }
296 
297     /// The seconds component remains unchanged.
298     /// @brief Set the nanoseconds component using a number of microseconds.
microseconds(int32_t micros)299     void microseconds ( int32_t micros ) {
300       this->nanos_ = micros * NANOSECONDS_PER_MICROSECOND;
301       this->normalize();
302     }
303 
304     /// The seconds component remains unchanged.
305     /// @brief Set the nanoseconds component using a number of milliseconds.
milliseconds(int32_t millis)306     void milliseconds ( int32_t millis ) {
307       this->nanos_ = millis * NANOSECONDS_PER_MILLISECOND;
308       this->normalize();
309     }
310 
311     /// @brief Converts from microsecond format to TimeValue format
usec(int64_t microseconds)312     void usec( int64_t microseconds ) {
313       this->seconds_ = microseconds / MICROSECONDS_PER_SECOND;
314       this->nanos_ = NanoSecondsType(microseconds % MICROSECONDS_PER_SECOND) *
315         NANOSECONDS_PER_MICROSECOND;
316       this->normalize();
317     }
318 
319     /// @brief Converts from millisecond format to TimeValue format
msec(int64_t milliseconds)320     void msec( int64_t milliseconds ) {
321       this->seconds_ = milliseconds / MILLISECONDS_PER_SECOND;
322       this->nanos_ = NanoSecondsType(milliseconds % MILLISECONDS_PER_SECOND) *
323         NANOSECONDS_PER_MILLISECOND;
324       this->normalize();
325     }
326 
327     /// Converts the \p seconds argument from PosixTime to the corresponding
328     /// TimeValue and assigns that value to \p this.
329     /// @brief Convert seconds form PosixTime to TimeValue
fromEpochTime(SecondsType seconds)330     void fromEpochTime( SecondsType seconds ) {
331       seconds_ = seconds + PosixZeroTime.seconds_;
332       nanos_ = 0;
333       this->normalize();
334     }
335 
336     /// Converts the \p win32Time argument from Windows FILETIME to the
337     /// corresponding TimeValue and assigns that value to \p this.
338     /// @brief Convert seconds form Windows FILETIME to TimeValue
fromWin32Time(uint64_t win32Time)339     void fromWin32Time( uint64_t win32Time ) {
340       this->seconds_ = win32Time / 10000000 + Win32ZeroTime.seconds_;
341       this->nanos_ = NanoSecondsType(win32Time  % 10000000) * 100;
342     }
343 
344   /// @}
345   /// @name Implementation
346   /// @{
347   private:
348     /// This causes the values to be represented so that the fractional
349     /// part is minimized, possibly incrementing the seconds part.
350     /// @brief Normalize to canonical form.
351     void normalize();
352 
353   /// @}
354   /// @name Data
355   /// @{
356   private:
357     /// Store the values as a <timeval>.
358     SecondsType      seconds_;///< Stores the seconds part of the TimeVal
359     NanoSecondsType  nanos_;  ///< Stores the nanoseconds part of the TimeVal
360   /// @}
361 
362   };
363 
364 inline TimeValue operator + (const TimeValue &tv1, const TimeValue &tv2) {
365   TimeValue sum (tv1.seconds_ + tv2.seconds_, tv1.nanos_ + tv2.nanos_);
366   sum.normalize ();
367   return sum;
368 }
369 
370 inline TimeValue operator - (const TimeValue &tv1, const TimeValue &tv2) {
371   TimeValue difference (tv1.seconds_ - tv2.seconds_, tv1.nanos_ - tv2.nanos_ );
372   difference.normalize ();
373   return difference;
374 }
375 
376 }
377 }
378 
379 #endif
380