Skip to content

Commit c2d3bb1

Browse files
author
Alex Ciobanu
committed
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.
1 parent 55044b3 commit c2d3bb1

2 files changed

Lines changed: 22 additions & 50 deletions

File tree

dist/TZDB.pas

Lines changed: 5 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -12765,58 +12765,13 @@ class function TBundledTimeZone.GetTimezoneFromAlias(const AAliasID: string): st
1276512765
end;
1276612766

1276712767
class function TBundledTimeZone.IsValidTimeZone(const ATimeZoneID: string; const AIncludeAliases: Boolean = False): Boolean;
12768-
var
12769-
LIndex: Integer;
12770-
LTimeZoneID: string;
1277112768
begin
12772-
Result := False;
12773-
{ Access the cache }
12774-
{$IFDEF DELPHI}
12775-
MonitorEnter(FTimeZoneCache);
12776-
{$ELSE}
12777-
FTimeZoneCacheLock.Enter();
12778-
{$ENDIF}
1277912769
try
12780-
{ Check TZ is cached }
12781-
{$IFNDEF FPC}
12782-
if FTimeZoneCache.ContainsKey(UpperCase(ATimeZoneID)) then
12783-
{$ELSE}
12784-
if FTimeZoneCache.IndexOf(UpperCase(ATimeZoneID)) > -1 then
12785-
{$ENDIF}
12786-
Result := True
12787-
else
12788-
begin
12789-
{ First, search in the CZones array }
12790-
for LIndex := Low(CZones) to High(CZones) do
12791-
if SameText(CZones[LIndex].FName, ATimeZoneID) then
12792-
begin
12793-
Result := True;
12794-
break;
12795-
end;
12796-
12797-
{ Second, search in the aliases array (if AIncludeAliases }
12798-
if not Result and AIncludeAliases then
12799-
begin
12800-
{$IFDEF MSWINDOWS}
12801-
if not GetNonLocalizedTZName(ATimeZoneID, LTimeZoneID) then
12802-
LTimeZoneID := ATimeZoneID;
12803-
{$ELSE}
12804-
LTimeZoneID := ATimeZoneID;
12805-
{$ENDIF}
12806-
for LIndex := Low(CAliases) to High(CAliases) do
12807-
if SameText(CAliases[LIndex].FName, LTimeZoneID) then
12808-
begin
12809-
Result := True;
12810-
break;
12811-
end;
12812-
end;
12813-
end;
12814-
finally
12815-
{$IFDEF DELPHI}
12816-
MonitorExit(FTimeZoneCache);
12817-
{$ELSE}
12818-
FTimeZoneCacheLock.Leave;
12819-
{$ENDIF}
12770+
GetTimeZone(ATimeZoneID);
12771+
Result := True;
12772+
except
12773+
on E: ETimeZoneInvalid do
12774+
Result := False;
1282012775
end;
1282112776
end;
1282212777

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;
@@ -1750,6 +1756,17 @@ class function TBundledTimeZone.GetTimeZone(const ATimeZoneID: string): TBundled
17501756
end;
17511757
end;
17521758

1759+
class function TBundledTimeZone.IsValidTimeZone(const ATimeZoneID: string; const AIncludeAliases: Boolean = False): Boolean;
1760+
begin
1761+
try
1762+
GetTimeZone(ATimeZoneID);
1763+
Result := True;
1764+
except
1765+
on E: ETimeZoneInvalid do
1766+
Result := False;
1767+
end;
1768+
end;
1769+
17531770
class function TBundledTimeZone.GetTimezoneFromAlias(const AAliasID: string): string;
17541771
begin
17551772
Result := GetTimeZone(AAliasID).ID;

0 commit comments

Comments
 (0)