OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Time represents an absolute point in time, internally represented as | 5 // Time represents an absolute point in time, internally represented as |
6 // microseconds (s/1,000,000) since the Windows epoch (1601-01-01 00:00:00 UTC) | 6 // microseconds (s/1,000,000) since the Windows epoch (1601-01-01 00:00:00 UTC) |
7 // (See http://crbug.com/14734). System-dependent clock interface routines are | 7 // (See http://crbug.com/14734). System-dependent clock interface routines are |
8 // defined in time_PLATFORM.cc. | 8 // defined in time_PLATFORM.cc. |
9 // | 9 // |
10 // TimeDelta represents a duration of time, internally represented in | 10 // TimeDelta represents a duration of time, internally represented in |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
54 class TimeTicks; | 54 class TimeTicks; |
55 | 55 |
56 // TimeDelta ------------------------------------------------------------------ | 56 // TimeDelta ------------------------------------------------------------------ |
57 | 57 |
58 class BASE_EXPORT TimeDelta { | 58 class BASE_EXPORT TimeDelta { |
59 public: | 59 public: |
60 TimeDelta() : delta_(0) { | 60 TimeDelta() : delta_(0) { |
61 } | 61 } |
62 | 62 |
63 // Converts units of time to TimeDeltas. | 63 // Converts units of time to TimeDeltas. |
64 static TimeDelta FromDays(int days); | 64 static TimeDelta FromDays(int64 days); |
65 static TimeDelta FromHours(int hours); | 65 static TimeDelta FromHours(int64 hours); |
66 static TimeDelta FromMinutes(int minutes); | 66 static TimeDelta FromMinutes(int64 minutes); |
67 static TimeDelta FromSeconds(int64 secs); | 67 static TimeDelta FromSeconds(int64 secs); |
68 static TimeDelta FromMilliseconds(int64 ms); | 68 static TimeDelta FromMilliseconds(int64 ms); |
69 static TimeDelta FromMicroseconds(int64 us); | 69 static TimeDelta FromMicroseconds(int64 us); |
70 #if defined(OS_WIN) | 70 #if defined(OS_WIN) |
71 static TimeDelta FromQPCValue(LONGLONG qpc_value); | 71 static TimeDelta FromQPCValue(LONGLONG qpc_value); |
72 #endif | 72 #endif |
73 | 73 |
74 // Converts an integer value representing TimeDelta to a class. This is used | 74 // Converts an integer value representing TimeDelta to a class. This is used |
75 // when deserializing a |TimeDelta| structure, using a value known to be | 75 // when deserializing a |TimeDelta| structure, using a value known to be |
76 // compatible. It is not provided as a constructor because the integer type | 76 // compatible. It is not provided as a constructor because the integer type |
77 // may be unclear from the perspective of a caller. | 77 // may be unclear from the perspective of a caller. |
78 static TimeDelta FromInternalValue(int64 delta) { | 78 static TimeDelta FromInternalValue(int64 delta) { |
79 return TimeDelta(delta); | 79 return TimeDelta(delta); |
80 } | 80 } |
81 | 81 |
82 // Returns the maximum time delta, which should be greater than any reasonable | |
83 // time delta we might compare it to. Adding or subtracting the maximum time | |
84 // delta to a time or another time delta has an undefined result. | |
85 static TimeDelta Max(); | |
86 | |
87 // Returns the internal numeric value of the TimeDelta object. Please don't | 82 // Returns the internal numeric value of the TimeDelta object. Please don't |
88 // use this and do arithmetic on it, as it is more error prone than using the | 83 // use this and do arithmetic on it, as it is more error prone than using the |
89 // provided operators. | 84 // provided operators. |
90 // For serializing, use FromInternalValue to reconstitute. | 85 // For serializing, use FromInternalValue to reconstitute. |
91 int64 ToInternalValue() const { | 86 int64 ToInternalValue() const { |
92 return delta_; | 87 return delta_; |
93 } | 88 } |
94 | 89 |
95 // Returns true if the time delta is the maximum time delta. | |
96 bool is_max() const { | |
97 return delta_ == std::numeric_limits<int64>::max(); | |
98 } | |
99 | |
100 #if defined(OS_POSIX) | 90 #if defined(OS_POSIX) |
101 struct timespec ToTimeSpec() const; | 91 struct timespec ToTimeSpec() const; |
102 #endif | 92 #endif |
103 | 93 |
104 // Returns the time delta in some unit. The F versions return a floating | 94 // Returns the time delta in some unit. The F versions return a floating |
105 // point value, the "regular" versions return a rounded-down value. | 95 // point value, the "regular" versions return a rounded-down value. |
106 // | 96 // |
107 // InMillisecondsRoundedUp() instead returns an integer that is rounded up | 97 // InMillisecondsRoundedUp() instead returns an integer that is rounded up |
108 // to the next full millisecond. | 98 // to the next full millisecond. |
109 int InDays() const; | 99 int InDays() const; |
(...skipping 386 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
496 static int high_resolution_timer_activated_; | 486 static int high_resolution_timer_activated_; |
497 #endif | 487 #endif |
498 | 488 |
499 // Time in microseconds in UTC. | 489 // Time in microseconds in UTC. |
500 int64 us_; | 490 int64 us_; |
501 }; | 491 }; |
502 | 492 |
503 // Inline the TimeDelta factory methods, for fast TimeDelta construction. | 493 // Inline the TimeDelta factory methods, for fast TimeDelta construction. |
504 | 494 |
505 // static | 495 // static |
506 inline TimeDelta TimeDelta::FromDays(int days) { | 496 inline TimeDelta TimeDelta::FromDays(int64 days) { |
507 // Preserve max to prevent overflow. | |
508 if (days == std::numeric_limits<int>::max()) | |
509 return Max(); | |
510 return TimeDelta(days * Time::kMicrosecondsPerDay); | 497 return TimeDelta(days * Time::kMicrosecondsPerDay); |
511 } | 498 } |
512 | 499 |
513 // static | 500 // static |
514 inline TimeDelta TimeDelta::FromHours(int hours) { | 501 inline TimeDelta TimeDelta::FromHours(int64 hours) { |
515 // Preserve max to prevent overflow. | |
516 if (hours == std::numeric_limits<int>::max()) | |
517 return Max(); | |
518 return TimeDelta(hours * Time::kMicrosecondsPerHour); | 502 return TimeDelta(hours * Time::kMicrosecondsPerHour); |
519 } | 503 } |
520 | 504 |
521 // static | 505 // static |
522 inline TimeDelta TimeDelta::FromMinutes(int minutes) { | 506 inline TimeDelta TimeDelta::FromMinutes(int64 minutes) { |
523 // Preserve max to prevent overflow. | |
524 if (minutes == std::numeric_limits<int>::max()) | |
525 return Max(); | |
526 return TimeDelta(minutes * Time::kMicrosecondsPerMinute); | 507 return TimeDelta(minutes * Time::kMicrosecondsPerMinute); |
527 } | 508 } |
528 | 509 |
529 // static | 510 // static |
530 inline TimeDelta TimeDelta::FromSeconds(int64 secs) { | 511 inline TimeDelta TimeDelta::FromSeconds(int64 secs) { |
531 // Preserve max to prevent overflow. | |
532 if (secs == std::numeric_limits<int64>::max()) | |
533 return Max(); | |
534 return TimeDelta(secs * Time::kMicrosecondsPerSecond); | 512 return TimeDelta(secs * Time::kMicrosecondsPerSecond); |
535 } | 513 } |
536 | 514 |
537 // static | 515 // static |
538 inline TimeDelta TimeDelta::FromMilliseconds(int64 ms) { | 516 inline TimeDelta TimeDelta::FromMilliseconds(int64 ms) { |
539 // Preserve max to prevent overflow. | |
540 if (ms == std::numeric_limits<int64>::max()) | |
541 return Max(); | |
542 return TimeDelta(ms * Time::kMicrosecondsPerMillisecond); | 517 return TimeDelta(ms * Time::kMicrosecondsPerMillisecond); |
543 } | 518 } |
544 | 519 |
545 // static | 520 // static |
546 inline TimeDelta TimeDelta::FromMicroseconds(int64 us) { | 521 inline TimeDelta TimeDelta::FromMicroseconds(int64 us) { |
547 // Preserve max to prevent overflow. | |
548 if (us == std::numeric_limits<int64>::max()) | |
549 return Max(); | |
550 return TimeDelta(us); | 522 return TimeDelta(us); |
551 } | 523 } |
552 | 524 |
553 inline Time TimeDelta::operator+(Time t) const { | 525 inline Time TimeDelta::operator+(Time t) const { |
554 return Time(t.us_ + delta_); | 526 return Time(t.us_ + delta_); |
555 } | 527 } |
556 | 528 |
557 // TimeTicks ------------------------------------------------------------------ | 529 // TimeTicks ------------------------------------------------------------------ |
558 | 530 |
559 class BASE_EXPORT TimeTicks { | 531 class BASE_EXPORT TimeTicks { |
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
712 #endif | 684 #endif |
713 }; | 685 }; |
714 | 686 |
715 inline TimeTicks TimeDelta::operator+(TimeTicks t) const { | 687 inline TimeTicks TimeDelta::operator+(TimeTicks t) const { |
716 return TimeTicks(t.ticks_ + delta_); | 688 return TimeTicks(t.ticks_ + delta_); |
717 } | 689 } |
718 | 690 |
719 } // namespace base | 691 } // namespace base |
720 | 692 |
721 #endif // BASE_TIME_TIME_H_ | 693 #endif // BASE_TIME_TIME_H_ |
OLD | NEW |