Chromium Code Reviews| Index: base/time_posix.cc |
| =================================================================== |
| --- base/time_posix.cc (revision 4280) |
| +++ base/time_posix.cc (working copy) |
| @@ -10,6 +10,8 @@ |
| #include <sys/time.h> |
| #include <time.h> |
| +#include <limits> |
| + |
| #include "base/basictypes.h" |
| #include "base/logging.h" |
| @@ -58,16 +60,34 @@ |
| seconds = mktime(×truct); |
| else |
| seconds = timegm(×truct); |
| - DCHECK(seconds >= 0) << "mktime/timegm could not convert from exploded"; |
| - uint64 milliseconds = seconds * kMillisecondsPerSecond + exploded.millisecond; |
| + int64 milliseconds; |
| + // Handle overflow. |
| + if (seconds == -1 && |
| + (exploded.year < 1969 || exploded.year > 1970)) { |
| + // If exploded.year is 1969 or 1970, take -1 as correct, with the |
| + // time indicating 1 second prior to the epoch. (1970 is allowed to handle |
| + // time zone and DST offsets.) Otherwise, return the most future or past |
| + // time representable. Assumes the time_t epoch is 1970-01-01 00:00:00 UTC. |
| + if (exploded.year < 1969) { |
| + milliseconds = std::numeric_limits<time_t>::min() * |
|
wtc
2008/10/31 20:52:01
Some points to ponder:
- What if time_t is uint32
Mark Mentovai
2008/10/31 21:21:01
wtc wrote:
|
| + kMillisecondsPerSecond; |
| + } else { |
| + milliseconds = (std::numeric_limits<time_t>::max() * |
| + kMillisecondsPerSecond) + |
| + kMillisecondsPerSecond - 1; |
|
wtc
2008/10/31 20:52:01
The need to add 999 milliseconds is still not clea
Mark Mentovai
2008/10/31 21:21:01
wtc wrote:
|
| + } |
| + } else { |
| + milliseconds = seconds * kMillisecondsPerSecond + exploded.millisecond; |
| + } |
| + |
| return Time(milliseconds * kMicrosecondsPerMillisecond); |
| } |
| void Time::Explode(bool is_local, Exploded* exploded) const { |
| // Time stores times with microsecond resolution, but Exploded only carries |
| // millisecond resolution, so begin by being lossy. |
| - uint64 milliseconds = us_ / kMicrosecondsPerMillisecond; |
| + int64 milliseconds = us_ / kMicrosecondsPerMillisecond; |
| time_t seconds = milliseconds / kMillisecondsPerSecond; |
| struct tm timestruct; |