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

Side by Side Diff: base/time/time.cc

Issue 163413004: Add base::TimeDelta::Max(). (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase Created 6 years, 10 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
« no previous file with comments | « base/time/time.h ('k') | base/time/time_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/time.h" 5 #include "base/time/time.h"
6 6
7 #include <limits> 7 #include <limits>
8 #include <ostream> 8 #include <ostream>
9 9
10 #include "base/float_util.h" 10 #include "base/float_util.h"
11 #include "base/lazy_instance.h" 11 #include "base/lazy_instance.h"
12 #include "base/logging.h" 12 #include "base/logging.h"
13 #include "base/third_party/nspr/prtime.h" 13 #include "base/third_party/nspr/prtime.h"
14 #include "base/third_party/nspr/prtypes.h" 14 #include "base/third_party/nspr/prtypes.h"
15 15
16 namespace base { 16 namespace base {
17 17
18 // TimeDelta ------------------------------------------------------------------ 18 // TimeDelta ------------------------------------------------------------------
19 19
20 // static
21 TimeDelta TimeDelta::Max() {
22 return TimeDelta(std::numeric_limits<int64>::max());
23 }
24
20 int TimeDelta::InDays() const { 25 int TimeDelta::InDays() const {
26 if (is_max()) {
27 // Preserve max to prevent overflow.
28 return std::numeric_limits<int>::max();
29 }
21 return static_cast<int>(delta_ / Time::kMicrosecondsPerDay); 30 return static_cast<int>(delta_ / Time::kMicrosecondsPerDay);
22 } 31 }
23 32
24 int TimeDelta::InHours() const { 33 int TimeDelta::InHours() const {
34 if (is_max()) {
35 // Preserve max to prevent overflow.
36 return std::numeric_limits<int>::max();
37 }
25 return static_cast<int>(delta_ / Time::kMicrosecondsPerHour); 38 return static_cast<int>(delta_ / Time::kMicrosecondsPerHour);
26 } 39 }
27 40
28 int TimeDelta::InMinutes() const { 41 int TimeDelta::InMinutes() const {
42 if (is_max()) {
43 // Preserve max to prevent overflow.
44 return std::numeric_limits<int>::max();
45 }
29 return static_cast<int>(delta_ / Time::kMicrosecondsPerMinute); 46 return static_cast<int>(delta_ / Time::kMicrosecondsPerMinute);
30 } 47 }
31 48
32 double TimeDelta::InSecondsF() const { 49 double TimeDelta::InSecondsF() const {
50 if (is_max()) {
51 // Preserve max to prevent overflow.
52 return std::numeric_limits<double>::infinity();
53 }
33 return static_cast<double>(delta_) / Time::kMicrosecondsPerSecond; 54 return static_cast<double>(delta_) / Time::kMicrosecondsPerSecond;
34 } 55 }
35 56
36 int64 TimeDelta::InSeconds() const { 57 int64 TimeDelta::InSeconds() const {
58 if (is_max()) {
59 // Preserve max to prevent overflow.
60 return std::numeric_limits<int64>::max();
61 }
37 return delta_ / Time::kMicrosecondsPerSecond; 62 return delta_ / Time::kMicrosecondsPerSecond;
38 } 63 }
39 64
40 double TimeDelta::InMillisecondsF() const { 65 double TimeDelta::InMillisecondsF() const {
66 if (is_max()) {
67 // Preserve max to prevent overflow.
68 return std::numeric_limits<double>::infinity();
69 }
41 return static_cast<double>(delta_) / Time::kMicrosecondsPerMillisecond; 70 return static_cast<double>(delta_) / Time::kMicrosecondsPerMillisecond;
42 } 71 }
43 72
44 int64 TimeDelta::InMilliseconds() const { 73 int64 TimeDelta::InMilliseconds() const {
74 if (is_max()) {
75 // Preserve max to prevent overflow.
76 return std::numeric_limits<int64>::max();
77 }
45 return delta_ / Time::kMicrosecondsPerMillisecond; 78 return delta_ / Time::kMicrosecondsPerMillisecond;
46 } 79 }
47 80
48 int64 TimeDelta::InMillisecondsRoundedUp() const { 81 int64 TimeDelta::InMillisecondsRoundedUp() const {
82 if (is_max()) {
83 // Preserve max to prevent overflow.
84 return std::numeric_limits<int64>::max();
85 }
49 return (delta_ + Time::kMicrosecondsPerMillisecond - 1) / 86 return (delta_ + Time::kMicrosecondsPerMillisecond - 1) /
50 Time::kMicrosecondsPerMillisecond; 87 Time::kMicrosecondsPerMillisecond;
51 } 88 }
52 89
53 int64 TimeDelta::InMicroseconds() const { 90 int64 TimeDelta::InMicroseconds() const {
91 if (is_max()) {
92 // Preserve max to prevent overflow.
93 return std::numeric_limits<int64>::max();
94 }
54 return delta_; 95 return delta_;
55 } 96 }
56 97
57 // Time ----------------------------------------------------------------------- 98 // Time -----------------------------------------------------------------------
58 99
59 // static 100 // static
60 Time Time::Max() { 101 Time Time::Max() {
61 return Time(std::numeric_limits<int64>::max()); 102 return Time(std::numeric_limits<int64>::max());
62 } 103 }
63 104
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after
223 return is_in_range(month, 1, 12) && 264 return is_in_range(month, 1, 12) &&
224 is_in_range(day_of_week, 0, 6) && 265 is_in_range(day_of_week, 0, 6) &&
225 is_in_range(day_of_month, 1, 31) && 266 is_in_range(day_of_month, 1, 31) &&
226 is_in_range(hour, 0, 23) && 267 is_in_range(hour, 0, 23) &&
227 is_in_range(minute, 0, 59) && 268 is_in_range(minute, 0, 59) &&
228 is_in_range(second, 0, 60) && 269 is_in_range(second, 0, 60) &&
229 is_in_range(millisecond, 0, 999); 270 is_in_range(millisecond, 0, 999);
230 } 271 }
231 272
232 } // namespace base 273 } // namespace base
OLDNEW
« no previous file with comments | « base/time/time.h ('k') | base/time/time_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698