ipc/chromium/src/base/time.cc

Wed, 31 Dec 2014 06:55:46 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:55:46 +0100
changeset 1
ca08bd8f51b2
permissions
-rw-r--r--

Added tag TORBROWSER_REPLICA for changeset 6474c204b198

     1 // Copyright (c) 2006-2008 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.
     5 #include "base/time.h"
     6 #include "base/string_util.h"
     7 #include "base/sys_string_conversions.h"
     8 #include "prtime.h"
    10 #include "base/logging.h"
    12 namespace base {
    14 // TimeDelta ------------------------------------------------------------------
    16 int TimeDelta::InDays() const {
    17   return static_cast<int>(delta_ / Time::kMicrosecondsPerDay);
    18 }
    20 int TimeDelta::InHours() const {
    21   return static_cast<int>(delta_ / Time::kMicrosecondsPerHour);
    22 }
    24 int TimeDelta::InMinutes() const {
    25   return static_cast<int>(delta_ / Time::kMicrosecondsPerMinute);
    26 }
    28 double TimeDelta::InSecondsF() const {
    29   return static_cast<double>(delta_) / Time::kMicrosecondsPerSecond;
    30 }
    32 int64_t TimeDelta::InSeconds() const {
    33   return delta_ / Time::kMicrosecondsPerSecond;
    34 }
    36 double TimeDelta::InMillisecondsF() const {
    37   return static_cast<double>(delta_) / Time::kMicrosecondsPerMillisecond;
    38 }
    40 int64_t TimeDelta::InMilliseconds() const {
    41   return delta_ / Time::kMicrosecondsPerMillisecond;
    42 }
    44 int64_t TimeDelta::InMicroseconds() const {
    45   return delta_;
    46 }
    48 // Time -----------------------------------------------------------------------
    50 // static
    51 Time Time::FromTimeT(time_t tt) {
    52   if (tt == 0)
    53     return Time();  // Preserve 0 so we can tell it doesn't exist.
    54   return (tt * kMicrosecondsPerSecond) + kTimeTToMicrosecondsOffset;
    55 }
    57 time_t Time::ToTimeT() const {
    58   if (us_ == 0)
    59     return 0;  // Preserve 0 so we can tell it doesn't exist.
    60   return (us_ - kTimeTToMicrosecondsOffset) / kMicrosecondsPerSecond;
    61 }
    63 // static
    64 Time Time::FromDoubleT(double dt) {
    65   return (dt * static_cast<double>(kMicrosecondsPerSecond)) +
    66       kTimeTToMicrosecondsOffset;
    67 }
    69 double Time::ToDoubleT() const {
    70   if (us_ == 0)
    71     return 0;  // Preserve 0 so we can tell it doesn't exist.
    72   return (static_cast<double>(us_ - kTimeTToMicrosecondsOffset) /
    73           static_cast<double>(kMicrosecondsPerSecond));
    74 }
    76 Time Time::LocalMidnight() const {
    77   Exploded exploded;
    78   LocalExplode(&exploded);
    79   exploded.hour = 0;
    80   exploded.minute = 0;
    81   exploded.second = 0;
    82   exploded.millisecond = 0;
    83   return FromLocalExploded(exploded);
    84 }
    86 // static
    87 bool Time::FromString(const wchar_t* time_string, Time* parsed_time) {
    88   DCHECK((time_string != NULL) && (parsed_time != NULL));
    89   std::string ascii_time_string = SysWideToUTF8(time_string);
    90   if (ascii_time_string.length() == 0)
    91     return false;
    92   PRTime result_time = 0;
    93   PRStatus result = PR_ParseTimeString(ascii_time_string.c_str(), PR_FALSE,
    94                                        &result_time);
    95   if (PR_SUCCESS != result)
    96     return false;
    97   result_time += kTimeTToMicrosecondsOffset;
    98   *parsed_time = Time(result_time);
    99   return true;
   100 }
   102 }  // namespace base

mercurial