ipc/chromium/src/base/time.cc

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
michael@0 2 // Use of this source code is governed by a BSD-style license that can be
michael@0 3 // found in the LICENSE file.
michael@0 4
michael@0 5 #include "base/time.h"
michael@0 6 #include "base/string_util.h"
michael@0 7 #include "base/sys_string_conversions.h"
michael@0 8 #include "prtime.h"
michael@0 9
michael@0 10 #include "base/logging.h"
michael@0 11
michael@0 12 namespace base {
michael@0 13
michael@0 14 // TimeDelta ------------------------------------------------------------------
michael@0 15
michael@0 16 int TimeDelta::InDays() const {
michael@0 17 return static_cast<int>(delta_ / Time::kMicrosecondsPerDay);
michael@0 18 }
michael@0 19
michael@0 20 int TimeDelta::InHours() const {
michael@0 21 return static_cast<int>(delta_ / Time::kMicrosecondsPerHour);
michael@0 22 }
michael@0 23
michael@0 24 int TimeDelta::InMinutes() const {
michael@0 25 return static_cast<int>(delta_ / Time::kMicrosecondsPerMinute);
michael@0 26 }
michael@0 27
michael@0 28 double TimeDelta::InSecondsF() const {
michael@0 29 return static_cast<double>(delta_) / Time::kMicrosecondsPerSecond;
michael@0 30 }
michael@0 31
michael@0 32 int64_t TimeDelta::InSeconds() const {
michael@0 33 return delta_ / Time::kMicrosecondsPerSecond;
michael@0 34 }
michael@0 35
michael@0 36 double TimeDelta::InMillisecondsF() const {
michael@0 37 return static_cast<double>(delta_) / Time::kMicrosecondsPerMillisecond;
michael@0 38 }
michael@0 39
michael@0 40 int64_t TimeDelta::InMilliseconds() const {
michael@0 41 return delta_ / Time::kMicrosecondsPerMillisecond;
michael@0 42 }
michael@0 43
michael@0 44 int64_t TimeDelta::InMicroseconds() const {
michael@0 45 return delta_;
michael@0 46 }
michael@0 47
michael@0 48 // Time -----------------------------------------------------------------------
michael@0 49
michael@0 50 // static
michael@0 51 Time Time::FromTimeT(time_t tt) {
michael@0 52 if (tt == 0)
michael@0 53 return Time(); // Preserve 0 so we can tell it doesn't exist.
michael@0 54 return (tt * kMicrosecondsPerSecond) + kTimeTToMicrosecondsOffset;
michael@0 55 }
michael@0 56
michael@0 57 time_t Time::ToTimeT() const {
michael@0 58 if (us_ == 0)
michael@0 59 return 0; // Preserve 0 so we can tell it doesn't exist.
michael@0 60 return (us_ - kTimeTToMicrosecondsOffset) / kMicrosecondsPerSecond;
michael@0 61 }
michael@0 62
michael@0 63 // static
michael@0 64 Time Time::FromDoubleT(double dt) {
michael@0 65 return (dt * static_cast<double>(kMicrosecondsPerSecond)) +
michael@0 66 kTimeTToMicrosecondsOffset;
michael@0 67 }
michael@0 68
michael@0 69 double Time::ToDoubleT() const {
michael@0 70 if (us_ == 0)
michael@0 71 return 0; // Preserve 0 so we can tell it doesn't exist.
michael@0 72 return (static_cast<double>(us_ - kTimeTToMicrosecondsOffset) /
michael@0 73 static_cast<double>(kMicrosecondsPerSecond));
michael@0 74 }
michael@0 75
michael@0 76 Time Time::LocalMidnight() const {
michael@0 77 Exploded exploded;
michael@0 78 LocalExplode(&exploded);
michael@0 79 exploded.hour = 0;
michael@0 80 exploded.minute = 0;
michael@0 81 exploded.second = 0;
michael@0 82 exploded.millisecond = 0;
michael@0 83 return FromLocalExploded(exploded);
michael@0 84 }
michael@0 85
michael@0 86 // static
michael@0 87 bool Time::FromString(const wchar_t* time_string, Time* parsed_time) {
michael@0 88 DCHECK((time_string != NULL) && (parsed_time != NULL));
michael@0 89 std::string ascii_time_string = SysWideToUTF8(time_string);
michael@0 90 if (ascii_time_string.length() == 0)
michael@0 91 return false;
michael@0 92 PRTime result_time = 0;
michael@0 93 PRStatus result = PR_ParseTimeString(ascii_time_string.c_str(), PR_FALSE,
michael@0 94 &result_time);
michael@0 95 if (PR_SUCCESS != result)
michael@0 96 return false;
michael@0 97 result_time += kTimeTToMicrosecondsOffset;
michael@0 98 *parsed_time = Time(result_time);
michael@0 99 return true;
michael@0 100 }
michael@0 101
michael@0 102 } // namespace base

mercurial