1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/ipc/chromium/src/base/time.cc Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,102 @@ 1.4 +// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. 1.5 +// Use of this source code is governed by a BSD-style license that can be 1.6 +// found in the LICENSE file. 1.7 + 1.8 +#include "base/time.h" 1.9 +#include "base/string_util.h" 1.10 +#include "base/sys_string_conversions.h" 1.11 +#include "prtime.h" 1.12 + 1.13 +#include "base/logging.h" 1.14 + 1.15 +namespace base { 1.16 + 1.17 +// TimeDelta ------------------------------------------------------------------ 1.18 + 1.19 +int TimeDelta::InDays() const { 1.20 + return static_cast<int>(delta_ / Time::kMicrosecondsPerDay); 1.21 +} 1.22 + 1.23 +int TimeDelta::InHours() const { 1.24 + return static_cast<int>(delta_ / Time::kMicrosecondsPerHour); 1.25 +} 1.26 + 1.27 +int TimeDelta::InMinutes() const { 1.28 + return static_cast<int>(delta_ / Time::kMicrosecondsPerMinute); 1.29 +} 1.30 + 1.31 +double TimeDelta::InSecondsF() const { 1.32 + return static_cast<double>(delta_) / Time::kMicrosecondsPerSecond; 1.33 +} 1.34 + 1.35 +int64_t TimeDelta::InSeconds() const { 1.36 + return delta_ / Time::kMicrosecondsPerSecond; 1.37 +} 1.38 + 1.39 +double TimeDelta::InMillisecondsF() const { 1.40 + return static_cast<double>(delta_) / Time::kMicrosecondsPerMillisecond; 1.41 +} 1.42 + 1.43 +int64_t TimeDelta::InMilliseconds() const { 1.44 + return delta_ / Time::kMicrosecondsPerMillisecond; 1.45 +} 1.46 + 1.47 +int64_t TimeDelta::InMicroseconds() const { 1.48 + return delta_; 1.49 +} 1.50 + 1.51 +// Time ----------------------------------------------------------------------- 1.52 + 1.53 +// static 1.54 +Time Time::FromTimeT(time_t tt) { 1.55 + if (tt == 0) 1.56 + return Time(); // Preserve 0 so we can tell it doesn't exist. 1.57 + return (tt * kMicrosecondsPerSecond) + kTimeTToMicrosecondsOffset; 1.58 +} 1.59 + 1.60 +time_t Time::ToTimeT() const { 1.61 + if (us_ == 0) 1.62 + return 0; // Preserve 0 so we can tell it doesn't exist. 1.63 + return (us_ - kTimeTToMicrosecondsOffset) / kMicrosecondsPerSecond; 1.64 +} 1.65 + 1.66 +// static 1.67 +Time Time::FromDoubleT(double dt) { 1.68 + return (dt * static_cast<double>(kMicrosecondsPerSecond)) + 1.69 + kTimeTToMicrosecondsOffset; 1.70 +} 1.71 + 1.72 +double Time::ToDoubleT() const { 1.73 + if (us_ == 0) 1.74 + return 0; // Preserve 0 so we can tell it doesn't exist. 1.75 + return (static_cast<double>(us_ - kTimeTToMicrosecondsOffset) / 1.76 + static_cast<double>(kMicrosecondsPerSecond)); 1.77 +} 1.78 + 1.79 +Time Time::LocalMidnight() const { 1.80 + Exploded exploded; 1.81 + LocalExplode(&exploded); 1.82 + exploded.hour = 0; 1.83 + exploded.minute = 0; 1.84 + exploded.second = 0; 1.85 + exploded.millisecond = 0; 1.86 + return FromLocalExploded(exploded); 1.87 +} 1.88 + 1.89 +// static 1.90 +bool Time::FromString(const wchar_t* time_string, Time* parsed_time) { 1.91 + DCHECK((time_string != NULL) && (parsed_time != NULL)); 1.92 + std::string ascii_time_string = SysWideToUTF8(time_string); 1.93 + if (ascii_time_string.length() == 0) 1.94 + return false; 1.95 + PRTime result_time = 0; 1.96 + PRStatus result = PR_ParseTimeString(ascii_time_string.c_str(), PR_FALSE, 1.97 + &result_time); 1.98 + if (PR_SUCCESS != result) 1.99 + return false; 1.100 + result_time += kTimeTToMicrosecondsOffset; 1.101 + *parsed_time = Time(result_time); 1.102 + return true; 1.103 +} 1.104 + 1.105 +} // namespace base