Skip to content

Commit 88cea1f

Browse files
pavkamhavrlisanAlex Ciobanu
authored
refactor: simplify IsValidTimeZone to delegate to GetTimeZone (#54)
* feat: add class function TBundledTimeZone.IsValidTimeZone * refactor: simplify IsValidTimeZone to delegate to GetTimeZone Removes duplicated lock acquisition, zone scanning, and alias scanning logic. GetTimeZone already handles all of this (including alias resolution), so the implementation reduces to a simple try/except. AIncludeAliases parameter is retained for API compatibility. Also adds IsValidTimeZone declaration and implementation to src/TZDBPK/TZDB.pas which was missing from the original PR. --------- Co-authored-by: Havrlišan <20707187+havrlisan@users.noreply.github.com> Co-authored-by: Alex Ciobanu <alex@ciobanu.org>
1 parent 37bcf1f commit 88cea1f

2 files changed

Lines changed: 33 additions & 0 deletions

File tree

dist/TZDB.pas

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,11 @@ TBundledTimeZone = class
208208
/// <exception cref="TZDB|ETimeZoneInvalid">The specified ID cannot be found in the bundled database.</exception>
209209
class function GetTimeZone(const ATimeZoneID: string): TBundledTimeZone;
210210

211+
/// <summary>Returns <c>True</c> if time zone exists.</summary>
212+
/// <param name="ATimeZoneID">The ID of the timezone to validate (ex. "Europe/Zagreb").</param>
213+
/// <param name="AIncludeAliases">Pass <c>True</c> to include time zone aliases into the validation.</param>
214+
class function IsValidTimeZone(const ATimeZoneID: string; const AIncludeAliases: Boolean = False): Boolean;
215+
211216
/// <summary>Returns the version of the TZDB component.</summary>
212217
/// <returns>A string representing the version of the source.</returns>
213218
class function Version: string;
@@ -12768,6 +12773,17 @@ class function TBundledTimeZone.GetTimezoneFromAlias(const AAliasID: string): st
1276812773
Result := GetTimeZone(AAliasID).ID;
1276912774
end;
1277012775

12776+
class function TBundledTimeZone.IsValidTimeZone(const ATimeZoneID: string; const AIncludeAliases: Boolean = False): Boolean;
12777+
begin
12778+
try
12779+
GetTimeZone(ATimeZoneID);
12780+
Result := True;
12781+
except
12782+
on E: ETimeZoneInvalid do
12783+
Result := False;
12784+
end;
12785+
end;
12786+
1277112787
function TBundledTimeZone.GetYearBreakdown(const AYear: Word): TYearSegmentArray;
1277212788
begin
1277312789
{ Guard for upper and lower date/time limits }

src/TZDBPK/TZDB.pas

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,12 @@ TBundledTimeZone = class
183183
/// <exception cref="TZDB|ETimeZoneInvalid">The specified ID cannot be found in the bundled database.</exception>
184184
class function GetTimeZone(const ATimeZoneID: string): TBundledTimeZone;
185185

186+
/// <summary>Checks if a given time zone ID is valid.</summary>
187+
/// <param name="ATimeZoneID">The ID of the timezone to validate (ex. "Europe/Zagreb").</param>
188+
/// <param name="AIncludeAliases">Pass <c>True</c> to include time zone aliases into the validation.</param>
189+
/// <returns><c>True</c> if the time zone ID is valid; <c>False</c> otherwise.</returns>
190+
class function IsValidTimeZone(const ATimeZoneID: string; const AIncludeAliases: Boolean = False): Boolean;
191+
186192
/// <summary>Returns the version of the TZDB component.</summary>
187193
/// <returns>A string representing the version of the source.</returns>
188194
class function Version: string;
@@ -1767,6 +1773,17 @@ class function TBundledTimeZone.GetTimeZone(const ATimeZoneID: string): TBundled
17671773
end;
17681774
end;
17691775

1776+
class function TBundledTimeZone.IsValidTimeZone(const ATimeZoneID: string; const AIncludeAliases: Boolean = False): Boolean;
1777+
begin
1778+
try
1779+
GetTimeZone(ATimeZoneID);
1780+
Result := True;
1781+
except
1782+
on E: ETimeZoneInvalid do
1783+
Result := False;
1784+
end;
1785+
end;
1786+
17701787
class function TBundledTimeZone.GetTimezoneFromAlias(const AAliasID: string): string;
17711788
begin
17721789
Result := GetTimeZone(AAliasID).ID;

0 commit comments

Comments
 (0)