Skip to content

Commit a85b7f4

Browse files
Made TimeZone string generation locale-agnostic for Windows Phone and Unity.
Previously, TimeZone string generation that we sent along with ParseInstallation was dependent upon the `CurrentCulture` of the device being set to the english locale (as `StandardName` is localized, WHAT?!) Our new, fancy TimeZone string is now generated agnostically of the current device locale. There is potential for information about the specific region of the time zone to be lost here, but most applications should not require this. This should fix #138.
1 parent a60db2e commit a85b7f4

File tree

2 files changed

+30
-22
lines changed

2 files changed

+30
-22
lines changed

Parse/Internal/PlatformHooks/Phone/PlatformHooks.Phone.cs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -150,17 +150,12 @@ public string DeviceType {
150150

151151
public string DeviceTimeZone {
152152
get {
153-
// We need the system string to be in english so we'll have the proper key in our lookup table.
154-
var culture = Thread.CurrentThread.CurrentCulture;
155-
Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
156-
string windowsName = TimeZoneInfo.Local.StandardName;
157-
Thread.CurrentThread.CurrentCulture = culture;
158-
159-
if (ParseInstallation.TimeZoneNameMap.ContainsKey(windowsName)) {
160-
return ParseInstallation.TimeZoneNameMap[windowsName];
161-
} else {
162-
return null;
163-
}
153+
TimeSpan utcOffset = TimeZoneInfo.Local.BaseUtcOffset;
154+
return String.Format("GMT{0}{1}:{2:d2}",
155+
offset.TotalSeconds < 0 ? "-" : "+",
156+
Math.Abs(offset.Hours),
157+
Math.Abs(offset.Minutes)
158+
);
164159
}
165160
}
166161

Parse/Internal/PlatformHooks/Unity/PlatformHooks.Unity.cs

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -86,17 +86,13 @@ public string DeviceType {
8686
}
8787

8888
public string DeviceTimeZone {
89-
get {
90-
try {
91-
string windowsName = TimeZoneInfo.Local.StandardName;
92-
if (ParseInstallation.TimeZoneNameMap.ContainsKey(windowsName)) {
93-
return ParseInstallation.TimeZoneNameMap[windowsName];
94-
} else {
95-
return null;
96-
}
97-
} catch (TimeZoneNotFoundException) {
98-
return null;
99-
}
89+
get {
90+
TimeSpan utcOffset = TimeZoneInfo.Local.BaseUtcOffset;
91+
return String.Format("GMT{0}{1}:{2:d2}",
92+
offset.TotalSeconds < 0 ? "-" : "+",
93+
Math.Abs(offset.Hours),
94+
Math.Abs(offset.Minutes)
95+
);
10096
}
10197
}
10298

@@ -147,6 +143,18 @@ internal static bool IsIOS {
147143
}
148144
}
149145

146+
/// <summary>
147+
/// Returns true if the current platform is tvOS.
148+
/// </summary>
149+
internal static bool IsTvOS {
150+
get {
151+
if (settingsPath == null) {
152+
throw new InvalidOperationException("Parse must be initialized before making any calls.");
153+
}
154+
return Application.platform == RuntimePlatform.tvOS;
155+
}
156+
}
157+
150158
/// <summary>
151159
/// Returns true if current running platform is Windows Phone 8.
152160
/// </summary>
@@ -230,6 +238,9 @@ private string Load() {
230238
try {
231239
if (IsWebPlayer) {
232240
return PlayerPrefs.GetString("Parse.settings", null);
241+
} else if (IsTvOS) {
242+
Debug.Log("Running on TvOS, prefs cannot be loaded.");
243+
return null;
233244
} else {
234245
using (var fs = new FileStream(settingsPath, FileMode.Open, FileAccess.Read)) {
235246
var reader = new StreamReader(fs);
@@ -250,6 +261,8 @@ private void Save() {
250261
if (IsWebPlayer) {
251262
PlayerPrefs.SetString("Parse.settings", ParseClient.SerializeJsonString(data));
252263
PlayerPrefs.Save();
264+
} else if (IsTvOS) {
265+
Debug.Log("Running on TvOS, prefs cannot be saved.");
253266
} else {
254267
using (var fs = new FileStream(settingsPath, FileMode.Create, FileAccess.Write)) {
255268
using (var writer = new StreamWriter(fs)) {

0 commit comments

Comments
 (0)