-
Notifications
You must be signed in to change notification settings - Fork 4.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[API Proposal]: TryFindSystemTimeZoneById #66928
Comments
I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label. |
Tagging subscribers to this area: @dotnet/area-system-runtime Issue DetailsBackground and motivationThe method for finding a timezone by a string ( I use a version of the method below to attempt to find a timezone from a string, and fallback to local time if one's not found: public static TimeZoneInfo GetTimeZone() =>
DateTimeUtil.TryGetTimeZoneById(Settings.Default.TimeZone, out var timeZoneInfo) ? timeZoneInfo : TimeZoneInfo.Local; This is of course still possible using the current method but it's less clean (IMO). API Proposalnamespace System
{
public class TimeZoneInfo
{
public static bool TryFindSystemTimeZoneById(string id, out TimeZoneInfo timeZoneInfo)
{
try
{
timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById(id);
return true;
}
catch (TimeZoneNotFoundException)
{
timeZoneInfo = null;
return false;
}
}
}
} API Usage// Ask user for timezone
Console.WriteLine("Enter a timezone");
var tz = Console.ReadLine();
// Validate
if(TimeZoneInfo.TryFindSystemTimeZoneById(tz, out var tzInfo))
Console.WriteLine("Using entered timezone");
else
Console.WriteLine("Invalid timezone"); // Ask user for timezone
Console.WriteLine("Enter a timezone");
var tz = Console.ReadLine();
// Try to find timezone or fall back to local time
var finalTimeZone = TimeZoneInfo.TryFindTimeZoneById(tz, out var tzInfo) ? tzInfo : TimeZoneInfo.Local;
Console.WriteLine("Using " + finalTimeZone.DisplayName); Alternative DesignsNo response RisksSame risks as adding any new API
|
I implemented the very same workaround this past week. I was surprised there isnt a Try-/non-throwing variant. We accept the TZ name as user-provided input so can't rely on it being valid. The workaround isn't that bad, but it'd be nice to have a way to do a lookup without the exception. |
CC. @tarekgh, could you provide insight into whether this is something we should look at providing? |
@tannergooding this is a nice to have helper method but not necessary blocking any scenario. It is easy to have the apps adding extension method that does that. |
Do you think this is in the right shape for us to mark |
The API shape looks good to me. |
Looks good as proposed (ignoring that the body says to do a try/catch). namespace System
{
public class TimeZoneInfo
{
public static bool TryFindSystemTimeZoneById(string id, out TimeZoneInfo timeZoneInfo);
}
} |
Background and motivation
The method for finding a timezone by a string,
TimeZoneInfo.FindSystemTimeZoneById
, throws aTimeZoneNotFoundException
if it can't find one. AFAIK there isn't a non-throwing method in the runtime.I use a version of the method below to attempt to find a time zone or fallback to local time:
This is of course still possible using the current method but it's not as clean IMO.
API Proposal
API Usage
Alternative Designs
No response
Risks
No response
The text was updated successfully, but these errors were encountered: