Repository navigation
Closed
Labels
Milestone
Description
Proposal
Allow retrieving the group of date and time parts in one call. This will help with performance and code writing/reading clarity.
API Proposal
namespace System
{
public readonly partial struct DateTime
{
public void Deconstruct(out int year, out int month, out int day) { }
public void Deconstruct(out int year, out int month, out int day, out int hour, out int minute, out int second) { }
public void Deconstruct(out int year, out int month, out int day, out int hour, out int minute, out int second, out int millisecond, out int microsecond) { }
}
public readonly partial struct DateTimeOffset
{
public void Deconstruct(out int year, out int month, out int day, out TimeSpan offset) { }
public void Deconstruct(out int year, out int month, out int day, out int hour, out int minute, out int second, out TimeSpan offset) { }
public void Deconstruct(out int year, out int month, out int day, out int hour, out int minute, out int second, out int millisecond, out int microsecond, out TimeSpan offset) { }
}
public readonly partial struct DateOnly
{
public void Deconstruct(out int year, out int month, out int day) { }
}
public readonly partial struct TimeOnly
{
public void Deconstruct(out int hour, out int minute, out int second) { }
public void Deconstruct(out int hour, out int minute, out int second, out int millisecond, out int microsecond) { }
}
}Code Sample
var (year, month, day) = DateTime.Now;Old description
From @panost on January 7, 2018 13:42
could be made public and renamed to Deconstruct, so we can write
var (year,month,day) = DateTime.Now;
The same pattern can be applied to DateTimeOffset and TimeSpan
Copied from original issue: dotnet/coreclr#15776
Reacted by Ben Gribaudo, damageboy, Meir Blachman, Lars Fernhomberg, Ryouko Konpaku, Roman, Anirudh Chhangani, Mario Gruda, Jorge Morales, Dan Findley and 9 more
Activity
aobatact commentedon Jan 13, 2018
I would be happy to get a year , month , day at the same time but won't it make confusion between (hour, minute, second) ? Isn't enough to set GetDatePart public?
panost commentedon Jan 13, 2018
@aobatact You can have Deconstuct overloads for example you can add
but if you only care for the TimeOfDay parts then an additional method is needed such as
and use it as
aobatact commentedon Jan 13, 2018
I know that I can have ovreloads to it. My question is how the user know what values are deconstructed. As for other types that can Deconstruct like ValueTuple, Tuple, KeyValuePair, etc... , it is obvious which and what order they are deconsructed (they are deconstructing all the fields), but why can we say that deconstruct with three values means deconstruct to year, month, day ? Why not hour, minute, second ?
aobatact commentedon Jan 13, 2018
If the description of what values are deconstructed are shown in IDE it might be OK but currently I can't find a way for users to know.
panost commentedon Jan 13, 2018
Because you do know that DateTime doesn't have a constructor with 3 arguments that are hour,minute,seconds but only one that is year,month,day. The Deconstructor overloads should follow the signature of a public constructor if that's possible
aobatact commentedon Jan 13, 2018
Well , that's right. I wonder why I didn't come up with a constructor.
damageboy commentedon Jan 18, 2018
I just did this on my own in my project, I'd like to point out that this also helps with perf:
BenchmarkDotNet=v0.10.12, OS=Windows 10 Redstone 3 [1709, Fall Creators Update] (10.0.16299.192)
Intel Core i7-7700HQ CPU 2.80GHz (Kaby Lake), 1 CPU, 8 logical cores and 4 physical cores
Frequency=2742186 Hz, Resolution=364.6726 ns, Timer=TSC
.NET Core SDK=2.1.4
[Host] : .NET Core 2.0.5 (Framework 4.6.26020.03), 64bit RyuJIT
DefaultJob : .NET Core 2.0.5 (Framework 4.6.26020.03), 64bit RyuJIT
I was actually about to open an issue about adding deconstruct in this form when I saw that it's already here...
Also, in my personal view, the
DateTimedeconstructor should be limited to(y, m d), where users wanting to deconstruct the time elements can use.TimeOfDayand deconstruct that in a separate call.There would be very little performance impact for users only needing the
(h, m, s)etc. elements in doing that, as it should be apparent from looking into the code...Naturally
DateTimecan also provide the 6+ element deconstructor, but IMO it just looks unfriendly...jkotas commentedon Jan 18, 2018
Could you please share code for your microbenchmark?
panost commentedon Jan 18, 2018
The results of the benchmark are expected, since the BaseLine calls the GetDatePart
three times (once for each Year,Month and Date). So BaseLine time =~ 3 * Deconstruct time
JonHanna commentedon Jan 18, 2018
If we were limited to only one I'd say the opposite, that we should have the fuller deconstruction since callers can choose to ignore what they don't need and it's the more flexible.
Luckily, we're not limited to only one.
damageboy commentedon Jan 19, 2018
@jkotas: https://github.com/damageboy/datetime-deconstruct
@panost right...
I also have a
#if INSANEfor doing magic number multiplication which for some reason works faster than what the JIT does in a consistent yet irrelevant way...Mark methods specially handled in JitInterface Intrinsic (#26209)
Mark methods specially handled in JitInterface Intrinsic (#26209)
55 remaining items
terrajobst commentedon Nov 29, 2022
Video
DateTime->(DateOnly, TimeOnly)DateTimeOffset->(DateOnly, TimeOnly, TimeSpan)DateTime(DateOnly, TimeOnly)DateTimeOffset(DateOnly, TimeOnly, TimeSpan)deeprobin commentedon Nov 29, 2022
@tarekgh
Since I have already addressed some changes in the classes
DateTime,DateOnly&TimeOnlyso far, I would very much like to face this API.If that goes okay, I would be happy if someone of you assigns me.
tarekgh commentedon Nov 29, 2022
Thanks @deeprobin for willing to help with the implementation.
svick commentedon Nov 30, 2022
When
var (year, month, day) = dateTime;works, is it a problem thatvar (year, month) = dateTime;does the wrong thing? Especially since cutting off the smaller parts works for deconstructingTimeOnly.Though it should be fairly obvious what is wrong because of the types involved. And I don't have a better suggestion.
panost commentedon Dec 1, 2022
@terrajobst I can't imagine a use case for
I mean, where you will need both of them (date and time) other than in the two use cases I already gave for
also the ctors of DateTime from DateOnly, TimeOnly should include another overload with Calendar
tarekgh commentedon Dec 1, 2022
You already gave the example for a scenario need the date and time parts. I can imagine other scenarios like scheduling calendars which will need to extract the date and time. We have avoided having a deconstruct which returns many outputs in this proposal. Also, talking logically, DateTime is really a Date and Time. deconstructing it to these parts makes a lot of sense.
Note, we have added the new constructors only because we added the deconstructors. If we find any scenario in the future that needs this constructor, we can consider adding it.
[API Implementation]: Deconstruct for DateTime, DateTimeOffset, DateO…