Skip to content

Commit

Permalink
converting if-else to switch statement
Browse files Browse the repository at this point in the history
moving the common code to timezoneinfo.cs
  • Loading branch information
AlexRadch committed Jun 27, 2023
1 parent 624036c commit 2899919
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -271,41 +271,13 @@ private static TimeZoneInfoResult TryGetTimeZoneFromLocalMachine(string id, out

/// <summary>
/// Helper function for retrieving a TimeZoneInfo object by time_zone_name.
/// This function wraps the logic necessary to keep the private
/// SystemTimeZones cache in working order.
///
/// This function will either return:
/// <c>TimeZoneInfoResult.Success</c> and a valid <see cref="TimeZoneInfo"/>instance and <c>null</c> Exception or
/// <c>TimeZoneInfoResult.TimeZoneNotFoundException</c> and <c>null</c> <see cref="TimeZoneInfo"/> and Exception (can be null) or
/// other <c>TimeZoneInfoResult</c> and <c>null</c> <see cref="TimeZoneInfo"/> and valid Exception.
/// This function may return null.
///
/// assumes cachedData lock is taken
/// </summary>
private static TimeZoneInfoResult TryFindSystemTimeZoneById(string id, out TimeZoneInfo? timeZone, out Exception? e)
{
// Special case for Utc as it will not exist in the dictionary with the rest
// of the system time zones. There is no need to do this check for Local.Id
// since Local is a real time zone that exists in the dictionary cache
if (string.Equals(id, UtcId, StringComparison.OrdinalIgnoreCase))
{
timeZone = Utc;
e = default;
return TimeZoneInfoResult.Success;
}

ArgumentNullException.ThrowIfNull(id);
if (id.Length == 0 || id.Contains('\0'))
{
timeZone = default;
e = default;
return TimeZoneInfoResult.TimeZoneNotFoundException;
}

CachedData cachedData = s_cachedData;

lock (cachedData)
{
return TryGetTimeZone(id, false, out timeZone, out e, cachedData, alwaysFallbackToLocalMachine: true);
}
}
private static TimeZoneInfoResult TryGetTimeZone(string id, out TimeZoneInfo? timeZone, out Exception? e, CachedData cachedData)
=> TryGetTimeZone(id, false, out timeZone, out e, cachedData, alwaysFallbackToLocalMachine: true);

// DateTime.Now fast path that avoids allocating an historically accurate TimeZoneInfo.Local and just creates a 1-year (current year) accurate time zone
internal static TimeSpan GetDateTimeNowUtcOffsetFromUtc(DateTime time, out bool isAmbiguousLocalDst)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -314,41 +314,13 @@ private static TimeZoneInfo GetLocalTimeZoneFromWin32Data(in TIME_ZONE_INFORMATI

/// <summary>
/// Helper function for retrieving a TimeZoneInfo object by time_zone_name.
/// This function wraps the logic necessary to keep the private
/// SystemTimeZones cache in working order.
///
/// This function will either return:
/// <c>TimeZoneInfoResult.Success</c> and a valid <see cref="TimeZoneInfo"/>instance and <c>null</c> Exception or
/// <c>TimeZoneInfoResult.TimeZoneNotFoundException</c> and <c>null</c> <see cref="TimeZoneInfo"/> and Exception (can be null) or
/// other <c>TimeZoneInfoResult</c> and <c>null</c> <see cref="TimeZoneInfo"/> and valid Exception.
/// This function may return null.
///
/// assumes cachedData lock is taken
/// </summary>
private static TimeZoneInfoResult TryFindSystemTimeZoneById(string id, out TimeZoneInfo? timeZone, out Exception? e)
{
// Special case for Utc as it will not exist in the dictionary with the rest
// of the system time zones. There is no need to do this check for Local.Id
// since Local is a real time zone that exists in the dictionary cache
if (string.Equals(id, UtcId, StringComparison.OrdinalIgnoreCase))
{
timeZone = Utc;
e = default;
return TimeZoneInfoResult.Success;
}

ArgumentNullException.ThrowIfNull(id);
if (id.Length == 0 || id.Length > MaxKeyLength || id.Contains('\0'))
{
timeZone = default;
e = default;
return TimeZoneInfoResult.TimeZoneNotFoundException;
}

CachedData cachedData = s_cachedData;

lock (cachedData)
{
return TryGetTimeZone(id, false, out timeZone, out e, cachedData);
}
}
private static TimeZoneInfoResult TryGetTimeZone(string id, out TimeZoneInfo? timeZone, out Exception? e, CachedData cachedData)
=> TryGetTimeZone(id, false, out timeZone, out e, cachedData);

// DateTime.Now fast path that avoids allocating an historically accurate TimeZoneInfo.Local and just creates a 1-year (current year) accurate time zone
internal static TimeSpan GetDateTimeNowUtcOffsetFromUtc(DateTime time, out bool isAmbiguousLocalDst)
Expand Down
68 changes: 50 additions & 18 deletions src/libraries/System.Private.CoreLib/src/System/TimeZoneInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -574,24 +574,18 @@ public static TimeZoneInfo FindSystemTimeZoneById(string id)
Exception? e;

TimeZoneInfoResult result = TryFindSystemTimeZoneById(id, out value, out e);

if (result == TimeZoneInfoResult.Success)
{
return value!;
}
else if (result == TimeZoneInfoResult.InvalidTimeZoneException)
{
Debug.Assert(e is InvalidTimeZoneException,
"TryGetTimeZone must create an InvalidTimeZoneException when it returns TimeZoneInfoResult.InvalidTimeZoneException");
throw e;
}
else if (result == TimeZoneInfoResult.SecurityException)
{
throw new SecurityException(SR.Format(SR.Security_CannotReadFileData, id), e);
}
else
{
throw new TimeZoneNotFoundException(SR.Format(SR.TimeZoneNotFound_MissingData, id), e);
switch (result)
{
case TimeZoneInfoResult.Success:
return value!;
case TimeZoneInfoResult.InvalidTimeZoneException:
Debug.Assert(e is InvalidTimeZoneException,
"TryGetTimeZone must create an InvalidTimeZoneException when it returns TimeZoneInfoResult.InvalidTimeZoneException");
throw e;
case TimeZoneInfoResult.SecurityException:
throw new SecurityException(SR.Format(SR.Security_CannotReadFileData, id), e);
default:
throw new TimeZoneNotFoundException(SR.Format(SR.TimeZoneNotFound_MissingData, id), e);
}
}

Expand All @@ -609,6 +603,44 @@ public static TimeZoneInfo FindSystemTimeZoneById(string id)
public static bool TryFindSystemTimeZoneById(string id, [NotNullWhenAttribute(true)] out TimeZoneInfo? timeZoneInfo)
=> TryFindSystemTimeZoneById(id, out timeZoneInfo, out _) == TimeZoneInfoResult.Success;

/// <summary>
/// Helper function for retrieving a TimeZoneInfo object by time_zone_name.
/// This function wraps the logic necessary to keep the private
/// SystemTimeZones cache in working order.
///
/// This function will either return:
/// <c>TimeZoneInfoResult.Success</c> and a valid <see cref="TimeZoneInfo"/>instance and <c>null</c> Exception or
/// <c>TimeZoneInfoResult.TimeZoneNotFoundException</c> and <c>null</c> <see cref="TimeZoneInfo"/> and Exception (can be null) or
/// other <c>TimeZoneInfoResult</c> and <c>null</c> <see cref="TimeZoneInfo"/> and valid Exception.
/// </summary>
private static TimeZoneInfoResult TryFindSystemTimeZoneById(string id, out TimeZoneInfo? timeZone, out Exception? e)
{
// Special case for Utc as it will not exist in the dictionary with the rest
// of the system time zones. There is no need to do this check for Local.Id
// since Local is a real time zone that exists in the dictionary cache
if (string.Equals(id, UtcId, StringComparison.OrdinalIgnoreCase))
{
timeZone = Utc;
e = default;
return TimeZoneInfoResult.Success;
}

ArgumentNullException.ThrowIfNull(id);
if (id.Length == 0 || id.Length > MaxKeyLength || id.Contains('\0'))
{
timeZone = default;
e = default;
return TimeZoneInfoResult.TimeZoneNotFoundException;
}

CachedData cachedData = s_cachedData;

lock (cachedData)
{
return TryGetTimeZone(id, out timeZone, out e, cachedData);
}
}

/// <summary>
/// Converts the value of a DateTime object from sourceTimeZone to destinationTimeZone.
/// </summary>
Expand Down

0 comments on commit 2899919

Please sign in to comment.