-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
System.TimeOnly: Compact round-trip format #68348
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. |
can't you just use static string C = "hh':'mm':'ss";
TimeOnly.FromDateTime(DateTime.Now).ToString(C) Note, |
Tagging subscribers to this area: @dotnet/area-system-runtime Issue DetailsToday for culture-invariant round-tripping Console.WriteLine(new TimeOnly(0, 1, 0).ToString("O")) // Outputs: "00:01:00.0000000" It would be nice if there was a more compact format akin to Something like: Console.WriteLine(new TimeOnly(0, 1, 0).ToString("c")) // Outputs: "00:01:00" The benefit of this format would be to save some bits during transport when fractional portions of the time are not used. Somewhat relates to #53539 & #53768 /cc @tarekgh
|
You can do it with a custom format string, yes. I think the format would need to be Basically this issue is because I think this would be useful/common enough that it should be a standard format. We can just leave this out there to see if anyone else feels the same. #53539 is proposing the same format which will probably need the support to go into #53768. PS: This format would be for round-tripping so it would need to be culture invariant. Just a more compact version of |
You are right if you want to get closer to I am wondering why |
This issue has been marked |
Here are a few examples. Basically the fractional units get compacted or are not written at all, depending on the value: string format = "HH':'mm':'ss.FFFFFFF";
Console.WriteLine(TimeOnly.FromTimeSpan(TimeSpan.FromMinutes(1)).ToString(format)); // 00:01:00
Console.WriteLine(TimeOnly.FromTimeSpan(TimeSpan.FromMilliseconds(1)).ToString(format)); // 00:00:00.001
Console.WriteLine(TimeOnly.FromTimeSpan(TimeSpan.FromTicks(1)).ToString(format)); // 00:00:00.0000001
Console.WriteLine(TimeOnly.FromTimeSpan(TimeSpan.FromMinutes(1)).ToString("O")); // 00:01:00.0000000
Console.WriteLine(TimeOnly.FromTimeSpan(TimeSpan.FromMilliseconds(1)).ToString("O")); // 00:00:00.0010000
Console.WriteLine(TimeOnly.FromTimeSpan(TimeSpan.FromTicks(1)).ToString("O")); // 00:00:00.0000001 |
I am wondering about your scenario that needs the compact form and the full form is not acceptable. Note, most of the time, TimeOnly will not have such milliseconds without smaller fraction as you see in TimeSpan especially when dealing with system time which I guess will be your scenario. So, it is unlikely to run into cases need to compact the output. |
You kind of lost me there. You are saying the precision is NOT likely to be used with |
What I mean is when working with system time, the time precision is higher than milliseconds. Compact formatting of such time wouldn't be noticeable. Here is an example which uses the compact pattern. string format = "HH':'mm':'ss.FFFFFFF";
for (int i = 0; i < 1000000; i++)
{
TimeOnly to = TimeOnly.FromDateTime(DateTime.Now);
Console.WriteLine($"{to.ToString(format)}");
} Out something like:
Compact formatting is not helping much with that. My point is, such compact formatting makes more sense for TimeSpan than TimeOnly. |
👍 |
Sure but what if you are not working with system time though? Or what if you are working with "time since the beginning of the day" instead of "time since now"? I don't quite understand why creating a specific scenario where milliseconds will unlikely be 0 invalidates this request for other scenarios where it will be. I can see a multitude of common use cases where the |
@julealgon the other scenarios you are talking about here which mostly not having a fraction of milliseconds. I am wondering why using |
I don't disagree with that, but I don't think something just being nice to have instead of crucial invalidates the request is my point. |
It doesn't. We still have the issue open :-) but it is a low priority though as the workaround is quite simple. |
Today for culture-invariant round-tripping
TimeOnly
supports theo
/O
format.It would be nice if there was a more compact format akin to
TimeSpan
c
format basicallyhh:mm:ss[.fffffff]
forTimeOnly
.Something like:
The benefit of this format would be to save some bits during transport when fractional portions of the time are not used.
Somewhat relates to #53539 & #53768
/cc @tarekgh
The text was updated successfully, but these errors were encountered: