Skip to content
This repository has been archived by the owner on Aug 4, 2022. It is now read-only.

Commit

Permalink
Bug 926777 - B2G system time: |time.timezone| should save the UTC val…
Browse files Browse the repository at this point in the history
…ue with the correct sign. r=gene
  • Loading branch information
Shao Hang Kao committed Oct 15, 2013
1 parent 6cc74da commit ac9443e
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 7 deletions.
4 changes: 2 additions & 2 deletions dom/system/gonk/RadioInterfaceLayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1928,14 +1928,14 @@ RadioInterface.prototype = {
setTimezoneByNitz: function setTimezoneByNitz(message) {
// To set the sytem timezone. Note that we need to convert the time zone
// value to a UTC repesentation string in the format of "UTC(+/-)hh:mm".
// Ex, time zone -480 is "UTC-08:00"; time zone 630 is "UTC+10:30".
// Ex, time zone -480 is "UTC+08:00"; time zone 630 is "UTC-10:30".
//
// We can unapply the DST correction if we want the raw time zone offset:
// message.networkTimeZoneInMinutes -= message.networkDSTInMinutes;
if (message.networkTimeZoneInMinutes != (new Date()).getTimezoneOffset()) {
let absTimeZoneInMinutes = Math.abs(message.networkTimeZoneInMinutes);
let timeZoneStr = "UTC";
timeZoneStr += (message.networkTimeZoneInMinutes >= 0 ? "+" : "-");
timeZoneStr += (message.networkTimeZoneInMinutes > 0 ? "-" : "+");
timeZoneStr += ("0" + Math.floor(absTimeZoneInMinutes / 60)).slice(-2);
timeZoneStr += ":";
timeZoneStr += ("0" + absTimeZoneInMinutes % 60).slice(-2);
Expand Down
32 changes: 28 additions & 4 deletions dom/system/gonk/TimeZoneSettingObserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "xpcpublic.h"
#include "nsContentUtils.h"
#include "nsCxPusher.h"
#include "nsPrintfCString.h"

#undef LOG
#define LOG(args...) __android_log_print(ANDROID_LOG_INFO, "Time Zone Setting" , ## args)
Expand Down Expand Up @@ -61,9 +62,18 @@ class TimeZoneSettingCb MOZ_FINAL : public nsISettingsServiceCallback
// to make settings consistent with system. This usually happens
// at the very first boot. After that, settings must have a value.
if (aResult.isNull()) {
// Get the current system timezone and convert it to a JS string.
nsCString curTimezone = hal::GetTimezone();
NS_ConvertUTF8toUTF16 utf16Str(curTimezone);
// Get the current system time zone offset. Note that we need to
// convert the value to a UTC representation in the format of
// "UTC{+,-}hh:mm", so that the Gaia end can know how to interpret.
// E.g., -480 is "UTC+08:00"; 630 is "UTC-10:30".
int32_t timeZoneOffset = hal::GetTimezoneOffset();
nsPrintfCString curTimeZone("UTC%+03d:%02d",
-timeZoneOffset / 60,
abs(timeZoneOffset) % 60);

// Convert it to a JS string.
NS_ConvertUTF8toUTF16 utf16Str(curTimeZone);

JSString *jsStr = JS_NewUCStringCopyN(cx, utf16Str.get(), utf16Str.Length());

// Set the settings based on the current system timezone.
Expand Down Expand Up @@ -127,13 +137,26 @@ TimeZoneSettingObserver::TimeZoneSettingObserver()
nsresult TimeZoneSettingObserver::SetTimeZone(const JS::Value &aValue, JSContext *aContext)
{
// Convert the JS value to a nsCString type.
// The value should be a JS string like "America/Chicago" or "UTC-05:00".
nsDependentJSString valueStr;
if (!valueStr.init(aContext, aValue.toString())) {
ERR("Failed to convert JS value to nsCString");
return NS_ERROR_FAILURE;
}
NS_ConvertUTF16toUTF8 newTimezone(valueStr);

// Hal expects opposite sign from general notations,
// so we need to flip it.
if (newTimezone.Find(NS_LITERAL_CSTRING("UTC+")) == 0) {
if (!newTimezone.SetCharAt('-', 3)) {
return NS_ERROR_FAILURE;
}
} else if (newTimezone.Find(NS_LITERAL_CSTRING("UTC-")) == 0) {
if (!newTimezone.SetCharAt('+', 3)) {
return NS_ERROR_FAILURE;
}
}

// Set the timezone only when the system timezone is not identical.
nsCString curTimezone = hal::GetTimezone();
if (!curTimezone.Equals(newTimezone)) {
Expand Down Expand Up @@ -166,7 +189,8 @@ TimeZoneSettingObserver::Observe(nsISupports *aSubject,
// so we need to carefully check if we have the one we're interested in.
//
// The string that we're interested in will be a JSON string that looks like:
// {"key":"time.timezone","value":"America/Chicago"}
// {"key":"time.timezone","value":"America/Chicago"} or
// {"key":"time.timezone","value":"UTC-05:00"}

AutoSafeJSContext cx;

Expand Down
7 changes: 7 additions & 0 deletions hal/Hal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,13 @@ SetTimezone(const nsCString& aTimezoneSpec)
PROXY_IF_SANDBOXED(SetTimezone(aTimezoneSpec));
}

int32_t
GetTimezoneOffset()
{
AssertMainThread();
RETURN_PROXY_IF_SANDBOXED(GetTimezoneOffset(), 0);
}

nsCString
GetTimezone()
{
Expand Down
6 changes: 6 additions & 0 deletions hal/Hal.h
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,12 @@ void SetTimezone(const nsCString& aTimezoneSpec);
*/
nsCString GetTimezone();

/**
* Get timezone offset
* returns the timezone offset relative to UTC in minutes (DST effect included)
*/
int32_t GetTimezoneOffset();

/**
* Register observer for system clock changed notification.
* @param aObserver The observer that should be added.
Expand Down
6 changes: 6 additions & 0 deletions hal/fallback/FallbackTime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ GetTimezone()
return EmptyCString();
}

int32_t
GetTimezoneOffset()
{
return 0;
}

void
EnableSystemClockChangeNotifications()
{
Expand Down
2 changes: 1 addition & 1 deletion hal/gonk/GonkHal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -742,7 +742,7 @@ AdjustSystemClock(int64_t aDeltaMilliseconds)
hal::NotifySystemClockChange(aDeltaMilliseconds);
}

static int32_t
int32_t
GetTimezoneOffset()
{
PRExplodedTime prTime;
Expand Down
2 changes: 2 additions & 0 deletions hal/sandbox/PHal.ipdl
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ parent:
SetTimezone(nsCString aTimezoneSpec);
sync GetTimezone()
returns (nsCString aTimezoneSpec);
sync GetTimezoneOffset()
returns (int32_t aTimezoneOffset);
EnableSystemClockChangeNotifications();
DisableSystemClockChangeNotifications();
EnableSystemTimezoneChangeNotifications();
Expand Down
18 changes: 18 additions & 0 deletions hal/sandbox/SandboxHal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,14 @@ GetTimezone()
return timezone;
}

int32_t
GetTimezoneOffset()
{
int32_t timezoneOffset;
Hal()->SendGetTimezoneOffset(&timezoneOffset);
return timezoneOffset;
}

void
EnableSystemClockChangeNotifications()
{
Expand Down Expand Up @@ -684,6 +692,16 @@ class HalParent : public PHalParent
return true;
}

virtual bool
RecvGetTimezoneOffset(int32_t *aTimezoneOffset) MOZ_OVERRIDE
{
if (!AssertAppProcessPermission(this, "time")) {
return false;
}
*aTimezoneOffset = hal::GetTimezoneOffset();
return true;
}

virtual bool
RecvEnableSystemClockChangeNotifications() MOZ_OVERRIDE
{
Expand Down

0 comments on commit ac9443e

Please sign in to comment.