Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(416)

Side by Side Diff: base/time.cc

Issue 10916089: Fixing Time::Max()'s behavior with Time::ToTimeT() and friends. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Windows. :( Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/time.h" 5 #include "base/time.h"
6 6
7 #include <math.h> 7 #include <math.h>
8 #if defined(OS_WIN) 8 #if defined(OS_WIN)
9 #include <float.h> 9 #include <float.h>
10 #endif 10 #endif
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
67 67
68 // static 68 // static
69 Time Time::Max() { 69 Time Time::Max() {
70 return Time(std::numeric_limits<int64>::max()); 70 return Time(std::numeric_limits<int64>::max());
71 } 71 }
72 72
73 // static 73 // static
74 Time Time::FromTimeT(time_t tt) { 74 Time Time::FromTimeT(time_t tt) {
75 if (tt == 0) 75 if (tt == 0)
76 return Time(); // Preserve 0 so we can tell it doesn't exist. 76 return Time(); // Preserve 0 so we can tell it doesn't exist.
77 if (tt == std::numeric_limits<time_t>::max())
78 return Max();
77 return Time((tt * kMicrosecondsPerSecond) + kTimeTToMicrosecondsOffset); 79 return Time((tt * kMicrosecondsPerSecond) + kTimeTToMicrosecondsOffset);
78 } 80 }
79 81
80 time_t Time::ToTimeT() const { 82 time_t Time::ToTimeT() const {
81 if (us_ == 0) 83 if (is_null())
82 return 0; // Preserve 0 so we can tell it doesn't exist. 84 return 0; // Preserve 0 so we can tell it doesn't exist.
85 if (is_max()) {
86 // Preserve max without offset to prevent overflow.
87 return std::numeric_limits<time_t>::max();
88 }
83 return (us_ - kTimeTToMicrosecondsOffset) / kMicrosecondsPerSecond; 89 return (us_ - kTimeTToMicrosecondsOffset) / kMicrosecondsPerSecond;
msarda 2012/09/06 15:04:34 There is still a risk of overflow here: us_ might
Mike West 2012/09/06 19:51:16 Good point. I've added the log. This one looks lik
84 } 90 }
85 91
86 // static 92 // static
87 Time Time::FromDoubleT(double dt) { 93 Time Time::FromDoubleT(double dt) {
88 if (dt == 0 || isnan(dt)) 94 if (dt == 0 || isnan(dt))
89 return Time(); // Preserve 0 so we can tell it doesn't exist. 95 return Time(); // Preserve 0 so we can tell it doesn't exist.
96 if (dt == std::numeric_limits<double>::max())
97 return Max();
90 return Time(static_cast<int64>((dt * 98 return Time(static_cast<int64>((dt *
91 static_cast<double>(kMicrosecondsPerSecond)) + 99 static_cast<double>(kMicrosecondsPerSecond)) +
92 kTimeTToMicrosecondsOffset)); 100 kTimeTToMicrosecondsOffset));
93 } 101 }
94 102
95 double Time::ToDoubleT() const { 103 double Time::ToDoubleT() const {
96 if (us_ == 0) 104 if (is_null())
97 return 0; // Preserve 0 so we can tell it doesn't exist. 105 return 0; // Preserve 0 so we can tell it doesn't exist.
106 if (is_max()) {
107 // Preserve max without offset to prevent overflow.
108 return std::numeric_limits<double>::max();
109 }
98 return (static_cast<double>(us_ - kTimeTToMicrosecondsOffset) / 110 return (static_cast<double>(us_ - kTimeTToMicrosecondsOffset) /
99 static_cast<double>(kMicrosecondsPerSecond)); 111 static_cast<double>(kMicrosecondsPerSecond));
100 } 112 }
101 113
102 // static 114 // static
103 Time Time::FromJsTime(double ms_since_epoch) { 115 Time Time::FromJsTime(double ms_since_epoch) {
104 // The epoch is a valid time, so this constructor doesn't interpret 116 // The epoch is a valid time, so this constructor doesn't interpret
105 // 0 as the null time. 117 // 0 as the null time.
118 if (ms_since_epoch == std::numeric_limits<double>::max())
119 return Max();
106 return Time(static_cast<int64>(ms_since_epoch * kMicrosecondsPerMillisecond) + 120 return Time(static_cast<int64>(ms_since_epoch * kMicrosecondsPerMillisecond) +
107 kTimeTToMicrosecondsOffset); 121 kTimeTToMicrosecondsOffset);
108 } 122 }
109 123
110 double Time::ToJsTime() const { 124 double Time::ToJsTime() const {
111 if (us_ == 0) { 125 if (is_null()) {
112 // Preserve 0 so the invalid result doesn't depend on the platform. 126 // Preserve 0 so the invalid result doesn't depend on the platform.
113 return 0; 127 return 0;
114 } 128 }
129 if (is_max()) {
130 // Preserve max without offset to prevent overflow.
131 return std::numeric_limits<double>::max();
132 }
115 return (static_cast<double>(us_ - kTimeTToMicrosecondsOffset) / 133 return (static_cast<double>(us_ - kTimeTToMicrosecondsOffset) /
116 kMicrosecondsPerMillisecond); 134 kMicrosecondsPerMillisecond);
117 } 135 }
118 136
119 // static 137 // static
120 Time Time::UnixEpoch() { 138 Time Time::UnixEpoch() {
121 Time time; 139 Time time;
122 time.us_ = kTimeTToMicrosecondsOffset; 140 time.us_ = kTimeTToMicrosecondsOffset;
123 return time; 141 return time;
124 } 142 }
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
161 return is_in_range(month, 1, 12) && 179 return is_in_range(month, 1, 12) &&
162 is_in_range(day_of_week, 0, 6) && 180 is_in_range(day_of_week, 0, 6) &&
163 is_in_range(day_of_month, 1, 31) && 181 is_in_range(day_of_month, 1, 31) &&
164 is_in_range(hour, 0, 23) && 182 is_in_range(hour, 0, 23) &&
165 is_in_range(minute, 0, 59) && 183 is_in_range(minute, 0, 59) &&
166 is_in_range(second, 0, 60) && 184 is_in_range(second, 0, 60) &&
167 is_in_range(millisecond, 0, 999); 185 is_in_range(millisecond, 0, 999);
168 } 186 }
169 187
170 } // namespace base 188 } // namespace base
OLDNEW
« no previous file with comments | « base/time.h ('k') | base/time_mac.cc » ('j') | base/time_unittest.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698