You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

89 lines
3.2 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2005 Google Inc.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  19. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  22. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  23. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  24. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #ifndef TALK_BASE_TIMEUTILS_H_
  28. #define TALK_BASE_TIMEUTILS_H_
  29. #ifndef WIN32
  30. #include <time.h>
  31. #endif
  32. #include "talk/base/basictypes.h"
  33. namespace talk_base {
  34. static const int64 kNumMillisecsPerSec = 1000;
  35. static const int64 kNumMicrosecsPerSec = 1000000;
  36. static const int64 kNumNanosecsPerSec = 1000000000;
  37. static const int64 kNumMicrosecsPerMillisec = kNumMicrosecsPerSec /
  38. kNumMillisecsPerSec;
  39. static const int64 kNumNanosecsPerMillisec = kNumNanosecsPerSec /
  40. kNumMillisecsPerSec;
  41. typedef uint32 TimeStamp;
  42. // Returns the current time in milliseconds.
  43. uint32 Time();
  44. // Returns the current time in nanoseconds.
  45. uint64 TimeNanos();
  46. // Returns a future timestamp, 'elapsed' milliseconds from now.
  47. uint32 TimeAfter(int32 elapsed);
  48. // Comparisons between time values, which can wrap around.
  49. bool TimeIsBetween(uint32 earlier, uint32 middle, uint32 later); // Inclusive
  50. bool TimeIsLaterOrEqual(uint32 earlier, uint32 later); // Inclusive
  51. bool TimeIsLater(uint32 earlier, uint32 later); // Exclusive
  52. // Returns the later of two timestamps.
  53. inline uint32 TimeMax(uint32 ts1, uint32 ts2) {
  54. return TimeIsLaterOrEqual(ts1, ts2) ? ts2 : ts1;
  55. }
  56. // Returns the earlier of two timestamps.
  57. inline uint32 TimeMin(uint32 ts1, uint32 ts2) {
  58. return TimeIsLaterOrEqual(ts1, ts2) ? ts1 : ts2;
  59. }
  60. // Number of milliseconds that would elapse between 'earlier' and 'later'
  61. // timestamps. The value is negative if 'later' occurs before 'earlier'.
  62. int32 TimeDiff(uint32 later, uint32 earlier);
  63. // The number of milliseconds that have elapsed since 'earlier'.
  64. inline int32 TimeSince(uint32 earlier) {
  65. return TimeDiff(Time(), earlier);
  66. }
  67. // The number of milliseconds that will elapse between now and 'later'.
  68. inline int32 TimeUntil(uint32 later) {
  69. return TimeDiff(later, Time());
  70. }
  71. } // namespace talk_base
  72. #endif // TALK_BASE_TIMEUTILS_H_