-
Notifications
You must be signed in to change notification settings - Fork 4.6k
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
TimeSpan.Parse(string) converts hour values > 24 to day values #101187
Comments
Tagging subscribers to this area: @dotnet/area-system-datetime |
Looks like a duplicate of #90214 |
That is right, this is a duplicate of #90214. We are not willing to change this behavior because of application compatibility. |
@tarekgh the linked issue doesn't list any "workaround" to the original problem. Are there any? What if someone wants to represent 48 hours, is that allowed somehow by changing the format of the string? Apologies if this was mentioned somewhere already. |
TimeSpan does not support hours outside the range -23 through 23. Such an hour value is not correct for TimeSpan. We have another proposal to support Duration #72064 which will allow such scenarios. For this specific case, if you are sure your format is always in form of "hh:mm:ss", you can manually parse it: public static TimeSpan Parsehhmmss(string value)
{
string[] parts = value.Split(':');
if (parts.Length == 3)
{
if (int.TryParse(parts[0], out int hours) && int.TryParse(parts[1], out int minutes) && int.TryParse(parts[2], out int seconds))
{
int days = Math.DivRem(hours, 24, out hours); // hours %= 24
return new TimeSpan(days, hours, minutes, seconds);
}
}
throw new FormatException("Invalid TimeSpan format");
} |
Description
When parsing a string like
25:00:00
,TimeSpan.Parse
returns an instance with 25 days instead.Reproduction Steps
The text was updated successfully, but these errors were encountered: