TimeValue.h 14 KB

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