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

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: 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(); // Max is 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 (us_ == 0)
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 return std::numeric_limits<time_t>::max();
83 return (us_ - kTimeTToMicrosecondsOffset) / kMicrosecondsPerSecond; 87 return (us_ - kTimeTToMicrosecondsOffset) / kMicrosecondsPerSecond;
84 } 88 }
85 89
86 // static 90 // static
87 Time Time::FromDoubleT(double dt) { 91 Time Time::FromDoubleT(double dt) {
88 if (dt == 0 || isnan(dt)) 92 if (dt == 0 || isnan(dt))
89 return Time(); // Preserve 0 so we can tell it doesn't exist. 93 return Time(); // Preserve 0 so we can tell it doesn't exist.
94 if (dt == std::numeric_limits<double>::max())
95 return Max(); // Max is max.
90 return Time(static_cast<int64>((dt * 96 return Time(static_cast<int64>((dt *
91 static_cast<double>(kMicrosecondsPerSecond)) + 97 static_cast<double>(kMicrosecondsPerSecond)) +
92 kTimeTToMicrosecondsOffset)); 98 kTimeTToMicrosecondsOffset));
93 } 99 }
94 100
95 double Time::ToDoubleT() const { 101 double Time::ToDoubleT() const {
96 if (us_ == 0) 102 if (us_ == 0)
97 return 0; // Preserve 0 so we can tell it doesn't exist. 103 return 0; // Preserve 0 so we can tell it doesn't exist.
104 if (is_max())
105 return std::numeric_limits<double>::max(); // Max is max.
98 return (static_cast<double>(us_ - kTimeTToMicrosecondsOffset) / 106 return (static_cast<double>(us_ - kTimeTToMicrosecondsOffset) /
99 static_cast<double>(kMicrosecondsPerSecond)); 107 static_cast<double>(kMicrosecondsPerSecond));
100 } 108 }
101 109
102 // static 110 // static
103 Time Time::FromJsTime(double ms_since_epoch) { 111 Time Time::FromJsTime(double ms_since_epoch) {
104 // The epoch is a valid time, so this constructor doesn't interpret 112 // The epoch is a valid time, so this constructor doesn't interpret
105 // 0 as the null time. 113 // 0 as the null time.
114 if (ms_since_epoch == std::numeric_limits<double>::max())
115 return Max(); // Max is max.
106 return Time(static_cast<int64>(ms_since_epoch * kMicrosecondsPerMillisecond) + 116 return Time(static_cast<int64>(ms_since_epoch * kMicrosecondsPerMillisecond) +
107 kTimeTToMicrosecondsOffset); 117 kTimeTToMicrosecondsOffset);
108 } 118 }
109 119
110 double Time::ToJsTime() const { 120 double Time::ToJsTime() const {
111 if (us_ == 0) { 121 if (us_ == 0)
112 // Preserve 0 so the invalid result doesn't depend on the platform. 122 return 0; // Preserve 0 so the invalid result doesn't depend on a platform.
113 return 0; 123 if (is_max())
114 } 124 return std::numeric_limits<double>::max();
115 return (static_cast<double>(us_ - kTimeTToMicrosecondsOffset) / 125 return (static_cast<double>(us_ - kTimeTToMicrosecondsOffset) /
116 kMicrosecondsPerMillisecond); 126 kMicrosecondsPerMillisecond);
117 } 127 }
118 128
119 // static 129 // static
120 Time Time::UnixEpoch() { 130 Time Time::UnixEpoch() {
121 Time time; 131 Time time;
122 time.us_ = kTimeTToMicrosecondsOffset; 132 time.us_ = kTimeTToMicrosecondsOffset;
123 return time; 133 return time;
124 } 134 }
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
161 return is_in_range(month, 1, 12) && 171 return is_in_range(month, 1, 12) &&
162 is_in_range(day_of_week, 0, 6) && 172 is_in_range(day_of_week, 0, 6) &&
163 is_in_range(day_of_month, 1, 31) && 173 is_in_range(day_of_month, 1, 31) &&
164 is_in_range(hour, 0, 23) && 174 is_in_range(hour, 0, 23) &&
165 is_in_range(minute, 0, 59) && 175 is_in_range(minute, 0, 59) &&
166 is_in_range(second, 0, 60) && 176 is_in_range(second, 0, 60) &&
167 is_in_range(millisecond, 0, 999); 177 is_in_range(millisecond, 0, 999);
168 } 178 }
169 179
170 } // namespace base 180 } // namespace base
OLDNEW
« base/time.h ('K') | « base/time.h ('k') | base/time_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698