LLVM API Documentation

TimeValue.h
Go to the documentation of this file.
00001 //===-- TimeValue.h - Declare OS TimeValue Concept --------------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 //  This header file declares the operating system TimeValue concept.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #ifndef LLVM_SUPPORT_TIMEVALUE_H
00015 #define LLVM_SUPPORT_TIMEVALUE_H
00016 
00017 #include "llvm/Support/DataTypes.h"
00018 #include <string>
00019 
00020 namespace llvm {
00021 namespace sys {
00022   /// This class is used where a precise fixed point in time is required. The
00023   /// range of TimeValue spans many hundreds of billions of years both past and
00024   /// present.  The precision of TimeValue is to the nanosecond. However, the
00025   /// actual precision of its values will be determined by the resolution of
00026   /// the system clock. The TimeValue class is used in conjunction with several
00027   /// other lib/System interfaces to specify the time at which a call should
00028   /// timeout, etc.
00029   /// @since 1.4
00030   /// @brief Provides an abstraction for a fixed point in time.
00031   class TimeValue {
00032 
00033   /// @name Constants
00034   /// @{
00035   public:
00036 
00037     /// A constant TimeValue representing the smallest time
00038     /// value permissible by the class. MinTime is some point
00039     /// in the distant past, about 300 billion years BCE.
00040     /// @brief The smallest possible time value.
00041     static TimeValue MinTime() {
00042       return TimeValue ( INT64_MIN,0 );
00043     }
00044 
00045     /// A constant TimeValue representing the largest time
00046     /// value permissible by the class. MaxTime is some point
00047     /// in the distant future, about 300 billion years AD.
00048     /// @brief The largest possible time value.
00049     static TimeValue MaxTime() {
00050       return TimeValue ( INT64_MAX,0 );
00051     }
00052 
00053     /// A constant TimeValue representing the base time,
00054     /// or zero time of 00:00:00 (midnight) January 1st, 2000.
00055     /// @brief 00:00:00 Jan 1, 2000 UTC.
00056     static TimeValue ZeroTime() {
00057       return TimeValue ( 0,0 );
00058     }
00059 
00060     /// A constant TimeValue for the Posix base time which is
00061     /// 00:00:00 (midnight) January 1st, 1970.
00062     /// @brief 00:00:00 Jan 1, 1970 UTC.
00063     static TimeValue PosixZeroTime() {
00064       return TimeValue ( PosixZeroTimeSeconds,0 );
00065     }
00066 
00067     /// A constant TimeValue for the Win32 base time which is
00068     /// 00:00:00 (midnight) January 1st, 1601.
00069     /// @brief 00:00:00 Jan 1, 1601 UTC.
00070     static TimeValue Win32ZeroTime() {
00071       return TimeValue ( Win32ZeroTimeSeconds,0 );
00072     }
00073 
00074   /// @}
00075   /// @name Types
00076   /// @{
00077   public:
00078     typedef int64_t SecondsType;    ///< Type used for representing seconds.
00079     typedef int32_t NanoSecondsType;///< Type used for representing nanoseconds.
00080 
00081     enum TimeConversions {
00082       NANOSECONDS_PER_SECOND = 1000000000,  ///< One Billion
00083       MICROSECONDS_PER_SECOND = 1000000,    ///< One Million
00084       MILLISECONDS_PER_SECOND = 1000,       ///< One Thousand
00085       NANOSECONDS_PER_MICROSECOND = 1000,   ///< One Thousand
00086       NANOSECONDS_PER_MILLISECOND = 1000000,///< One Million
00087       NANOSECONDS_PER_WIN32_TICK = 100      ///< Win32 tick is 10^7 Hz (10ns)
00088     };
00089 
00090   /// @}
00091   /// @name Constructors
00092   /// @{
00093   public:
00094     /// \brief Default construct a time value, initializing to ZeroTime.
00095     TimeValue() : seconds_(0), nanos_(0) {}
00096 
00097     /// Caller provides the exact value in seconds and nanoseconds. The
00098     /// \p nanos argument defaults to zero for convenience.
00099     /// @brief Explicit constructor
00100     explicit TimeValue (SecondsType seconds, NanoSecondsType nanos = 0)
00101       : seconds_( seconds ), nanos_( nanos ) { this->normalize(); }
00102 
00103     /// Caller provides the exact value as a double in seconds with the
00104     /// fractional part representing nanoseconds.
00105     /// @brief Double Constructor.
00106     explicit TimeValue( double new_time )
00107       : seconds_( 0 ) , nanos_ ( 0 ) {
00108       SecondsType integer_part = static_cast<SecondsType>( new_time );
00109       seconds_ = integer_part;
00110       nanos_ = static_cast<NanoSecondsType>( (new_time -
00111                static_cast<double>(integer_part)) * NANOSECONDS_PER_SECOND );
00112       this->normalize();
00113     }
00114 
00115     /// This is a static constructor that returns a TimeValue that represents
00116     /// the current time.
00117     /// @brief Creates a TimeValue with the current time (UTC).
00118     static TimeValue now();
00119 
00120   /// @}
00121   /// @name Operators
00122   /// @{
00123   public:
00124     /// Add \p that to \p this.
00125     /// @returns this
00126     /// @brief Incrementing assignment operator.
00127     TimeValue& operator += (const TimeValue& that ) {
00128       this->seconds_ += that.seconds_  ;
00129       this->nanos_ += that.nanos_ ;
00130       this->normalize();
00131       return *this;
00132     }
00133 
00134     /// Subtract \p that from \p this.
00135     /// @returns this
00136     /// @brief Decrementing assignment operator.
00137     TimeValue& operator -= (const TimeValue &that ) {
00138       this->seconds_ -= that.seconds_ ;
00139       this->nanos_ -= that.nanos_ ;
00140       this->normalize();
00141       return *this;
00142     }
00143 
00144     /// Determine if \p this is less than \p that.
00145     /// @returns True iff *this < that.
00146     /// @brief True if this < that.
00147     int operator < (const TimeValue &that) const { return that > *this; }
00148 
00149     /// Determine if \p this is greather than \p that.
00150     /// @returns True iff *this > that.
00151     /// @brief True if this > that.
00152     int operator > (const TimeValue &that) const {
00153       if ( this->seconds_ > that.seconds_ ) {
00154           return 1;
00155       } else if ( this->seconds_ == that.seconds_ ) {
00156           if ( this->nanos_ > that.nanos_ ) return 1;
00157       }
00158       return 0;
00159     }
00160 
00161     /// Determine if \p this is less than or equal to \p that.
00162     /// @returns True iff *this <= that.
00163     /// @brief True if this <= that.
00164     int operator <= (const TimeValue &that) const { return that >= *this; }
00165 
00166     /// Determine if \p this is greater than or equal to \p that.
00167     /// @returns True iff *this >= that.
00168     int operator >= (const TimeValue &that) const {
00169       if ( this->seconds_ > that.seconds_ ) {
00170           return 1;
00171       } else if ( this->seconds_ == that.seconds_ ) {
00172           if ( this->nanos_ >= that.nanos_ ) return 1;
00173       }
00174       return 0;
00175     }
00176 
00177     /// Determines if two TimeValue objects represent the same moment in time.
00178     /// @returns True iff *this == that.
00179     int operator == (const TimeValue &that) const {
00180       return (this->seconds_ == that.seconds_) &&
00181              (this->nanos_ == that.nanos_);
00182     }
00183 
00184     /// Determines if two TimeValue objects represent times that are not the
00185     /// same.
00186     /// @returns True iff *this != that.
00187     int operator != (const TimeValue &that) const { return !(*this == that); }
00188 
00189     /// Adds two TimeValue objects together.
00190     /// @returns The sum of the two operands as a new TimeValue
00191     /// @brief Addition operator.
00192     friend TimeValue operator + (const TimeValue &tv1, const TimeValue &tv2);
00193 
00194     /// Subtracts two TimeValue objects.
00195     /// @returns The difference of the two operands as a new TimeValue
00196     /// @brief Subtraction operator.
00197     friend TimeValue operator - (const TimeValue &tv1, const TimeValue &tv2);
00198 
00199   /// @}
00200   /// @name Accessors
00201   /// @{
00202   public:
00203 
00204     /// Returns only the seconds component of the TimeValue. The nanoseconds
00205     /// portion is ignored. No rounding is performed.
00206     /// @brief Retrieve the seconds component
00207     SecondsType seconds() const { return seconds_; }
00208 
00209     /// Returns only the nanoseconds component of the TimeValue. The seconds
00210     /// portion is ignored.
00211     /// @brief Retrieve the nanoseconds component.
00212     NanoSecondsType nanoseconds() const { return nanos_; }
00213 
00214     /// Returns only the fractional portion of the TimeValue rounded down to the
00215     /// nearest microsecond (divide by one thousand).
00216     /// @brief Retrieve the fractional part as microseconds;
00217     uint32_t microseconds() const {
00218       return nanos_ / NANOSECONDS_PER_MICROSECOND;
00219     }
00220 
00221     /// Returns only the fractional portion of the TimeValue rounded down to the
00222     /// nearest millisecond (divide by one million).
00223     /// @brief Retrieve the fractional part as milliseconds;
00224     uint32_t milliseconds() const {
00225       return nanos_ / NANOSECONDS_PER_MILLISECOND;
00226     }
00227 
00228     /// Returns the TimeValue as a number of microseconds. Note that the value
00229     /// returned can overflow because the range of a uint64_t is smaller than
00230     /// the range of a TimeValue. Nevertheless, this is useful on some operating
00231     /// systems and is therefore provided.
00232     /// @brief Convert to a number of microseconds (can overflow)
00233     uint64_t usec() const {
00234       return seconds_ * MICROSECONDS_PER_SECOND +
00235              ( nanos_ / NANOSECONDS_PER_MICROSECOND );
00236     }
00237 
00238     /// Returns the TimeValue as a number of milliseconds. Note that the value
00239     /// returned can overflow because the range of a uint64_t is smaller than
00240     /// the range of a TimeValue. Nevertheless, this is useful on some operating
00241     /// systems and is therefore provided.
00242     /// @brief Convert to a number of milliseconds (can overflow)
00243     uint64_t msec() const {
00244       return seconds_ * MILLISECONDS_PER_SECOND +
00245              ( nanos_ / NANOSECONDS_PER_MILLISECOND );
00246     }
00247 
00248     /// Converts the TimeValue into the corresponding number of seconds
00249     /// since the epoch (00:00:00 Jan 1,1970).
00250     uint64_t toEpochTime() const {
00251       return seconds_ - PosixZeroTimeSeconds;
00252     }
00253 
00254     /// Converts the TimeValue into the corresponding number of "ticks" for
00255     /// Win32 platforms, correcting for the difference in Win32 zero time.
00256     /// @brief Convert to Win32's FILETIME
00257     /// (100ns intervals since 00:00:00 Jan 1, 1601 UTC)
00258     uint64_t toWin32Time() const {
00259       uint64_t result = (uint64_t)10000000 * (seconds_ - Win32ZeroTimeSeconds);
00260       result += nanos_ / NANOSECONDS_PER_WIN32_TICK;
00261       return result;
00262     }
00263 
00264     /// Provides the seconds and nanoseconds as results in its arguments after
00265     /// correction for the Posix zero time.
00266     /// @brief Convert to timespec time (ala POSIX.1b)
00267     void getTimespecTime( uint64_t& seconds, uint32_t& nanos ) const {
00268       seconds = seconds_ - PosixZeroTimeSeconds;
00269       nanos = nanos_;
00270     }
00271 
00272     /// Provides conversion of the TimeValue into a readable time & date.
00273     /// @returns std::string containing the readable time value
00274     /// @brief Convert time to a string.
00275     std::string str() const;
00276 
00277   /// @}
00278   /// @name Mutators
00279   /// @{
00280   public:
00281     /// The seconds component of the TimeValue is set to \p sec without
00282     /// modifying the nanoseconds part.  This is useful for whole second
00283     /// arithmetic.
00284     /// @brief Set the seconds component.
00285     void seconds (SecondsType sec ) {
00286       this->seconds_ = sec;
00287       this->normalize();
00288     }
00289 
00290     /// The nanoseconds component of the TimeValue is set to \p nanos without
00291     /// modifying the seconds part. This is useful for basic computations
00292     /// involving just the nanoseconds portion. Note that the TimeValue will be
00293     /// normalized after this call so that the fractional (nanoseconds) portion
00294     /// will have the smallest equivalent value.
00295     /// @brief Set the nanoseconds component using a number of nanoseconds.
00296     void nanoseconds ( NanoSecondsType nanos ) {
00297       this->nanos_ = nanos;
00298       this->normalize();
00299     }
00300 
00301     /// The seconds component remains unchanged.
00302     /// @brief Set the nanoseconds component using a number of microseconds.
00303     void microseconds ( int32_t micros ) {
00304       this->nanos_ = micros * NANOSECONDS_PER_MICROSECOND;
00305       this->normalize();
00306     }
00307 
00308     /// The seconds component remains unchanged.
00309     /// @brief Set the nanoseconds component using a number of milliseconds.
00310     void milliseconds ( int32_t millis ) {
00311       this->nanos_ = millis * NANOSECONDS_PER_MILLISECOND;
00312       this->normalize();
00313     }
00314 
00315     /// @brief Converts from microsecond format to TimeValue format
00316     void usec( int64_t microseconds ) {
00317       this->seconds_ = microseconds / MICROSECONDS_PER_SECOND;
00318       this->nanos_ = NanoSecondsType(microseconds % MICROSECONDS_PER_SECOND) *
00319         NANOSECONDS_PER_MICROSECOND;
00320       this->normalize();
00321     }
00322 
00323     /// @brief Converts from millisecond format to TimeValue format
00324     void msec( int64_t milliseconds ) {
00325       this->seconds_ = milliseconds / MILLISECONDS_PER_SECOND;
00326       this->nanos_ = NanoSecondsType(milliseconds % MILLISECONDS_PER_SECOND) *
00327         NANOSECONDS_PER_MILLISECOND;
00328       this->normalize();
00329     }
00330 
00331     /// Converts the \p seconds argument from PosixTime to the corresponding
00332     /// TimeValue and assigns that value to \p this.
00333     /// @brief Convert seconds form PosixTime to TimeValue
00334     void fromEpochTime( SecondsType seconds ) {
00335       seconds_ = seconds + PosixZeroTimeSeconds;
00336       nanos_ = 0;
00337       this->normalize();
00338     }
00339 
00340     /// Converts the \p win32Time argument from Windows FILETIME to the
00341     /// corresponding TimeValue and assigns that value to \p this.
00342     /// @brief Convert seconds form Windows FILETIME to TimeValue
00343     void fromWin32Time( uint64_t win32Time ) {
00344       this->seconds_ = win32Time / 10000000 + Win32ZeroTimeSeconds;
00345       this->nanos_ = NanoSecondsType(win32Time  % 10000000) * 100;
00346     }
00347 
00348   /// @}
00349   /// @name Implementation
00350   /// @{
00351   private:
00352     /// This causes the values to be represented so that the fractional
00353     /// part is minimized, possibly incrementing the seconds part.
00354     /// @brief Normalize to canonical form.
00355     void normalize();
00356 
00357   /// @}
00358   /// @name Data
00359   /// @{
00360   private:
00361     /// Store the values as a <timeval>.
00362     SecondsType      seconds_;///< Stores the seconds part of the TimeVal
00363     NanoSecondsType  nanos_;  ///< Stores the nanoseconds part of the TimeVal
00364 
00365     static const SecondsType PosixZeroTimeSeconds;
00366     static const SecondsType Win32ZeroTimeSeconds;
00367   /// @}
00368 
00369   };
00370 
00371 inline TimeValue operator + (const TimeValue &tv1, const TimeValue &tv2) {
00372   TimeValue sum (tv1.seconds_ + tv2.seconds_, tv1.nanos_ + tv2.nanos_);
00373   sum.normalize ();
00374   return sum;
00375 }
00376 
00377 inline TimeValue operator - (const TimeValue &tv1, const TimeValue &tv2) {
00378   TimeValue difference (tv1.seconds_ - tv2.seconds_, tv1.nanos_ - tv2.nanos_ );
00379   difference.normalize ();
00380   return difference;
00381 }
00382 
00383 }
00384 }
00385 
00386 #endif