Skip to content

Commit a450fb0

Browse files
authored
Release/1.1.0 (#2)
1 parent 4ae208c commit a450fb0

3 files changed

Lines changed: 48 additions & 0 deletions

File tree

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff 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

src/Timezones.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff 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
*

tests/TimezonesTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff 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 */

0 commit comments

Comments
 (0)