File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -189,6 +189,19 @@ $timezones->findByIdentifier(iana: 'Asia/Tokyo'); # Timezone("Asia/Tokyo")
189189$timezones->findByIdentifier(iana: 'Europe/London'); # null
190190```
191191
192+ #### Finding a timezone by identifier with UTC fallback
193+
194+ Searches for a specific IANA identifier within the collection. Returns UTC if not found.
195+
196+ ``` php
197+ use TinyBlocks\Time\Timezones;
198+
199+ $timezones = Timezones::fromStrings('UTC', 'America/Sao_Paulo', 'Asia/Tokyo');
200+
201+ $timezones->findByIdentifierOrUtc(iana: 'Asia/Tokyo'); # Timezone("Asia/Tokyo")
202+ $timezones->findByIdentifierOrUtc(iana: 'Europe/London'); # Timezone("UTC")
203+ ```
204+
192205#### Checking if a timezone exists in the collection
193206
194207``` php
Original file line number Diff line number Diff line change @@ -99,6 +99,17 @@ public function findByIdentifier(string $iana): ?Timezone
9999 );
100100 }
101101
102+ /**
103+ * Finds a Timezone by its IANA identifier, falling back to UTC if not found.
104+ *
105+ * @param string $iana The IANA timezone identifier to search for (e.g. America/Sao_Paulo).
106+ * @return Timezone The matching Timezone, or UTC if not found in this collection.
107+ */
108+ public function findByIdentifierOrUtc (string $ iana ): Timezone
109+ {
110+ return $ this ->findByIdentifier (iana: $ iana ) ?? Timezone::utc ();
111+ }
112+
102113 /**
103114 * Returns all timezone identifiers as plain strings.
104115 *
Original file line number Diff line number Diff line change @@ -112,6 +112,30 @@ public function testTimezonesFindByIdentifierReturnsNullWhenNotFound(): void
112112 self ::assertNull ($ found );
113113 }
114114
115+ public function testTimezonesFindByIdentifierOrUtcReturnsMatchingTimezone (): void
116+ {
117+ /** @Given a Timezones collection with multiple identifiers */
118+ $ timezones = Timezones::fromStrings ('UTC ' , 'America/Sao_Paulo ' , 'Asia/Tokyo ' );
119+
120+ /** @When searching for an existing identifier */
121+ $ found = $ timezones ->findByIdentifierOrUtc (iana: 'Asia/Tokyo ' );
122+
123+ /** @Then the matching Timezone should be returned */
124+ self ::assertSame ('Asia/Tokyo ' , $ found ->value );
125+ }
126+
127+ public function testTimezonesFindByIdentifierOrUtcReturnsUtcWhenNotFound (): void
128+ {
129+ /** @Given a Timezones collection without Europe/London */
130+ $ timezones = Timezones::fromStrings ('America/Sao_Paulo ' , 'Asia/Tokyo ' );
131+
132+ /** @When searching for a non-existing identifier */
133+ $ found = $ timezones ->findByIdentifierOrUtc (iana: 'Europe/London ' );
134+
135+ /** @Then UTC should be returned as fallback */
136+ self ::assertSame ('UTC ' , $ found ->value );
137+ }
138+
115139 public function testTimezonesCountMatchesAllSize (): void
116140 {
117141 /** @Given a Timezones collection with four items */
You can’t perform that action at this time.
0 commit comments