Skip to content
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

Closed
NikoUmbleLSPA opened this issue Apr 17, 2024 · 5 comments
Closed

TimeSpan.Parse(string) converts hour values > 24 to day values #101187

NikoUmbleLSPA opened this issue Apr 17, 2024 · 5 comments

Comments

@NikoUmbleLSPA
Copy link

Description

When parsing a string like 25:00:00, TimeSpan.Parse returns an instance with 25 days instead.

image

Reproduction Steps

TimeSpan ts = TimeSpan.Parse("25:00:00");

### Expected behavior

Instance is set to 25 hours

### Actual behavior

Instance is set to 25 days

### Regression?

_No response_

### Known Workarounds

_No response_

### Configuration

Microsoft .NET Framework Version 4.8.09032

### Other information

Seems to be a regression of https://github.com/dotnet/runtime/issues/40316, 
which was closed as completed on [Aug 4, 2020](https://github.com/dotnet/runtime/issues/40316#event-3621743601)
@dotnet-issue-labeler dotnet-issue-labeler bot added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label Apr 17, 2024
@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Apr 17, 2024
@vcsjones vcsjones added area-System.DateTime and removed needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners labels Apr 17, 2024
Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-system-datetime
See info in area-owners.md if you want to be subscribed.

@campersau
Copy link
Contributor

Looks like a duplicate of #90214

@tarekgh
Copy link
Member

tarekgh commented Jun 20, 2024

That is right, this is a duplicate of #90214. We are not willing to change this behavior because of application compatibility.

@tarekgh tarekgh closed this as completed Jun 20, 2024
@dotnet-policy-service dotnet-policy-service bot removed the untriaged New issue has not been triaged by the area owner label Jun 20, 2024
@julealgon
Copy link

@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.

@tarekgh
Copy link
Member

tarekgh commented Jun 20, 2024

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");
        }

@github-actions github-actions bot locked and limited conversation to collaborators Jul 21, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants