Files
David Richardson c39f03823a Replace date-string parsing in CPTimeZone with Intl API
* Update abbreviationForDate and _abbreviationForNameAndDate to read the short time zone name directly from Intl.DateTimeFormat(...).formatToParts(). This replaces regular expression parsing of date strings.

 * Update knownTimeZoneNames to use Intl.supportedValuesOf('timeZone') to retrieve the complete IANA time zone set. The method falls back to the legacy 48-city list if Intl is unavailable.

* Replace duplicate regular expression logic in testInitWithNameRespectsDaylightSaving with a direct Intl call.

* Add pinning tests for the recently corrected offsets and the unchanged MSD value.

* Add a feature-detected test for the knownTimeZoneNames upgrade.

* Add two disabled tests to document unresolved gaps: non-English localizedName outputs and ambiguous reverse offset lookups.

* The full IANA-identity redesign as a separate task. Abbreviation-keyed lookups, colliding offsets, and English-only localizedName limitations remain unresolved in this interim fix.
2026-08-28 14:19:46 -06:00

462 lines
20 KiB
Plaintext

/* CPTimeZoneTest.j
* Foundation
*
* Created by Alexandre Wilhelm
* Copyright 2012 <alexandre.wilhelmfr@gmail.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
@import <Foundation/Foundation.j>
@import <OJUnit/OJTestCase.j>
@implementation CPTimeZoneTest : OJTestCase
{
CPLocale _locale;
CPData _data;
CPDate _date;
}
- (void)setUp
{
_locale = [[CPLocale alloc] initWithLocaleIdentifier:@"en_US"];
_data = [CPData dataWithRawString:@"Data with string"];
_date = [[CPDate alloc] initWithString:@"2011-10-05 16:34:38 +0900"];
}
- (void)tearDown
{
}
- (void)testTimeZoneWithAbbreviation
{
var timeZone = [CPTimeZone timeZoneWithAbbreviation:@"PDT"];
[self assert:[timeZone name] equals:@"America/Los_Angeles"];
[self assert:[timeZone abbreviation] equals:@"PDT"];
[self assert:[timeZone secondsFromGMT] equals:(-420 * 60)];
[self assert:[timeZone description] equals:@"America/Los_Angeles (PDT) offset -25200"];
[self assert:[timeZone localizedName:CPTimeZoneNameStyleStandard locale:_locale] equals:@"Pacific Standard Time"];
[self assert:[timeZone localizedName:CPTimeZoneNameStyleShortStandard locale:_locale] equals:@"PST"];
[self assert:[timeZone localizedName:CPTimeZoneNameStyleDaylightSaving locale:_locale] equals:@"Pacific Daylight Time"];
[self assert:[timeZone localizedName:CPTimeZoneNameStyleShortDaylightSaving locale:_locale] equals:@"PDT"];
[self assert:[timeZone localizedName:CPTimeZoneNameStyleGeneric locale:_locale] equals:@"Pacific Time"];
[self assert:[timeZone localizedName:CPTimeZoneNameStyleShortGeneric locale:_locale] equals:@"PT"];
}
- (void)testTimeZoneWithWrongAbbreviation
{
var timeZone = [CPTimeZone timeZoneWithAbbreviation:@"PDTezdez"];
[self assert:timeZone equals:nil];
}
- (void)testTimeZoneWithName
{
var timeZone = [CPTimeZone timeZoneWithName:@"America/Los_Angeles"];
// The current implementation can return daylight saving time or standard time depending on the current implmentation on CPDictionary
if ([timeZone abbreviation] === @"PDT") {
[self assert:[timeZone name] equals:@"America/Los_Angeles"];
[self assert:[timeZone abbreviation] equals:@"PDT"];
[self assert:[timeZone secondsFromGMT] equals:(-420 * 60)];
[self assert:[timeZone description] equals:@"America/Los_Angeles (PDT) offset -25200"];
} else {
[self assert:[timeZone name] equals:@"America/Los_Angeles"];
[self assert:[timeZone abbreviation] equals:@"PST"];
[self assert:[timeZone secondsFromGMT] equals:(-480 * 60)];
[self assert:[timeZone description] equals:@"America/Los_Angeles (PST) offset -28800"];
}
[self assert:[timeZone localizedName:CPTimeZoneNameStyleStandard locale:_locale] equals:@"Pacific Standard Time"];
[self assert:[timeZone localizedName:CPTimeZoneNameStyleShortStandard locale:_locale] equals:@"PST"];
[self assert:[timeZone localizedName:CPTimeZoneNameStyleDaylightSaving locale:_locale] equals:@"Pacific Daylight Time"];
[self assert:[timeZone localizedName:CPTimeZoneNameStyleShortDaylightSaving locale:_locale] equals:@"PDT"];
[self assert:[timeZone localizedName:CPTimeZoneNameStyleGeneric locale:_locale] equals:@"Pacific Time"];
[self assert:[timeZone localizedName:CPTimeZoneNameStyleShortGeneric locale:_locale] equals:@"PT"];
}
- (void)testTimeZoneWithWrongName
{
var timeZone = [CPTimeZone timeZoneWithName:@"America/Los_Angelesezdez"];
[self assert:timeZone equals:nil];
}
- (void)testexceptionTimeZoneWithNilName
{
try
{
[CPTimeZone timeZoneWithName:nil];
[self fail:"Invalid value provided for tzName"];
}
catch (e)
{
}
}
- (void)testTimeZoneWithNameWithData
{
var timeZone = [CPTimeZone timeZoneWithName:@"America/Los_Angeles" data:_data];
// The current implementation can return daylight saving time or standard time depending on the current implmentation on CPDictionary
if ([timeZone abbreviation] === @"PDT") {
[self assert:[timeZone name] equals:@"America/Los_Angeles"];
[self assert:[timeZone abbreviation] equals:@"PDT"];
[self assert:[timeZone secondsFromGMT] equals:(-420 * 60)];
[self assert:[timeZone description] equals:@"America/Los_Angeles (PDT) offset -25200"];
} else {
[self assert:[timeZone name] equals:@"America/Los_Angeles"];
[self assert:[timeZone abbreviation] equals:@"PST"];
[self assert:[timeZone secondsFromGMT] equals:(-480 * 60)];
[self assert:[timeZone description] equals:@"America/Los_Angeles (PST) offset -28800"];
}
[self assert:[timeZone data] equals:_data];
[self assert:[timeZone localizedName:CPTimeZoneNameStyleStandard locale:_locale] equals:@"Pacific Standard Time"];
[self assert:[timeZone localizedName:CPTimeZoneNameStyleShortStandard locale:_locale] equals:@"PST"];
[self assert:[timeZone localizedName:CPTimeZoneNameStyleDaylightSaving locale:_locale] equals:@"Pacific Daylight Time"];
[self assert:[timeZone localizedName:CPTimeZoneNameStyleShortDaylightSaving locale:_locale] equals:@"PDT"];
[self assert:[timeZone localizedName:CPTimeZoneNameStyleGeneric locale:_locale] equals:@"Pacific Time"];
[self assert:[timeZone localizedName:CPTimeZoneNameStyleShortGeneric locale:_locale] equals:@"PT"];
}
- (void)testTimeZoneWithWrongNameWithData
{
var timeZone = [CPTimeZone timeZoneWithName:@"America/Los_Angelesezdez" data:_data];
[self assert:timeZone equals:nil];
}
- (void)testexceptionTimeZoneWithNilNameWithData
{
try
{
[CPTimeZone timeZoneWithName:nil data:_data];
[self fail:"Invalid value provided for tzName"];
}
catch (e)
{
}
}
- (void)testTimeZoneWithSecondsFromGMT
{
var timeZone = [CPTimeZone timeZoneForSecondsFromGMT:(-600 * 60)];
[self assert:[timeZone name] equals:@"Pacific/Honolulu"];
[self assert:[timeZone abbreviation] equals:@"HST"];
[self assert:[timeZone secondsFromGMT] equals:(-600 * 60)];
[self assert:[timeZone description] equals:@"Pacific/Honolulu (HST) offset -36000"];
[self assert:[timeZone localizedName:CPTimeZoneNameStyleStandard locale:_locale] equals:@"Hawaii-Aleutian Standard Time"];
[self assert:[timeZone localizedName:CPTimeZoneNameStyleShortStandard locale:_locale] equals:@"HST"];
[self assert:[timeZone localizedName:CPTimeZoneNameStyleDaylightSaving locale:_locale] equals:@"Hawaii-Aleutian Daylight Time"];
[self assert:[timeZone localizedName:CPTimeZoneNameStyleShortDaylightSaving locale:_locale] equals:@"HDT"];
[self assert:[timeZone localizedName:CPTimeZoneNameStyleGeneric locale:_locale] equals:@"Hawaii-Aleutian Standard Time"];
[self assert:[timeZone localizedName:CPTimeZoneNameStyleShortGeneric locale:_locale] equals:@"HST"];
}
- (void)testTimeZoneWithWrongSecondsFromGMT
{
var timeZone = [CPTimeZone timeZoneForSecondsFromGMT:(-421 * 60)];
[self assert:timeZone equals:nil];
}
- (void)testInitTimeZoneWithName
{
var timeZone = [[CPTimeZone alloc] initWithName:@"America/Los_Angeles"];
// The current implementation can return daylight saving time or standard time depending on the current implmentation on CPDictionary
if ([timeZone abbreviation] === @"PDT") {
[self assert:[timeZone name] equals:@"America/Los_Angeles"];
[self assert:[timeZone abbreviation] equals:@"PDT"];
[self assert:[timeZone secondsFromGMT] equals:(-420 * 60)];
[self assert:[timeZone description] equals:@"America/Los_Angeles (PDT) offset -25200"];
} else {
[self assert:[timeZone name] equals:@"America/Los_Angeles"];
[self assert:[timeZone abbreviation] equals:@"PST"];
[self assert:[timeZone secondsFromGMT] equals:(-480 * 60)];
[self assert:[timeZone description] equals:@"America/Los_Angeles (PST) offset -28800"];
}
[self assert:[timeZone localizedName:CPTimeZoneNameStyleStandard locale:_locale] equals:@"Pacific Standard Time"];
[self assert:[timeZone localizedName:CPTimeZoneNameStyleShortStandard locale:_locale] equals:@"PST"];
[self assert:[timeZone localizedName:CPTimeZoneNameStyleDaylightSaving locale:_locale] equals:@"Pacific Daylight Time"];
[self assert:[timeZone localizedName:CPTimeZoneNameStyleShortDaylightSaving locale:_locale] equals:@"PDT"];
[self assert:[timeZone localizedName:CPTimeZoneNameStyleGeneric locale:_locale] equals:@"Pacific Time"];
[self assert:[timeZone localizedName:CPTimeZoneNameStyleShortGeneric locale:_locale] equals:@"PT"];
}
- (void)testInitTimeZoneWithWrongName
{
var timeZone = [[CPTimeZone alloc] initWithName:@"America/Los_Angelesezdez"];
[self assert:timeZone equals:nil];
}
- (void)testexceptionInitTimeZoneWithNilName
{
try
{
[[CPTimeZone alloc] initWithName:nil];
[self fail:"Invalid value provided for tzName"];
}
catch (e)
{
}
}
- (void)testInitTimeZoneWithNameWithData
{
var timeZone = [[CPTimeZone alloc] initWithName:@"America/Los_Angeles" data:_data];
// The current implementation can return daylight saving time or standard time depending on the current implmentation on CPDictionary
if ([timeZone abbreviation] === @"PDT") {
[self assert:[timeZone name] equals:@"America/Los_Angeles"];
[self assert:[timeZone abbreviation] equals:@"PDT"];
[self assert:[timeZone secondsFromGMT] equals:(-420 * 60)];
[self assert:[timeZone description] equals:@"America/Los_Angeles (PDT) offset -25200"];
} else {
[self assert:[timeZone name] equals:@"America/Los_Angeles"];
[self assert:[timeZone abbreviation] equals:@"PST"];
[self assert:[timeZone secondsFromGMT] equals:(-480 * 60)];
[self assert:[timeZone description] equals:@"America/Los_Angeles (PST) offset -28800"];
}
[self assert:[timeZone data] equals:_data];
[self assert:[timeZone localizedName:CPTimeZoneNameStyleStandard locale:_locale] equals:@"Pacific Standard Time"];
[self assert:[timeZone localizedName:CPTimeZoneNameStyleShortStandard locale:_locale] equals:@"PST"];
[self assert:[timeZone localizedName:CPTimeZoneNameStyleDaylightSaving locale:_locale] equals:@"Pacific Daylight Time"];
[self assert:[timeZone localizedName:CPTimeZoneNameStyleShortDaylightSaving locale:_locale] equals:@"PDT"];
[self assert:[timeZone localizedName:CPTimeZoneNameStyleGeneric locale:_locale] equals:@"Pacific Time"];
[self assert:[timeZone localizedName:CPTimeZoneNameStyleShortGeneric locale:_locale] equals:@"PT"];
}
- (void)testInitTimeZoneWithWrongNameWithData
{
var timeZone = [[CPTimeZone alloc] initWithName:@"America/Los_Angelesezdez" data:_data];
[self assert:timeZone equals:nil];
}
- (void)testexceptionInitTimeZoneWithNilNameWithData
{
try
{
[[CPTimeZone alloc] initWithName:nil data:_data];
[self fail:"Invalid value provided for tzName"];
}
catch (e)
{
}
}
- (void)testAbbreviationWithDate
{
var timeZone = [CPTimeZone localTimeZone],
abbreviation = [timeZone abbreviationForDate:_date];
// With the new robust implementation, we can't easily predict the exact
// abbreviation string without re-implementing the parsing logic.
// Instead, we verify that the returned abbreviation is valid and exists
// in the known dictionary, which is sufficient.
[self assert:(abbreviation !== nil) equals:YES];
var knownAbbreviations = [CPTimeZone abbreviationDictionary];
[self assert:[knownAbbreviations containsKey:abbreviation] equals:YES];
}
- (void)testAbbreviationWithNilDate
{
var timeZone = [CPTimeZone localTimeZone],
abbreviation = [timeZone abbreviationForDate:nil];
[self assert:abbreviation equals:nil];
}
- (void)testSecondsFromGMTForDate
{
var timeZone = [CPTimeZone localTimeZone],
seconds = [timeZone secondsFromGMTForDate:_date];
[self assert:seconds equals:(_date.getTimezoneOffset() * -60)];
}
- (void)testSecondsFromGMTForDateWithNilDate
{
var timeZone = [CPTimeZone localTimeZone],
seconds = [timeZone secondsFromGMTForDate:nil];
[self assert:seconds equals:nil];
}
- (void)testSecondsFromGMT
{
var timeZone = [[CPTimeZone alloc] initWithName:@"America/Los_Angeles"];
// The current implementation can return daylight saving time or standard time depending on the current implmentation on CPDictionary
if ([timeZone abbreviation] === @"PDT") {
[self assert:[timeZone secondsFromGMT] equals:(-420 * 60)];
} else {
[self assert:[timeZone secondsFromGMT] equals:(-480 * 60)];
}
}
- (void)testDescription
{
var timeZone = [[CPTimeZone alloc] initWithName:@"America/Los_Angeles"];
// The current implementation can return daylight saving time or standard time depending on the current implmentation on CPDictionary
if ([timeZone abbreviation] === @"PDT") {
[self assert:[timeZone description] equals:@"America/Los_Angeles (PDT) offset -25200"];
} else {
[self assert:[timeZone description] equals:@"America/Los_Angeles (PST) offset -28800"];
}
}
- (void)testLocalizedName
{
var timeZone = [[CPTimeZone alloc] initWithName:@"America/Los_Angeles"];
[self assert:[timeZone localizedName:CPTimeZoneNameStyleStandard locale:_locale] equals:@"Pacific Standard Time"];
[self assert:[timeZone localizedName:CPTimeZoneNameStyleShortStandard locale:_locale] equals:@"PST"];
[self assert:[timeZone localizedName:CPTimeZoneNameStyleDaylightSaving locale:_locale] equals:@"Pacific Daylight Time"];
[self assert:[timeZone localizedName:CPTimeZoneNameStyleShortDaylightSaving locale:_locale] equals:@"PDT"];
[self assert:[timeZone localizedName:CPTimeZoneNameStyleGeneric locale:_locale] equals:@"Pacific Time"];
[self assert:[timeZone localizedName:CPTimeZoneNameStyleShortGeneric locale:_locale] equals:@"PT"];
}
- (void)testEqualTrueToTimeZone
{
var timeZone1 = [[CPTimeZone alloc] initWithName:@"America/Los_Angeles"],
timeZone2 = [[CPTimeZone alloc] initWithName:@"America/Los_Angeles"];
[self assert:[timeZone1 isEqualToTimeZone:timeZone2] equals:YES];
}
- (void)testEqualFalseToTimeZone
{
var timeZone1 = [[CPTimeZone alloc] initWithName:@"America/Los_Angeles"],
timeZone2 = [[CPTimeZone alloc] initWithName:@"Pacific/Honolulu"];
[self assert:[timeZone1 isEqualToTimeZone:timeZone2] equals:NO];
}
- (void)testInitWithNameRespectsDaylightSaving
{
// This test verifies that initWithName: correctly selects the abbreviation
// (e.g., PST vs. PDT) based on the current date's daylight saving status.
// 1. Test a time zone that observes DST, like America/Los_Angeles.
var laTimeZoneName = @"America/Los_Angeles";
var expectedAbbreviationLA;
try {
// This logic mirrors the _abbreviationForNameAndDate helper in
// CPTimeZone.j: read the short time zone name directly from Intl,
// as an independent check that CPTimeZone's own use of the same
// API returns the same thing, not a duplicate of an algorithm.
var options = { timeZone: laTimeZoneName, timeZoneName: 'short' };
var parts = new Intl.DateTimeFormat('en-US', options).formatToParts(new Date());
var tzPart = parts.filter(function (p) { return p.type === 'timeZoneName'; })[0];
expectedAbbreviationLA = tzPart ? tzPart.value : nil;
} catch (e) {
[self fail:"Could not determine expected abbreviation for America/Los_Angeles"];
return;
}
var timeZoneLA = [[CPTimeZone alloc] initWithName:laTimeZoneName];
if (timeZoneLA == nil)
[self fail:"Time zone for America/Los_Angeles should be created successfully."];
[self assert:[timeZoneLA abbreviation] equals:expectedAbbreviationLA];
// 2. Test a time zone that does not observe DST, like Pacific/Honolulu.
var hnlTimeZoneName = @"Pacific/Honolulu";
var timeZoneHNL = [[CPTimeZone alloc] initWithName:hnlTimeZoneName];
if (timeZoneHNL == nil)
[self fail:"Time zone for Pacific/Honolulu should be created successfully."];
// For a non-DST zone, the abbreviation is constant.
[self assert:[timeZoneHNL abbreviation] equals:@"HST"];
}
// Pins the six timeDifferenceFromUTC entries corrected on CPTimeZoneTest-fix
// (MDT, MSK, NZDT, NZST, WAT, WIT) through the public API, so a regression
// back to any of the old wrong values is caught directly.
- (void)testCorrectedOffsets
{
[self assert:[[CPTimeZone timeZoneWithAbbreviation:@"MDT"] secondsFromGMT] equals:(-360 * 60)];
[self assert:[[CPTimeZone timeZoneWithAbbreviation:@"MSK"] secondsFromGMT] equals:(180 * 60)];
[self assert:[[CPTimeZone timeZoneWithAbbreviation:@"NZDT"] secondsFromGMT] equals:(780 * 60)];
[self assert:[[CPTimeZone timeZoneWithAbbreviation:@"NZST"] secondsFromGMT] equals:(720 * 60)];
[self assert:[[CPTimeZone timeZoneWithAbbreviation:@"WAT"] secondsFromGMT] equals:(60 * 60)];
[self assert:[[CPTimeZone timeZoneWithAbbreviation:@"WIT"] secondsFromGMT] equals:(420 * 60)];
}
// MSD ("Moscow Summer Time") has no correct current value: Russia abolished
// DST in 2014, so there is no live distinct summer offset for Moscow to
// assign. This pins the current, intentionally-unfixed value, so any future
// edit to it happens deliberately alongside CPTimeZone.j's comment and the
// tracked Intl redesign, not as a silent, unrelated drift.
- (void)testMSDKnownStaleValue
{
[self assert:[[CPTimeZone timeZoneWithAbbreviation:@"MSD"] secondsFromGMT] equals:(240 * 60)];
}
// knownTimeZoneNames should prefer the runtime's own IANA database over the
// old 48-city hardcoded list, on any engine that exposes
// Intl.supportedValuesOf. Skips itself where that API is unavailable, rather
// than asserting a specific engine capability.
- (void)testKnownTimeZoneNamesUsesIntlWhenAvailable
{
if (typeof Intl === "undefined" || typeof Intl.supportedValuesOf !== "function")
return;
var names = [CPTimeZone knownTimeZoneNames];
[self assertTrue:([names count] > 48)
message:"knownTimeZoneNames should use Intl.supportedValuesOf when available, not the small hardcoded fallback list"];
[self assertTrue:[names containsObject:@"Europe/Berlin"]
message:"a zone absent from the old hardcoded list should be present via Intl"];
}
// Disabled: the "fr" entry in localizedName is an empty dictionary, so any
// lookup for a French locale currently returns nil regardless of style. This
// is the same static, English-only table design as before the Intl
// migration; folding it in was deferred because CPTimeZoneNameStyleStandard/
// DaylightSaving need to force a specific state independent of the current
// date, which Intl.formatToParts(date) alone can't do for an arbitrary date.
// Tracked for the larger IANA-identity redesign, not fixed here.
- (void)disabled_testLocalizedNameFrenchLocale
{
var frenchLocale = [[CPLocale alloc] initWithLocaleIdentifier:@"fr_FR"],
timeZone = [CPTimeZone timeZoneWithAbbreviation:@"PST"];
[self assert:[timeZone localizedName:CPTimeZoneNameStyleStandard locale:frenchLocale] equals:@"Heure normale du Pacifique"];
}
// Disabled: timeZoneForSecondsFromGMT: does a linear scan of
// timeDifferenceFromUTC and returns the first matching key, with no defined
// tie-break when more than one abbreviation shares an offset. MDT and CST
// both now correctly resolve to -360; asking for that offset silently
// returns whichever CPDictionary happens to enumerate first, not a
// documented choice. The idealized contract is that an inherently ambiguous
// query should not silently guess one answer. No fix exists for this within
// the current flat abbreviation-to-offset table design.
- (void)disabled_testTimeZoneForSecondsFromGMTOffsetCollisionIsAmbiguous
{
var timeZone = [CPTimeZone timeZoneForSecondsFromGMT:(-360 * 60)];
[self assert:timeZone equals:nil];
}
@end