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 coordinated universal time (UTC), | 5 // Time represents an absolute point in coordinated universal time (UTC), |
6 // internally represented as microseconds (s/1,000,000) since the Windows epoch | 6 // internally represented as microseconds (s/1,000,000) since the Windows epoch |
7 // (1601-01-01 00:00:00 UTC). System-dependent clock interface routines are | 7 // (1601-01-01 00:00:00 UTC). System-dependent clock interface routines are |
8 // defined in time_PLATFORM.cc. Note that values for Time may skew and jump | 8 // defined in time_PLATFORM.cc. Note that values for Time may skew and jump |
9 // around as the operating system makes adjustments to synchronize (e.g., with | 9 // around as the operating system makes adjustments to synchronize (e.g., with |
10 // NTP servers). Thus, client code that uses the Time class must account for | 10 // NTP servers). Thus, client code that uses the Time class must account for |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
49 #ifndef BASE_TIME_TIME_H_ | 49 #ifndef BASE_TIME_TIME_H_ |
50 #define BASE_TIME_TIME_H_ | 50 #define BASE_TIME_TIME_H_ |
51 | 51 |
52 #include <stdint.h> | 52 #include <stdint.h> |
53 #include <time.h> | 53 #include <time.h> |
54 | 54 |
55 #include <iosfwd> | 55 #include <iosfwd> |
56 #include <limits> | 56 #include <limits> |
57 | 57 |
58 #include "base/base_export.h" | 58 #include "base/base_export.h" |
| 59 #include "base/compiler_specific.h" |
59 #include "base/numerics/safe_math.h" | 60 #include "base/numerics/safe_math.h" |
60 #include "build/build_config.h" | 61 #include "build/build_config.h" |
61 | 62 |
62 #if defined(OS_MACOSX) | 63 #if defined(OS_MACOSX) |
63 #include <CoreFoundation/CoreFoundation.h> | 64 #include <CoreFoundation/CoreFoundation.h> |
64 // Avoid Mac system header macro leak. | 65 // Avoid Mac system header macro leak. |
65 #undef TYPE_BOOL | 66 #undef TYPE_BOOL |
66 #endif | 67 #endif |
67 | 68 |
68 #if defined(OS_POSIX) | 69 #if defined(OS_POSIX) |
(...skipping 443 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
512 static bool ActivateHighResolutionTimer(bool activate); | 513 static bool ActivateHighResolutionTimer(bool activate); |
513 | 514 |
514 // Returns true if the high resolution timer is both enabled and activated. | 515 // Returns true if the high resolution timer is both enabled and activated. |
515 // This is provided for testing only, and is not tracked in a thread-safe | 516 // This is provided for testing only, and is not tracked in a thread-safe |
516 // way. | 517 // way. |
517 static bool IsHighResolutionTimerInUse(); | 518 static bool IsHighResolutionTimerInUse(); |
518 #endif | 519 #endif |
519 | 520 |
520 // Converts an exploded structure representing either the local time or UTC | 521 // Converts an exploded structure representing either the local time or UTC |
521 // into a Time class. | 522 // into a Time class. |
| 523 // TODO(maksims): Get rid of these in favor of the methods below when |
| 524 // all the callers stop using these ones. |
522 static Time FromUTCExploded(const Exploded& exploded) { | 525 static Time FromUTCExploded(const Exploded& exploded) { |
523 return FromExploded(false, exploded); | 526 base::Time time; |
| 527 ignore_result(FromUTCExploded(exploded, &time)); |
| 528 return time; |
524 } | 529 } |
525 static Time FromLocalExploded(const Exploded& exploded) { | 530 static Time FromLocalExploded(const Exploded& exploded) { |
526 return FromExploded(true, exploded); | 531 base::Time time; |
| 532 ignore_result(FromLocalExploded(exploded, &time)); |
| 533 return time; |
| 534 } |
| 535 |
| 536 // Converts an exploded structure representing either the local time or UTC |
| 537 // into a Time class. Returns false on a failure when, for example, a day of |
| 538 // month is set to 31 on a 28-30 day month. |
| 539 static bool FromUTCExploded(const Exploded& exploded, |
| 540 Time* time) WARN_UNUSED_RESULT { |
| 541 return FromExploded(false, exploded, time); |
| 542 } |
| 543 static bool FromLocalExploded(const Exploded& exploded, |
| 544 Time* time) WARN_UNUSED_RESULT { |
| 545 return FromExploded(true, exploded, time); |
527 } | 546 } |
528 | 547 |
529 // Converts a string representation of time to a Time object. | 548 // Converts a string representation of time to a Time object. |
530 // An example of a time string which is converted is as below:- | 549 // An example of a time string which is converted is as below:- |
531 // "Tue, 15 Nov 1994 12:45:26 GMT". If the timezone is not specified | 550 // "Tue, 15 Nov 1994 12:45:26 GMT". If the timezone is not specified |
532 // in the input string, FromString assumes local time and FromUTCString | 551 // in the input string, FromString assumes local time and FromUTCString |
533 // assumes UTC. A timezone that cannot be parsed (e.g. "UTC" which is not | 552 // assumes UTC. A timezone that cannot be parsed (e.g. "UTC" which is not |
534 // specified in RFC822) is treated as if the timezone is not specified. | 553 // specified in RFC822) is treated as if the timezone is not specified. |
535 // TODO(iyengar) Move the FromString/FromTimeT/ToTimeT/FromFileTime to | 554 // TODO(iyengar) Move the FromString/FromTimeT/ToTimeT/FromFileTime to |
536 // a new time converter class. | 555 // a new time converter class. |
(...skipping 20 matching lines...) Expand all Loading... |
557 private: | 576 private: |
558 friend class time_internal::TimeBase<Time>; | 577 friend class time_internal::TimeBase<Time>; |
559 | 578 |
560 explicit Time(int64_t us) : TimeBase(us) {} | 579 explicit Time(int64_t us) : TimeBase(us) {} |
561 | 580 |
562 // Explodes the given time to either local time |is_local = true| or UTC | 581 // Explodes the given time to either local time |is_local = true| or UTC |
563 // |is_local = false|. | 582 // |is_local = false|. |
564 void Explode(bool is_local, Exploded* exploded) const; | 583 void Explode(bool is_local, Exploded* exploded) const; |
565 | 584 |
566 // Unexplodes a given time assuming the source is either local time | 585 // Unexplodes a given time assuming the source is either local time |
567 // |is_local = true| or UTC |is_local = false|. | 586 // |is_local = true| or UTC |is_local = false|. Function returns false on |
568 static Time FromExploded(bool is_local, const Exploded& exploded); | 587 // failure and sets |time| to Time(0). Otherwise returns true and sets |time| |
| 588 // to non-exploded time. |
| 589 static bool FromExploded(bool is_local, |
| 590 const Exploded& exploded, |
| 591 Time* time) WARN_UNUSED_RESULT; |
569 | 592 |
570 // Converts a string representation of time to a Time object. | 593 // Converts a string representation of time to a Time object. |
571 // An example of a time string which is converted is as below:- | 594 // An example of a time string which is converted is as below:- |
572 // "Tue, 15 Nov 1994 12:45:26 GMT". If the timezone is not specified | 595 // "Tue, 15 Nov 1994 12:45:26 GMT". If the timezone is not specified |
573 // in the input string, local time |is_local = true| or | 596 // in the input string, local time |is_local = true| or |
574 // UTC |is_local = false| is assumed. A timezone that cannot be parsed | 597 // UTC |is_local = false| is assumed. A timezone that cannot be parsed |
575 // (e.g. "UTC" which is not specified in RFC822) is treated as if the | 598 // (e.g. "UTC" which is not specified in RFC822) is treated as if the |
576 // timezone is not specified. | 599 // timezone is not specified. |
577 static bool FromStringInternal(const char* time_string, | 600 static bool FromStringInternal(const char* time_string, |
578 bool is_local, | 601 bool is_local, |
579 Time* parsed_time); | 602 Time* parsed_time); |
| 603 |
| 604 // Comparison does not consider |day_of_week| when doing the operation. |
| 605 static bool ExplodedMostlyEquals(const Exploded& lhs, const Exploded& rhs); |
580 }; | 606 }; |
581 | 607 |
582 // static | 608 // static |
583 constexpr TimeDelta TimeDelta::FromDays(int days) { | 609 constexpr TimeDelta TimeDelta::FromDays(int days) { |
584 return days == std::numeric_limits<int>::max() | 610 return days == std::numeric_limits<int>::max() |
585 ? Max() | 611 ? Max() |
586 : TimeDelta(days * Time::kMicrosecondsPerDay); | 612 : TimeDelta(days * Time::kMicrosecondsPerDay); |
587 } | 613 } |
588 | 614 |
589 // static | 615 // static |
(...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
789 static void WaitUntilInitializedWin(); | 815 static void WaitUntilInitializedWin(); |
790 #endif | 816 #endif |
791 }; | 817 }; |
792 | 818 |
793 // For logging use only. | 819 // For logging use only. |
794 BASE_EXPORT std::ostream& operator<<(std::ostream& os, ThreadTicks time_ticks); | 820 BASE_EXPORT std::ostream& operator<<(std::ostream& os, ThreadTicks time_ticks); |
795 | 821 |
796 } // namespace base | 822 } // namespace base |
797 | 823 |
798 #endif // BASE_TIME_TIME_H_ | 824 #endif // BASE_TIME_TIME_H_ |
OLD | NEW |