From 24bf09b70c1bd064bb1b755562d97c8f2da943ea Mon Sep 17 00:00:00 2001 From: Martin Carlberg Date: Tue, 21 Sep 2021 10:30:26 +0200 Subject: [PATCH] Fixed: Abbreviation for a timezone is not handled in JavaScript. This changes makes it better but not perfect. --- Foundation/CPTimeZone.j | 23 +++++++++++++++++------ Tests/Foundation/CPTimeZoneTest.j | 18 ++++++++++++------ 2 files changed, 29 insertions(+), 12 deletions(-) diff --git a/Foundation/CPTimeZone.j b/Foundation/CPTimeZone.j index d5e652234..d9ca83a4d 100644 --- a/Foundation/CPTimeZone.j +++ b/Foundation/CPTimeZone.j @@ -44,6 +44,17 @@ var abbreviationDictionary, timeZoneDataVersion, localizedName; +function abbreviationForDate(date) +{ + var abbreviation = String(String(date).split("(")[1]).split(")")[0]; + + if (abbreviation.includes(" ")) + // Some browsers will now have the time zone in long format. Take first letter of each word. + // This is not 100% but is better than nothing. + abbreviation = abbreviation.split(" ").map(function(l) { return l[0]}).join(""); + + return abbreviation; +} /*! @class CPTimeZone @ingroup foundation @@ -269,11 +280,11 @@ var abbreviationDictionary, }; var date = [CPDate date], - abbreviation = String(String(date).split("(")[1]).split(")")[0]; + abbreviation = abbreviationForDate(date); localTimeZone = [self timeZoneWithAbbreviation:abbreviation]; - systemTimeZone = [self timeZoneWithAbbreviation:abbreviation]; - defaultTimeZone = [self timeZoneWithAbbreviation:abbreviation]; + systemTimeZone = localTimeZone; + defaultTimeZone = localTimeZone; localizedName = @{ @"en" : englishLocalizedName, @@ -430,7 +441,7 @@ var abbreviationDictionary, + (void)resetSystemTimeZone { var date = [CPDate date], - abbreviation = String(String(date).split("(")[1]).split(")")[0]; + abbreviation = abbreviationForDate(date); systemTimeZone = [self timeZoneWithAbbreviation:abbreviation]; @@ -560,7 +571,7 @@ var abbreviationDictionary, if (!date) return nil; - return String(String(date).split("(")[1]).split(")")[0]; + return abbreviationForDate(date); } /*! Returns the number of seconds from GMT for the given date @@ -573,7 +584,7 @@ var abbreviationDictionary, if (!date) return nil; - var abbreviation = String(String(date).split("(")[1]).split(")")[0]; + var abbreviation = abbreviationForDate(date); return [timeDifferenceFromUTC valueForKey:abbreviation] * 60; } diff --git a/Tests/Foundation/CPTimeZoneTest.j b/Tests/Foundation/CPTimeZoneTest.j index 50dc8a2a2..1af957c76 100644 --- a/Tests/Foundation/CPTimeZoneTest.j +++ b/Tests/Foundation/CPTimeZoneTest.j @@ -96,7 +96,7 @@ { try { - var timeZone = [CPTimeZone timeZoneWithName:nil]; + [CPTimeZone timeZoneWithName:nil]; [self fail:"Invalid value provided for tzName"]; } catch (e) @@ -140,7 +140,7 @@ { try { - var timeZone = [CPTimeZone timeZoneWithName:nil data:_data]; + [CPTimeZone timeZoneWithName:nil data:_data]; [self fail:"Invalid value provided for tzName"]; } catch (e) @@ -204,7 +204,7 @@ { try { - var timeZone = [[CPTimeZone alloc] initWithName:nil]; + [[CPTimeZone alloc] initWithName:nil]; [self fail:"Invalid value provided for tzName"]; } catch (e) @@ -248,7 +248,7 @@ { try { - var timeZone = [[CPTimeZone alloc] initWithName:nil data:_data]; + [[CPTimeZone alloc] initWithName:nil data:_data]; [self fail:"Invalid value provided for tzName"]; } catch (e) @@ -260,9 +260,15 @@ - (void)testAbbreviationWithDate { var timeZone = [CPTimeZone localTimeZone], - abbreviation = [timeZone abbreviationForDate:_date]; + abbreviation = [timeZone abbreviationForDate:_date], + expected = String(String(_date).split("(")[1]).split(")")[0]; - [self assert:abbreviation equals:String(String(_date).split("(")[1]).split(")")[0]]; + if (expected.includes(" ")) + // Some browsers will now have the time zone in long format. Take first letter of each word. + // This is not 100% but is better than nothing. + expected = expected.split(" ").map(function(l) { return l[0]}).join(""); + + [self assert:abbreviation equals:expected]; } - (void)testAbbreviationWithNilDate