Chromium Code Reviews| 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(FromExploded(false, exploded, &time)); | |
|
Mark Mentovai
2016/05/26 14:08:39
Centralize the “false”/“true”. This should be igno
| |
| 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(FromExploded(true, 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 failure. For example, | |
| 538 // FromExploded can fail when 31st of a month is set on a 28-30 day month, | |
| 539 // which results in 1st day of the next month. | |
|
Mark Mentovai
2016/05/26 14:08:39
Nuke “which results in 1st day of the next month.”
| |
| 540 static bool FromUTCExploded(const Exploded& exploded, | |
| 541 Time* time) WARN_UNUSED_RESULT { | |
| 542 return FromExploded(false, exploded, time); | |
| 543 } | |
| 544 static bool FromLocalExploded(const Exploded& exploded, | |
| 545 Time* time) WARN_UNUSED_RESULT { | |
| 546 return FromExploded(true, exploded, time); | |
| 527 } | 547 } |
| 528 | 548 |
| 529 // Converts a string representation of time to a Time object. | 549 // Converts a string representation of time to a Time object. |
| 530 // An example of a time string which is converted is as below:- | 550 // 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 | 551 // "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 | 552 // 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 | 553 // 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. | 554 // specified in RFC822) is treated as if the timezone is not specified. |
| 535 // TODO(iyengar) Move the FromString/FromTimeT/ToTimeT/FromFileTime to | 555 // TODO(iyengar) Move the FromString/FromTimeT/ToTimeT/FromFileTime to |
| 536 // a new time converter class. | 556 // a new time converter class. |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 557 private: | 577 private: |
| 558 friend class time_internal::TimeBase<Time>; | 578 friend class time_internal::TimeBase<Time>; |
| 559 | 579 |
| 560 explicit Time(int64_t us) : TimeBase(us) {} | 580 explicit Time(int64_t us) : TimeBase(us) {} |
| 561 | 581 |
| 562 // Explodes the given time to either local time |is_local = true| or UTC | 582 // Explodes the given time to either local time |is_local = true| or UTC |
| 563 // |is_local = false|. | 583 // |is_local = false|. |
| 564 void Explode(bool is_local, Exploded* exploded) const; | 584 void Explode(bool is_local, Exploded* exploded) const; |
| 565 | 585 |
| 566 // Unexplodes a given time assuming the source is either local time | 586 // Unexplodes a given time assuming the source is either local time |
| 567 // |is_local = true| or UTC |is_local = false|. | 587 // |is_local = true| or UTC |is_local = false|. Function returns false on |
| 568 static Time FromExploded(bool is_local, const Exploded& exploded); | 588 // failure and sets |time| to Time(0). Otherwise returns true and sets |time| |
| 589 // to non-exploded time. | |
| 590 static bool FromExploded(bool is_local, | |
| 591 const Exploded& exploded, | |
| 592 Time* time) WARN_UNUSED_RESULT; | |
| 569 | 593 |
| 570 // Converts a string representation of time to a Time object. | 594 // Converts a string representation of time to a Time object. |
| 571 // An example of a time string which is converted is as below:- | 595 // 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 | 596 // "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 | 597 // in the input string, local time |is_local = true| or |
| 574 // UTC |is_local = false| is assumed. A timezone that cannot be parsed | 598 // 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 | 599 // (e.g. "UTC" which is not specified in RFC822) is treated as if the |
| 576 // timezone is not specified. | 600 // timezone is not specified. |
| 577 static bool FromStringInternal(const char* time_string, | 601 static bool FromStringInternal(const char* time_string, |
| 578 bool is_local, | 602 bool is_local, |
| 579 Time* parsed_time); | 603 Time* parsed_time); |
| 604 | |
| 605 // Comparison does not consider |day_of_week| when doing the operation. | |
| 606 static bool ExplodedMostlyEquals(const Exploded& lhs, const Exploded& rhs); | |
|
Mark Mentovai
2016/05/26 14:08:39
Not sure why this isn’t a non-static MostlyEquals(
mmenke
2016/05/26 14:45:08
If we put it there, we'd either need to make it pu
maksims (do not use this acc)
2016/05/27 10:19:08
Yes, exactly. Should I change it to be a friend th
| |
| 580 }; | 607 }; |
| 581 | 608 |
| 582 // static | 609 // static |
| 583 constexpr TimeDelta TimeDelta::FromDays(int days) { | 610 constexpr TimeDelta TimeDelta::FromDays(int days) { |
| 584 return days == std::numeric_limits<int>::max() | 611 return days == std::numeric_limits<int>::max() |
| 585 ? Max() | 612 ? Max() |
| 586 : TimeDelta(days * Time::kMicrosecondsPerDay); | 613 : TimeDelta(days * Time::kMicrosecondsPerDay); |
| 587 } | 614 } |
| 588 | 615 |
| 589 // static | 616 // static |
| (...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 789 static void WaitUntilInitializedWin(); | 816 static void WaitUntilInitializedWin(); |
| 790 #endif | 817 #endif |
| 791 }; | 818 }; |
| 792 | 819 |
| 793 // For logging use only. | 820 // For logging use only. |
| 794 BASE_EXPORT std::ostream& operator<<(std::ostream& os, ThreadTicks time_ticks); | 821 BASE_EXPORT std::ostream& operator<<(std::ostream& os, ThreadTicks time_ticks); |
| 795 | 822 |
| 796 } // namespace base | 823 } // namespace base |
| 797 | 824 |
| 798 #endif // BASE_TIME_TIME_H_ | 825 #endif // BASE_TIME_TIME_H_ |
| OLD | NEW |