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

TimeOnly formats milliseconds wrong with format 'o' #51482

Closed
devsko opened this issue Apr 19, 2021 · 3 comments · Fixed by #51484
Closed

TimeOnly formats milliseconds wrong with format 'o' #51482

devsko opened this issue Apr 19, 2021 · 3 comments · Fixed by #51484

Comments

@devsko
Copy link
Contributor

devsko commented Apr 19, 2021

It seems that with format 'o' TimeOnly.TryFormat() always fills the whole buffer after the decimal point with '0' and writes 7 decimal places right aligned at the end of the buffer. Regardless it always sets charsWritten = 16.

        static void Main(string[] args)
        {
            Span<char> buffer = new char[22];
            var time = new TimeOnly(1, 1, 1, 123);

            time.TryFormat(buffer, out int written, "O");
            
            Console.WriteLine('"' + buffer.Slice(0, written).ToString() + '"');
            // => "01:01:01.0000001" instead of "01:01:01.1230000"
            
            Console.WriteLine('"' + buffer.ToString() + '"');
            // => "01:01:01.0000001230000" TryFormat writes more than charsWritten characters

            Console.WriteLine('"' + time.ToString("O") + '"');
            // => "01:01:01.1230000" correct

            Console.WriteLine(string.Format("\"{0:O}\"", time)); 
            // => "01:01:01.0000000" no milliseconds at all (they are far to the right)
        }

I think the bug is here

WriteDigits((uint)fraction, destination.Slice(9));

It should be

 WriteDigits((uint)fraction, destination.Slice(9, 7));
@dotnet-issue-labeler
Copy link

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.

@dotnet-issue-labeler dotnet-issue-labeler bot added the untriaged New issue has not been triaged by the area owner label Apr 19, 2021
@ghost ghost added the in-pr There is an active PR which will close this issue when it is merged label Apr 19, 2021
@Clockwork-Muse
Copy link
Contributor

.... I think I'm more surprised that ToString() appears to have its own codepath here...

@ghost
Copy link

ghost commented Apr 19, 2021

Tagging subscribers to this area: @tannergooding
See info in area-owners.md if you want to be subscribed.

Issue Details

It seems that with format 'o' TimeOnly.TryFormat() always fills the whole buffer after the decimal point with '0' and writes 7 decimal places right aligned at the end of the buffer. Regardless it always sets charsWritten = 16.

        static void Main(string[] args)
        {
            Span<char> buffer = new char[22];
            var time = new TimeOnly(1, 1, 1, 123);

            time.TryFormat(buffer, out int written, "O");
            
            Console.WriteLine('"' + buffer.Slice(0, written).ToString() + '"');
            // => "01:01:01.0000001" instead of "01:01:01.1230000"
            
            Console.WriteLine('"' + buffer.ToString() + '"');
            // => "01:01:01.0000001230000" TryFormat writes more than charsWritten characters

            Console.WriteLine('"' + time.ToString("O") + '"');
            // => "01:01:01.1230000" correct

            Console.WriteLine(string.Format("\"{0:O}\"", time)); 
            // => "01:01:01.0000000" no milliseconds at all (they are far to the right)
        }

I think the bug is here

WriteDigits((uint)fraction, destination.Slice(9));

It should be

 WriteDigits((uint)fraction, destination.Slice(9, 7));
Author: devsko
Assignees: -
Labels:

area-System.Runtime, in pr, untriaged

Milestone: -

@tarekgh tarekgh added bug and removed untriaged New issue has not been triaged by the area owner labels Apr 19, 2021
@tarekgh tarekgh added this to the 6.0.0 milestone Apr 19, 2021
@ghost ghost removed the in-pr There is an active PR which will close this issue when it is merged label Apr 19, 2021
@ghost ghost locked as resolved and limited conversation to collaborators May 19, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants