Home | History | Annotate | Download | only in base
      1 // Copyright (c) 2009 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "base/time.h"
      6 #include "base/sys_string_conversions.h"
      7 #include "base/third_party/nspr/prtime.h"
      8 
      9 #include "base/logging.h"
     10 
     11 namespace base {
     12 
     13 // TimeDelta ------------------------------------------------------------------
     14 
     15 int TimeDelta::InDays() const {
     16   return static_cast<int>(delta_ / Time::kMicrosecondsPerDay);
     17 }
     18 
     19 int TimeDelta::InHours() const {
     20   return static_cast<int>(delta_ / Time::kMicrosecondsPerHour);
     21 }
     22 
     23 int TimeDelta::InMinutes() const {
     24   return static_cast<int>(delta_ / Time::kMicrosecondsPerMinute);
     25 }
     26 
     27 double TimeDelta::InSecondsF() const {
     28   return static_cast<double>(delta_) / Time::kMicrosecondsPerSecond;
     29 }
     30 
     31 int64 TimeDelta::InSeconds() const {
     32   return delta_ / Time::kMicrosecondsPerSecond;
     33 }
     34 
     35 double TimeDelta::InMillisecondsF() const {
     36   return static_cast<double>(delta_) / Time::kMicrosecondsPerMillisecond;
     37 }
     38 
     39 int64 TimeDelta::InMilliseconds() const {
     40   return delta_ / Time::kMicrosecondsPerMillisecond;
     41 }
     42 
     43 int64 TimeDelta::InMillisecondsRoundedUp() const {
     44   return (delta_ + Time::kMicrosecondsPerMillisecond - 1) /
     45       Time::kMicrosecondsPerMillisecond;
     46 }
     47 
     48 int64 TimeDelta::InMicroseconds() const {
     49   return delta_;
     50 }
     51 
     52 // Time -----------------------------------------------------------------------
     53 
     54 // static
     55 Time Time::FromTimeT(time_t tt) {
     56   if (tt == 0)
     57     return Time();  // Preserve 0 so we can tell it doesn't exist.
     58   return Time((tt * kMicrosecondsPerSecond) + kTimeTToMicrosecondsOffset);
     59 }
     60 
     61 time_t Time::ToTimeT() const {
     62   if (us_ == 0)
     63     return 0;  // Preserve 0 so we can tell it doesn't exist.
     64   return (us_ - kTimeTToMicrosecondsOffset) / kMicrosecondsPerSecond;
     65 }
     66 
     67 // static
     68 Time Time::FromDoubleT(double dt) {
     69   if (dt == 0)
     70     return Time();  // Preserve 0 so we can tell it doesn't exist.
     71   return Time(static_cast<int64>((dt *
     72                                   static_cast<double>(kMicrosecondsPerSecond)) +
     73                                  kTimeTToMicrosecondsOffset));
     74 }
     75 
     76 double Time::ToDoubleT() const {
     77   if (us_ == 0)
     78     return 0;  // Preserve 0 so we can tell it doesn't exist.
     79   return (static_cast<double>(us_ - kTimeTToMicrosecondsOffset) /
     80           static_cast<double>(kMicrosecondsPerSecond));
     81 }
     82 
     83 // static
     84 Time Time::UnixEpoch() {
     85   Time time;
     86   time.us_ = kTimeTToMicrosecondsOffset;
     87   return time;
     88 }
     89 
     90 Time Time::LocalMidnight() const {
     91   Exploded exploded;
     92   LocalExplode(&exploded);
     93   exploded.hour = 0;
     94   exploded.minute = 0;
     95   exploded.second = 0;
     96   exploded.millisecond = 0;
     97   return FromLocalExploded(exploded);
     98 }
     99 
    100 // static
    101 bool Time::FromString(const wchar_t* time_string, Time* parsed_time) {
    102   DCHECK((time_string != NULL) && (parsed_time != NULL));
    103   std::string ascii_time_string = SysWideToUTF8(time_string);
    104   if (ascii_time_string.length() == 0)
    105     return false;
    106   PRTime result_time = 0;
    107   PRStatus result = PR_ParseTimeString(ascii_time_string.c_str(), PR_FALSE,
    108                                        &result_time);
    109   if (PR_SUCCESS != result)
    110     return false;
    111   result_time += kTimeTToMicrosecondsOffset;
    112   *parsed_time = Time(result_time);
    113   return true;
    114 }
    115 
    116 // Time::Exploded -------------------------------------------------------------
    117 
    118 inline bool is_in_range(int value, int lo, int hi) {
    119   return lo <= value && value <= hi;
    120 }
    121 
    122 bool Time::Exploded::HasValidValues() const {
    123   return is_in_range(month, 1, 12) &&
    124          is_in_range(day_of_week, 0, 6) &&
    125          is_in_range(day_of_month, 1, 31) &&
    126          is_in_range(hour, 0, 23) &&
    127          is_in_range(minute, 0, 59) &&
    128          is_in_range(second, 0, 60) &&
    129          is_in_range(millisecond, 0, 999);
    130 }
    131 
    132 }  // namespace base
    133