Clean up the interface of TimeValue:
[oota-llvm.git] / include / llvm / System / TimeValue.h
1 //===-- TimeValue.h - Declare OS TimeValue Concept ---------------*- C++ -*-===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by Reid Spencer and is distributed under the 
6 // University of Illinois Open Source 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
16 #ifndef LLVM_SYSTEM_TIMEVALUE_H
17 #define LLVM_SYSTEM_TIMEVALUE_H
18
19 namespace llvm {
20 namespace sys {
21   /// This class is used where a precise fixed point in time is required. The 
22   /// range of TimeValue spans many hundreds of billions of years both past and 
23   /// present.  The precision of TimeValue is to the nanosecond. However, the 
24   /// actual precision of its values will be determined by the resolution of 
25   /// the system clock. The TimeValue class is used in conjunction with several 
26   /// other lib/System interfaces to specify the time at which a call should 
27   /// timeout, etc.
28   /// @since 1.4
29   /// @brief Provides an abstraction for a fixed point in time.
30   class TimeValue {
31
32   /// @name Constants
33   /// @{
34   public:
35
36     /// A constant TimeValue representing the smallest time
37     /// value permissable by the class. MinTime is some point
38     /// in the distant past, about 300 billion years BCE.
39     /// @brief The smallest possible time value.
40     static const TimeValue MinTime;
41
42     /// A constant TimeValue representing the largest time
43     /// value permissable by the class. MaxTime is some point
44     /// in the distant future, about 300 billion years AD.
45     /// @brief The largest possible time value.
46     static const TimeValue MaxTime;
47
48     /// A constant TimeValue representing the base time,
49     /// or zero time of 00:00:00 (midnight) January 1st, 2000.
50     /// @brief 00:00:00 Jan 1, 2000 UTC.
51     static const TimeValue ZeroTime;
52
53     /// A constant TimeValue for the Posix base time which is
54     /// 00:00:00 (midnight) January 1st, 1970.
55     /// @brief 00:00:00 Jan 1, 1970 UTC.
56     static const TimeValue PosixZeroTime;
57
58     /// A constant TimeValue for the Win32 base time which is
59     /// 00:00:00 (midnight) January 1st, 1601.
60     /// @brief 00:00:00 Jan 1, 1601 UTC.
61     static const TimeValue Win32ZeroTime;
62
63   /// @}
64   /// @name Types
65   /// @{
66   public:
67     typedef int64_t SecondsType;        ///< Type used for representing seconds.
68     typedef int32_t NanoSecondsType;    ///< Type used for representing nanoseconds.
69
70     enum TimeConversions {
71       NANOSECONDS_PER_SECOND = 1000000000,  ///< One Billion
72       MICROSECONDS_PER_SECOND = 1000000,    ///< One Million
73       MILLISECONDS_PER_SECOND = 1000,       ///< One Thousand
74       NANOSECONDS_PER_MICROSECOND = 1000,   ///< One Thousand
75       NANOSECONDS_PER_MILLISECOND = 1000000,///< One Million
76       NANOSECONDS_PER_POSIX_TICK = 100,     ///< Posix tick is 100 Hz (10ms)
77       NANOSECONDS_PER_WIN32_TICK = 100,     ///< Win32 tick is 100 Hz (10ms)
78     };
79
80   /// @}
81   /// @name Constructors
82   /// @{
83   public:
84     /// Caller provides the exact value in seconds and nanoseconds. The 
85     /// \p nanos argument defaults to zero for convenience.
86     /// @brief Explicit constructor 
87     explicit TimeValue (SecondsType seconds, NanoSecondsType nanos = 0)
88       : seconds_( seconds )
89       , 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.
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     /// @brief True if this >= that.
157     int operator >= (const TimeValue &that) const {
158       if ( this->seconds_ > that.seconds_ ) {
159           return 1;
160       } else if ( this->seconds_ == that.seconds_ ) {
161           if ( this->nanos_ >= that.nanos_ ) return 1;
162       }
163       return 0;
164     }
165
166     /// Determines if two TimeValue objects represent the same moment in time.
167     /// @brief True iff *this == that.
168     /// @brief True if this == that.
169     int operator == (const TimeValue &that) const {
170       return (this->seconds_ == that.seconds_) && 
171              (this->nanos_ == that.nanos_);
172     }
173
174     /// Determines if two TimeValue objects represent times that are not the
175     /// same.
176     /// @return True iff *this != that.
177     /// @brief True if this != that.
178     int operator != (const TimeValue &that) const { return !(*this == that); }
179
180     /// Adds two TimeValue objects together.
181     /// @returns The sum of the two operands as a new TimeValue
182     /// @brief Addition operator.
183     friend TimeValue operator + (const TimeValue &tv1, const TimeValue &tv2);
184
185     /// Subtracts two TimeValue objects.
186     /// @returns The difference of the two operands as a new TimeValue
187     /// @brief Subtraction operator.
188     friend TimeValue operator - (const TimeValue &tv1, const TimeValue &tv2);
189
190   /// @}
191   /// @name Accessors
192   /// @{
193   public:
194
195     /// Returns only the seconds component of the TimeValue. The nanoseconds
196     /// portion is ignored. No rounding is performed.
197     /// @brief Retrieve the seconds component
198     SecondsType seconds( void ) const { return seconds_; }
199
200     /// Returns only the nanoseconds component of the TimeValue. The seconds
201     /// portion is ignored. 
202     /// @brief Retrieve the nanoseconds component.
203     NanoSecondsType nanoseconds( void ) const { return nanos_; }
204
205     /// Returns only the fractional portion of the TimeValue rounded down to the
206     /// nearest microsecond (divide by one thousand).
207     /// @brief Retrieve the fractional part as microseconds;
208     uint32_t microseconds( void ) const { 
209       return nanos_ / NANOSECONDS_PER_MICROSECOND;
210     }
211
212     /// Returns only the fractional portion of the TimeValue rounded down to the 
213     /// nearest millisecond (divide by one million).
214     /// @brief Retrieve the fractional part as milliseconds;
215     uint32_t milliseconds( void ) const {
216       return nanos_ / NANOSECONDS_PER_MILLISECOND;
217     }
218
219     /// Returns the TimeValue as a number of microseconds. Note that the value
220     /// returned can overflow because the range of a uint64_t is smaller than
221     /// the range of a TimeValue. Nevertheless, this is useful on some operating
222     /// systems and is therefore provided.
223     /// @brief Convert to a number of microseconds (can overflow)
224     uint64_t usec( void ) const {
225       return seconds_ * MICROSECONDS_PER_SECOND + 
226              ( nanos_ / NANOSECONDS_PER_MICROSECOND );
227     }
228
229     /// Returns the TimeValue as a number of milliseconds. Note that the value
230     /// returned can overflow because the range of a uint64_t is smaller than 
231     /// the range of a TimeValue. Nevertheless, this is useful on some operating
232     /// systems and is therefore provided.
233     /// @brief Convert to a number of milliseconds (can overflow)
234     uint64_t msec( void ) const {
235       return seconds_ * MILLISECONDS_PER_SECOND + 
236              ( nanos_ / NANOSECONDS_PER_MILLISECOND );
237     }
238
239     /// Converts the TimeValue into the corresponding number of "ticks" for
240     /// Posix, correcting for the difference in Posix zero time.
241     /// @brief Convert to unix time (100 nanoseconds since 12:00:00a Jan 1,1970)
242     uint64_t ToPosixTime( void ) const {
243       uint64_t result = seconds_ - PosixZeroTime.seconds_;
244       result += nanos_ / NANOSECONDS_PER_POSIX_TICK;
245       return result;
246     }
247
248     /// Converts the TiemValue into the correspodning number of "ticks" for
249     /// Win32 platforms, correcting for the difference in Win32 zero time.
250     /// @brief Convert to windows time (seconds since 12:00:00a Jan 1, 1601)
251     uint64_t ToWin32Time( void ) const {
252       uint64_t result = seconds_ - Win32ZeroTime.seconds_;
253       result += nanos_ / NANOSECONDS_PER_WIN32_TICK;
254       return result;
255     }
256
257     /// Provides the seconds and nanoseconds as results in its arguments after
258     /// correction for the Posix zero time.
259     /// @brief Convert to timespec time (ala POSIX.1b)
260     void GetTimespecTime( uint64_t& seconds, uint32_t& nanos ) const {
261       seconds = seconds_ - PosixZeroTime.seconds_;
262       nanos = nanos_;
263     }
264
265   /// @}
266   /// @name Mutators
267   /// @{
268   public:
269     /// The seconds component of the TimeValue is set to \p sec without
270     /// modifying the nanoseconds part.  This is useful for whole second arithmetic.
271     /// @brief Set the seconds component.
272     void seconds (SecondsType sec ) {
273       this->seconds_ = sec;
274       this->normalize();
275     }
276
277     /// The nanoseconds component of the TimeValue is set to \p nanos without
278     /// modifying the seconds part. This is useful for basic computations
279     /// involving just the nanoseconds portion. Note that the TimeValue will be
280     /// normalized after this call so that the fractional (nanoseconds) portion
281     /// will have the smallest equivalent value.
282     /// @brief Set the nanoseconds component using a number of nanoseconds.
283     void nanoseconds ( NanoSecondsType nanos ) {
284       this->nanos_ = nanos;
285       this->normalize();
286     }
287
288     /// The seconds component remains unchanged.
289     /// @brief Set the nanoseconds component using a number of microseconds.
290     void microseconds ( int32_t micros ) {
291       this->nanos_ = micros * NANOSECONDS_PER_MICROSECOND;
292       this->normalize();
293     };
294
295     /// The seconds component remains unchanged.
296     /// @brief Set the nanoseconds component using a number of milliseconds.
297     void milliseconds ( int32_t millis ) {
298       this->nanos_ = millis * NANOSECONDS_PER_MILLISECOND;
299       this->normalize();
300     };
301
302     /// @brief Converts from microsecond format to TimeValue format
303     void usec( int64_t microseconds ) {
304       this->seconds_ = microseconds / MICROSECONDS_PER_SECOND;
305       this->nanos_ = (microseconds % MICROSECONDS_PER_SECOND) * 
306         NANOSECONDS_PER_MICROSECOND;
307       this->normalize();
308     }
309
310     /// @brief Converts from millisecond format to TimeValue format
311     void msec( int64_t milliseconds ) {
312       this->seconds_ = milliseconds / MILLISECONDS_PER_SECOND;
313       this->nanos_ = (milliseconds % MILLISECONDS_PER_SECOND) * 
314         NANOSECONDS_PER_MILLISECOND;
315       this->normalize();
316     }
317
318   /// @}
319   /// @name Implementation
320   /// @{
321   private:
322     /// This causes the values to be represented so that the fractional
323     /// part is minimized, possibly incrementing the seconds part.
324     /// @brief Normalize to canonical form.
325     void normalize (void);
326
327 /// @}
328   /// @name Data
329   /// @{
330   private:
331       /// Store the values as a <timeval>.
332       SecondsType      seconds_;///< Stores the seconds part of the TimeVal
333       NanoSecondsType  nanos_;  ///< Stores the nanoseconds part of the TimeVal
334
335   /// @}
336
337   };
338
339 inline TimeValue operator + (const TimeValue &tv1, const TimeValue &tv2) {
340   TimeValue sum (tv1.seconds_ + tv2.seconds_, tv1.nanos_ + tv2.nanos_);
341   sum.normalize ();
342   return sum;
343 }
344
345 inline TimeValue operator - (const TimeValue &tv1, const TimeValue &tv2) {
346   TimeValue difference (tv1.seconds_ - tv2.seconds_, tv1.nanos_ - tv2.nanos_ );
347   difference.normalize ();
348   return difference;
349 }
350
351 }
352 }
353
354 // vim: sw=2 smartindent smarttab tw=80 autoindent expandtab
355 #endif