1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/security/sandbox/chromium/base/time/time.cc Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,208 @@ 1.4 +// Copyright (c) 2012 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/time.h" 1.9 + 1.10 +#include <limits> 1.11 +#include <ostream> 1.12 + 1.13 +#include "base/float_util.h" 1.14 +#include "base/logging.h" 1.15 +#include "base/third_party/nspr/prtime.h" 1.16 +#include "base/third_party/nspr/prtypes.h" 1.17 + 1.18 +namespace base { 1.19 + 1.20 +// TimeDelta ------------------------------------------------------------------ 1.21 + 1.22 +int TimeDelta::InDays() const { 1.23 + return static_cast<int>(delta_ / Time::kMicrosecondsPerDay); 1.24 +} 1.25 + 1.26 +int TimeDelta::InHours() const { 1.27 + return static_cast<int>(delta_ / Time::kMicrosecondsPerHour); 1.28 +} 1.29 + 1.30 +int TimeDelta::InMinutes() const { 1.31 + return static_cast<int>(delta_ / Time::kMicrosecondsPerMinute); 1.32 +} 1.33 + 1.34 +double TimeDelta::InSecondsF() const { 1.35 + return static_cast<double>(delta_) / Time::kMicrosecondsPerSecond; 1.36 +} 1.37 + 1.38 +int64 TimeDelta::InSeconds() const { 1.39 + return delta_ / Time::kMicrosecondsPerSecond; 1.40 +} 1.41 + 1.42 +double TimeDelta::InMillisecondsF() const { 1.43 + return static_cast<double>(delta_) / Time::kMicrosecondsPerMillisecond; 1.44 +} 1.45 + 1.46 +int64 TimeDelta::InMilliseconds() const { 1.47 + return delta_ / Time::kMicrosecondsPerMillisecond; 1.48 +} 1.49 + 1.50 +int64 TimeDelta::InMillisecondsRoundedUp() const { 1.51 + return (delta_ + Time::kMicrosecondsPerMillisecond - 1) / 1.52 + Time::kMicrosecondsPerMillisecond; 1.53 +} 1.54 + 1.55 +int64 TimeDelta::InMicroseconds() const { 1.56 + return delta_; 1.57 +} 1.58 + 1.59 +// Time ----------------------------------------------------------------------- 1.60 + 1.61 +// static 1.62 +Time Time::Max() { 1.63 + return Time(std::numeric_limits<int64>::max()); 1.64 +} 1.65 + 1.66 +// static 1.67 +Time Time::FromTimeT(time_t tt) { 1.68 + if (tt == 0) 1.69 + return Time(); // Preserve 0 so we can tell it doesn't exist. 1.70 + if (tt == std::numeric_limits<time_t>::max()) 1.71 + return Max(); 1.72 + return Time((tt * kMicrosecondsPerSecond) + kTimeTToMicrosecondsOffset); 1.73 +} 1.74 + 1.75 +time_t Time::ToTimeT() const { 1.76 + if (is_null()) 1.77 + return 0; // Preserve 0 so we can tell it doesn't exist. 1.78 + if (is_max()) { 1.79 + // Preserve max without offset to prevent overflow. 1.80 + return std::numeric_limits<time_t>::max(); 1.81 + } 1.82 + if (std::numeric_limits<int64>::max() - kTimeTToMicrosecondsOffset <= us_) { 1.83 + DLOG(WARNING) << "Overflow when converting base::Time with internal " << 1.84 + "value " << us_ << " to time_t."; 1.85 + return std::numeric_limits<time_t>::max(); 1.86 + } 1.87 + return (us_ - kTimeTToMicrosecondsOffset) / kMicrosecondsPerSecond; 1.88 +} 1.89 + 1.90 +// static 1.91 +Time Time::FromDoubleT(double dt) { 1.92 + if (dt == 0 || IsNaN(dt)) 1.93 + return Time(); // Preserve 0 so we can tell it doesn't exist. 1.94 + if (dt == std::numeric_limits<double>::max()) 1.95 + return Max(); 1.96 + return Time(static_cast<int64>((dt * 1.97 + static_cast<double>(kMicrosecondsPerSecond)) + 1.98 + kTimeTToMicrosecondsOffset)); 1.99 +} 1.100 + 1.101 +double Time::ToDoubleT() const { 1.102 + if (is_null()) 1.103 + return 0; // Preserve 0 so we can tell it doesn't exist. 1.104 + if (is_max()) { 1.105 + // Preserve max without offset to prevent overflow. 1.106 + return std::numeric_limits<double>::max(); 1.107 + } 1.108 + return (static_cast<double>(us_ - kTimeTToMicrosecondsOffset) / 1.109 + static_cast<double>(kMicrosecondsPerSecond)); 1.110 +} 1.111 + 1.112 +#if defined(OS_POSIX) 1.113 +// static 1.114 +Time Time::FromTimeSpec(const timespec& ts) { 1.115 + return FromDoubleT(ts.tv_sec + 1.116 + static_cast<double>(ts.tv_nsec) / 1.117 + base::Time::kNanosecondsPerSecond); 1.118 +} 1.119 +#endif 1.120 + 1.121 +// static 1.122 +Time Time::FromJsTime(double ms_since_epoch) { 1.123 + // The epoch is a valid time, so this constructor doesn't interpret 1.124 + // 0 as the null time. 1.125 + if (ms_since_epoch == std::numeric_limits<double>::max()) 1.126 + return Max(); 1.127 + return Time(static_cast<int64>(ms_since_epoch * kMicrosecondsPerMillisecond) + 1.128 + kTimeTToMicrosecondsOffset); 1.129 +} 1.130 + 1.131 +double Time::ToJsTime() const { 1.132 + if (is_null()) { 1.133 + // Preserve 0 so the invalid result doesn't depend on the platform. 1.134 + return 0; 1.135 + } 1.136 + if (is_max()) { 1.137 + // Preserve max without offset to prevent overflow. 1.138 + return std::numeric_limits<double>::max(); 1.139 + } 1.140 + return (static_cast<double>(us_ - kTimeTToMicrosecondsOffset) / 1.141 + kMicrosecondsPerMillisecond); 1.142 +} 1.143 + 1.144 +int64 Time::ToJavaTime() const { 1.145 + if (is_null()) { 1.146 + // Preserve 0 so the invalid result doesn't depend on the platform. 1.147 + return 0; 1.148 + } 1.149 + if (is_max()) { 1.150 + // Preserve max without offset to prevent overflow. 1.151 + return std::numeric_limits<int64>::max(); 1.152 + } 1.153 + return ((us_ - kTimeTToMicrosecondsOffset) / 1.154 + kMicrosecondsPerMillisecond); 1.155 +} 1.156 + 1.157 +// static 1.158 +Time Time::UnixEpoch() { 1.159 + Time time; 1.160 + time.us_ = kTimeTToMicrosecondsOffset; 1.161 + return time; 1.162 +} 1.163 + 1.164 +Time Time::LocalMidnight() const { 1.165 + Exploded exploded; 1.166 + LocalExplode(&exploded); 1.167 + exploded.hour = 0; 1.168 + exploded.minute = 0; 1.169 + exploded.second = 0; 1.170 + exploded.millisecond = 0; 1.171 + return FromLocalExploded(exploded); 1.172 +} 1.173 + 1.174 +// static 1.175 +bool Time::FromStringInternal(const char* time_string, 1.176 + bool is_local, 1.177 + Time* parsed_time) { 1.178 + DCHECK((time_string != NULL) && (parsed_time != NULL)); 1.179 + 1.180 + if (time_string[0] == '\0') 1.181 + return false; 1.182 + 1.183 + PRTime result_time = 0; 1.184 + PRStatus result = PR_ParseTimeString(time_string, 1.185 + is_local ? PR_FALSE : PR_TRUE, 1.186 + &result_time); 1.187 + if (PR_SUCCESS != result) 1.188 + return false; 1.189 + 1.190 + result_time += kTimeTToMicrosecondsOffset; 1.191 + *parsed_time = Time(result_time); 1.192 + return true; 1.193 +} 1.194 + 1.195 +// Time::Exploded ------------------------------------------------------------- 1.196 + 1.197 +inline bool is_in_range(int value, int lo, int hi) { 1.198 + return lo <= value && value <= hi; 1.199 +} 1.200 + 1.201 +bool Time::Exploded::HasValidValues() const { 1.202 + return is_in_range(month, 1, 12) && 1.203 + is_in_range(day_of_week, 0, 6) && 1.204 + is_in_range(day_of_month, 1, 31) && 1.205 + is_in_range(hour, 0, 23) && 1.206 + is_in_range(minute, 0, 59) && 1.207 + is_in_range(second, 0, 60) && 1.208 + is_in_range(millisecond, 0, 999); 1.209 +} 1.210 + 1.211 +} // namespace base