Update CPTimeZoneTest temporal baselines

Expand test coverage for timezone parsing, offset evaluation, and GMT validation.

Establishes a strict temporal baseline and formally exposes existing logic flaws in CPTimeZone prior to applying implementation fixes.
This commit is contained in:
David Richardson committed 2026-09-11 18:28:15 -06:00
1 parent f56855010f
commit 71c2501ff6
2 files changed
+778 -666

No files matched your search

+699 -564
View File
File diff suppressed because it is too large. Load diff
+79 -102
View File
@@ -1,23 +1,23 @@
/* 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
*/
* 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>
@@ -66,7 +66,7 @@
{
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
// The current implementation can return daylight saving time or standard time depending on the current date
if ([timeZone abbreviation] === @"PDT") {
[self assert:[timeZone name] equals:@"America/Los_Angeles"];
[self assert:[timeZone abbreviation] equals:@"PDT"];
@@ -96,8 +96,8 @@
{
try
{
[CPTimeZone timeZoneWithName:nil];
[self fail:"Invalid value provided for tzName"];
[CPTimeZone timeZoneWithName:nil];
[self fail:"Invalid value provided for tzName"];
}
catch (e)
{
@@ -109,7 +109,6 @@
{
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"];
@@ -140,8 +139,8 @@
{
try
{
[CPTimeZone timeZoneWithName:nil data:_data];
[self fail:"Invalid value provided for tzName"];
[CPTimeZone timeZoneWithName:nil data:_data];
[self fail:"Invalid value provided for tzName"];
}
catch (e)
{
@@ -151,30 +150,37 @@
- (void)testTimeZoneWithSecondsFromGMT
{
// timeZoneForSecondsFromGMT returns a fixed-offset zone. It does not
// back-resolve to geographical regions (like Pacific/Honolulu).
var timeZone = [CPTimeZone timeZoneForSecondsFromGMT:(-600 * 60)];
[self assert:[timeZone name] equals:@"Pacific/Honolulu"];
[self assert:[timeZone abbreviation] equals:@"HST"];
[self assert:[timeZone name] equals:@"GMT-10:00"];
[self assert:[timeZone abbreviation] equals:@"GMT-10:00"];
[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"];
[self assert:[timeZone description] equals:@"GMT-10:00 (GMT-10:00) offset -36000"];
[self assert:[timeZone localizedName:CPTimeZoneNameStyleStandard locale:_locale] equals:@"GMT-10:00"];
[self assert:[timeZone localizedName:CPTimeZoneNameStyleShortStandard locale:_locale] equals:@"GMT-10:00"];
[self assert:[timeZone localizedName:CPTimeZoneNameStyleDaylightSaving locale:_locale] equals:@"GMT-10:00"];
[self assert:[timeZone localizedName:CPTimeZoneNameStyleShortDaylightSaving locale:_locale] equals:@"GMT-10:00"];
[self assert:[timeZone localizedName:CPTimeZoneNameStyleGeneric locale:_locale] equals:@"GMT-10:00"];
[self assert:[timeZone localizedName:CPTimeZoneNameStyleShortGeneric locale:_locale] equals:@"GMT-10:00"];
}
- (void)testTimeZoneWithWrongSecondsFromGMT
{
// +/- 18 hours is the limit. -421 * 60 is valid and creates a fixed zone.
var timeZone = [CPTimeZone timeZoneForSecondsFromGMT:(-421 * 60)];
[self assert:timeZone equals:nil];
[self assert:[timeZone name] equals:@"GMT-07:01"];
[self assert:[timeZone secondsFromGMT] equals:(-421 * 60)];
// An offset strictly outside the +/- 18 hour range should return nil.
var invalidTimeZone = [CPTimeZone timeZoneForSecondsFromGMT:(19 * 3600)];
[self assert:invalidTimeZone 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"];
@@ -204,8 +210,8 @@
{
try
{
[[CPTimeZone alloc] initWithName:nil];
[self fail:"Invalid value provided for tzName"];
[[CPTimeZone alloc] initWithName:nil];
[self fail:"Invalid value provided for tzName"];
}
catch (e)
{
@@ -217,7 +223,6 @@
{
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"];
@@ -248,8 +253,8 @@
{
try
{
[[CPTimeZone alloc] initWithName:nil data:_data];
[self fail:"Invalid value provided for tzName"];
[[CPTimeZone alloc] initWithName:nil data:_data];
[self fail:"Invalid value provided for tzName"];
}
catch (e)
{
@@ -260,12 +265,8 @@
- (void)testAbbreviationWithDate
{
var timeZone = [CPTimeZone localTimeZone],
abbreviation = [timeZone abbreviationForDate:_date];
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];
@@ -274,7 +275,7 @@
- (void)testAbbreviationWithNilDate
{
var timeZone = [CPTimeZone localTimeZone],
abbreviation = [timeZone abbreviationForDate:nil];
abbreviation = [timeZone abbreviationForDate:nil];
[self assert:abbreviation equals:nil];
}
@@ -283,7 +284,7 @@
- (void)testSecondsFromGMTForDate
{
var timeZone = [CPTimeZone localTimeZone],
seconds = [timeZone secondsFromGMTForDate:_date];
seconds = [timeZone secondsFromGMTForDate:_date];
[self assert:seconds equals:(_date.getTimezoneOffset() * -60)];
}
@@ -291,7 +292,7 @@
- (void)testSecondsFromGMTForDateWithNilDate
{
var timeZone = [CPTimeZone localTimeZone],
seconds = [timeZone secondsFromGMTForDate:nil];
seconds = [timeZone secondsFromGMTForDate:nil];
[self assert:seconds equals:nil];
}
@@ -300,7 +301,6 @@
{
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 {
@@ -312,7 +312,6 @@
{
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 {
@@ -334,7 +333,7 @@
- (void)testEqualTrueToTimeZone
{
var timeZone1 = [[CPTimeZone alloc] initWithName:@"America/Los_Angeles"],
timeZone2 = [[CPTimeZone alloc] initWithName:@"America/Los_Angeles"];
timeZone2 = [[CPTimeZone alloc] initWithName:@"America/Los_Angeles"];
[self assert:[timeZone1 isEqualToTimeZone:timeZone2] equals:YES];
}
@@ -342,25 +341,17 @@
- (void)testEqualFalseToTimeZone
{
var timeZone1 = [[CPTimeZone alloc] initWithName:@"America/Los_Angeles"],
timeZone2 = [[CPTimeZone alloc] initWithName:@"Pacific/Honolulu"];
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];
@@ -377,44 +368,46 @@
[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)];
// Validate IANA name resolution for legacy abbreviations.
[self assert:[[CPTimeZone timeZoneWithAbbreviation:@"MDT"] name] equals:@"America/Denver"];
[self assert:[[CPTimeZone timeZoneWithAbbreviation:@"MSK"] name] equals:@"Europe/Moscow"];
[self assert:[[CPTimeZone timeZoneWithAbbreviation:@"NZDT"] name] equals:@"Pacific/Auckland"];
[self assert:[[CPTimeZone timeZoneWithAbbreviation:@"NZST"] name] equals:@"Pacific/Auckland"];
[self assert:[[CPTimeZone timeZoneWithAbbreviation:@"WAT"] name] equals:@"Africa/Lagos"];
[self assert:[[CPTimeZone timeZoneWithAbbreviation:@"WIT"] name] equals:@"Asia/Jakarta"];
// Evaluate against fixed dates to prevent seasonal failures during CI runs.
var janDate = [[CPDate alloc] initWithString:@"2026-01-15 12:00:00 +0000"];
var julDate = [[CPDate alloc] initWithString:@"2026-07-15 12:00:00 +0000"];
[self assert:[[CPTimeZone timeZoneWithAbbreviation:@"MDT"] secondsFromGMTForDate:julDate] equals:(-360 * 60)];
[self assert:[[CPTimeZone timeZoneWithAbbreviation:@"MSK"] secondsFromGMTForDate:janDate] equals:(180 * 60)];
[self assert:[[CPTimeZone timeZoneWithAbbreviation:@"NZDT"] secondsFromGMTForDate:janDate] equals:(780 * 60)];
[self assert:[[CPTimeZone timeZoneWithAbbreviation:@"NZST"] secondsFromGMTForDate:julDate] equals:(720 * 60)];
[self assert:[[CPTimeZone timeZoneWithAbbreviation:@"WAT"] secondsFromGMTForDate:janDate] equals:(60 * 60)];
[self assert:[[CPTimeZone timeZoneWithAbbreviation:@"WIT"] secondsFromGMTForDate:janDate] 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
- (void)testMSKCorrectCurrentValue
{
[self assert:[[CPTimeZone timeZoneWithAbbreviation:@"MSD"] secondsFromGMT] equals:(240 * 60)];
// MSD/MSK resolve to Europe/Moscow.
// Russia abolished DST in 2014, so there is no distinct summer offset.
// The current permanent offset is +03:00 (10800 seconds).
var timeZone = [CPTimeZone timeZoneWithAbbreviation:@"MSD"];
[self assert:[timeZone secondsFromGMT] equals:(180 * 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")
@@ -423,38 +416,22 @@
var names = [CPTimeZone knownTimeZoneNames];
[self assertTrue:([names count] > 48)
message:"knownTimeZoneNames should use Intl.supportedValuesOf when available, not the small hardcoded fallback list"];
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"];
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"];
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];
}