Revision: 4530
Author: [email protected]
Date: Wed Apr 28 04:11:09 2010
Log: Port of date and timezone fixes from bleeding edge to
partial snapshots branch.
http://codereview.chromium.org/1576002 r4326
Fix time zone cache so it is not initialized when the snapshot is built.
http://codereview.chromium.org/1543008 r4332
Update the time zone offset and dst offset when time zone name changes.
http://codereview.chromium.org/1539009 r4342
Modify date printing to fetch time zone name before converting to local
time, so that the two agree. Fix a problem in DateToTimeString() time
zone calculation.
Review URL: http://codereview.chromium.org/1742012
http://code.google.com/p/v8/source/detail?r=4530
Modified:
/branches/experimental/partial_snapshots/src/date.js
=======================================
--- /branches/experimental/partial_snapshots/src/date.js Tue Mar 23
02:57:36 2010
+++ /branches/experimental/partial_snapshots/src/date.js Wed Apr 28
04:11:09 2010
@@ -121,9 +121,16 @@
}
-// Because computing the DST offset is a pretty expensive operation
-// we keep a cache of last computed offset along with a time interval
+// local_time_offset is initialized when the DST_offset_cache is missed.
+// It must not be used until after a call to DaylightSavingsOffset().
+// In this way, only one check, for a DST cache miss, is needed.
+var local_time_offset;
+
+
+// Because computing the DST offset is an expensive operation,
+// we keep a cache of the last computed DST offset along with a time
interval
// where we know the cache is valid.
+// When the cache is valid, local_time_offset is also valid.
var DST_offset_cache = {
// Cached DST offset.
offset: 0,
@@ -148,6 +155,11 @@
if (start <= t) {
// If the time fits in the cached interval, return the cached offset.
if (t <= end) return cache.offset;
+
+ // If the cache misses, the local_time_offset may not be initialized.
+ if (IS_UNDEFINED(local_time_offset)) {
+ local_time_offset = %DateLocalTimeOffset();
+ }
// Compute a possible new interval end.
var new_end = end + cache.increment;
@@ -185,6 +197,10 @@
}
}
+ // If the cache misses, the local_time_offset may not be initialized.
+ if (IS_UNDEFINED(local_time_offset)) {
+ local_time_offset = %DateLocalTimeOffset();
+ }
// Compute the DST offset for the time and shrink the cache interval
// to only contain the time. This allows fast repeated DST offset
// computations for the same time.
@@ -215,15 +231,17 @@
return Modulo(DAY(time) + 4, 7);
}
-var local_time_offset = %DateLocalTimeOffset();
function LocalTime(time) {
if (NUMBER_IS_NAN(time)) return time;
- return time + local_time_offset + DaylightSavingsOffset(time);
+ // DaylightSavingsOffset called before local_time_offset used.
+ return time + DaylightSavingsOffset(time) + local_time_offset;
}
function LocalTimeNoCheck(time) {
// Inline the DST offset cache checks for speed.
+ // The cache is hit, or DaylightSavingsOffset is called,
+ // before local_time_offset is used.
var cache = DST_offset_cache;
if (cache.start <= time && time <= cache.end) {
var dst_offset = cache.offset;
@@ -236,6 +254,11 @@
function UTC(time) {
if (NUMBER_IS_NAN(time)) return time;
+ // local_time_offset is needed before the call to DaylightSavingsOffset,
+ // so it may be uninitialized.
+ if (IS_UNDEFINED(local_time_offset)) {
+ local_time_offset = %DateLocalTimeOffset();
+ }
var tmp = time - local_time_offset;
return tmp - DaylightSavingsOffset(tmp);
}
@@ -564,14 +587,28 @@
function LocalTimezoneString(time) {
+ var old_timezone = timezone_cache_timezone;
+ var timezone = LocalTimezone(time);
+ if (old_timezone && timezone != old_timezone) {
+ // If the timezone string has changed from the one that we cached,
+ // the local time offset may now be wrong. So we need to update it
+ // and try again.
+ local_time_offset = %DateLocalTimeOffset();
+ // We also need to invalidate the DST cache as the new timezone may
have
+ // different DST times.
+ var dst_cache = DST_offset_cache;
+ dst_cache.start = 0;
+ dst_cache.end = -1;
+ }
+
var timezoneOffset =
- (local_time_offset + DaylightSavingsOffset(time)) / msPerMinute;
+ (DaylightSavingsOffset(time) + local_time_offset) / msPerMinute;
var sign = (timezoneOffset >= 0) ? 1 : -1;
var hours = FLOOR((sign * timezoneOffset)/60);
var min = FLOOR((sign * timezoneOffset)%60);
var gmt = ' GMT' + ((sign == 1) ? '+' : '-') +
TwoDigitString(hours) + TwoDigitString(min);
- return gmt + ' (' + LocalTimezone(time) + ')';
+ return gmt + ' (' + timezone + ')';
}
@@ -630,7 +667,8 @@
function DateToString() {
var t = DATE_VALUE(this);
if (NUMBER_IS_NAN(t)) return kInvalidDate;
- return DatePrintString(LocalTimeNoCheck(t)) + LocalTimezoneString(t);
+ var time_zone_string = LocalTimezoneString(t); // May update local
offset.
+ return DatePrintString(LocalTimeNoCheck(t)) + time_zone_string;
}
@@ -646,8 +684,8 @@
function DateToTimeString() {
var t = DATE_VALUE(this);
if (NUMBER_IS_NAN(t)) return kInvalidDate;
- var lt = LocalTimeNoCheck(t);
- return TimeString(lt) + LocalTimezoneString(lt);
+ var time_zone_string = LocalTimezoneString(t); // May update local
offset.
+ return TimeString(LocalTimeNoCheck(t)) + time_zone_string;
}
--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev