From 7f65bf94efec5f7fa95b877cea1154f64875dfe8 Mon Sep 17 00:00:00 2001 From: Jeff Walden Date: Fri, 25 Jan 2013 15:46:10 -0800 Subject: [PATCH] Bug 834988 - Invert the meaning of LocalUTCDifferenceSeconds and rename it to UTCToLocalStandardOffsetSeconds. This gives it the same sign/semantics (if different units) as the -8 in UTC-8, and it makes it consistent with ES5's LocalTZA concept. Also add an interface comment with two examples of its behavior. r=dmandelin --HG-- extra : rebase_source : 3e56a9fcc590c6b42c2782bd30431db8f3464825 --- js/src/vm/DateTime.cpp | 49 ++++++++++++++++++++++++++---------------- 1 file changed, 30 insertions(+), 19 deletions(-) diff --git a/js/src/vm/DateTime.cpp b/js/src/vm/DateTime.cpp index 42be929450a..8bff848bb7c 100644 --- a/js/src/vm/DateTime.cpp +++ b/js/src/vm/DateTime.cpp @@ -38,8 +38,22 @@ ComputeUTCTime(time_t t, struct tm *ptm) #endif } +/* + * Compute the offset in seconds from the current UTC time to the current local + * standard time (i.e. not including any offset due to DST). + * + * Examples: + * + * Suppose we are in California, USA on January 1, 2013 at 04:00 PST (UTC-8, no + * DST in effect), corresponding to 12:00 UTC. This function would then return + * -8 * SecondsPerHour, or -28800. + * + * Or suppose we are in Berlin, Germany on July 1, 2013 at 17:00 CEST (UTC+2, + * DST in effect), corresponding to 15:00 UTC. This function would then return + * +1 * SecondsPerHour, or +3600. + */ static int32_t -LocalUTCDifferenceSeconds() +UTCToLocalStandardOffsetSeconds() { using js::SecondsPerDay; using js::SecondsPerHour; @@ -96,24 +110,24 @@ LocalUTCDifferenceSeconds() int utc_secs = utc.tm_hour * SecondsPerHour + utc.tm_min * SecondsPerMinute; int local_secs = local.tm_hour * SecondsPerHour + local.tm_min * SecondsPerMinute; - // Callers expect the negative difference of the offset from local time - // and UTC. - + // Same-day? Just subtract the seconds counts. if (utc.tm_mday == local.tm_mday) - return utc_secs - local_secs; + return local_secs - utc_secs; - // Local date comes after UTC (offset in the positive range). + // If we have more UTC seconds, move local seconds into the UTC seconds' + // frame of reference and then subtract. if (utc_secs > local_secs) - return utc_secs - (SecondsPerDay + local_secs); + return (SecondsPerDay + local_secs) - utc_secs; - // Local date comes before UTC (offset in the negative range). - return (utc_secs + SecondsPerDay) - local_secs; + // Otherwise we have more local seconds, so move the UTC seconds into the + // local seconds' frame of reference and then subtract. + return local_secs - (utc_secs + SecondsPerDay); } void js::DateTimeInfo::updateTimeZoneAdjustment() { - double newTZA = -(LocalUTCDifferenceSeconds() * msPerSecond); + double newTZA = UTCToLocalStandardOffsetSeconds() * msPerSecond; if (newTZA == localTZA_) return; @@ -153,11 +167,9 @@ js::DateTimeInfo::computeDSTOffsetMilliseconds(int64_t localTimeSeconds) MOZ_ASSERT(localTimeSeconds <= MaxUnixTimeT); #if defined(XP_WIN) - /* Windows does not follow POSIX. Updates to the - * TZ environment variable are not reflected - * immediately on that platform as they are - * on UNIX systems without this call. - */ + // Windows does not follow POSIX. Updates to the TZ environment variable + // are not reflected immediately on that platform as they are on UNIX + // systems without this call. _tzset(); #endif @@ -165,11 +177,10 @@ js::DateTimeInfo::computeDSTOffsetMilliseconds(int64_t localTimeSeconds) if (!ComputeLocalTime(static_cast(localTimeSeconds), &tm)) return 0; - int32_t base = LocalUTCDifferenceSeconds(); + int32_t utcToLocalOffsetSeconds = UTCToLocalStandardOffsetSeconds(); - int32_t dayoff = int32_t((localTimeSeconds - base) % (SecondsPerHour * 24)); - int32_t tmoff = tm.tm_sec + (tm.tm_min * SecondsPerMinute) + - (tm.tm_hour * SecondsPerHour); + int32_t dayoff = int32_t((localTimeSeconds + utcToLocalOffsetSeconds) % SecondsPerDay); + int32_t tmoff = tm.tm_sec + (tm.tm_min * SecondsPerMinute) + (tm.tm_hour * SecondsPerHour); int32_t diff = tmoff - dayoff;