-
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
Expand API surface for DateTime, DateTimeOffset, DateOnly, TimeOnly, and TimeSpan #113033
Comments
Tagging subscribers to this area: @dotnet/area-system-datetime |
There is DateOnly.AddDays(Int32) but no DateOnly.AddDays(Double), so I don't think there should be DateOnly.SubtractDays(Double) either. |
Not feasible because years and months do not have a constant number of days and thus not a constant number of ticks either. |
I adjusted the overload. |
Makes sense. Withdrawn. |
Strictly speaking days don't either, but that only happens every ~18 months or so (and support is spotty in programming for leap seconds even then) If you're going to be working with date/time extensively, you may want to look at NodaTime |
I agree with the premise behind this proposal. When I try to do things right (like record a calibration date with a NodaTime, while the “correct” approach to many date/time problems, is complete overkill for those who don’t need to work with dates that often. |
That's sort of the spirit of this request. We shouldn't have to resort to using NodaTime unless we're doing more involved date/time manipulations. The issue at the moment is that the current API surface is inconsistent and lacking with DateOnly and TimeOnly especially feeling like forgotten types. |
Background and Motivation
Almost every project I've worked on, from small personal projects to large enterprise ones, ends up with various extension methods designed to make date and time manipulation more concise and less repetitive. Many of these are born out of inconsistencies and/or shortcomings of the current API surface on the five core date and time types:
DateTime
,DateTimeOffset
,DateOnly
,TimeOnly
, andTimeSpan
. Specifically,DateOnly
andTimeOnly
do not feel like first class citizens next toDateTime
andDateTimeOffset
.In an ecosystem where version after version, compiler and syntax changes have been consitently made to reduce the amount of syntax needed to create types (ie: collection expressions), I feel like these core system types have not been given the same amount of attention. Yet date and time manipulation is probably one of the most common operations and omni present in almost every single project a .NET developer will be involved in.
Proposed API
To avoid creating a large amount of issues that are all centered around the same background and motivation, I opted instead on creating a list of propose APIs with additional notes where appropriate. I have tried to group them as best as I can.
Missing APIs for consistency
1) DateTimeOffset.FromDateTime(DateTime dateTime) static method
DateOnly
andTimeOnly
both already exposeFromDateTime()
.DateTime
can already be cast into aDateTimeOffset
.2) DateTime.FromDateTimeOffset() static method
DateTimeOffset
which supports aDateTime
.DateTimeOffset
would also expose the similarFromDateTime()
.3) dateTime.ToDateTimeOffset(TimeSpan offset) instance method
DateTimeOffset
which supports aDateTime
andTimeSpan
.DateOnly
instances already expose the similarToDateTime
.4) DateTimeOffset.Today and DateOnly.Today property
DateTimeOffset
already exposesNow
andUtcNow
likeDateTime
.DateTime
already exposesToday
.5) TimeOnly.Now and TimeOnly.UtcNow property
DateTime
andDateTimeOffset
already exposeNow
andUtcNow
.6) timeOnly.AddSeconds(double value), timeOnly.AddMilliseconds(double value), timeOnly.AddMicroseconds(double value), and timeOnly.AddTicks(long value) instance method
DateTime
andDateTimeOffset
instances both already exposeAddSeconds()
,AddMilliseconds()
,AddMicroseconds()
, andAddTicks()
.7) DateOnly.FromDateTimeOffset(DateTimeOffset dateTimeOffset) and TimeOnly.FromDateTimeOffset(DateTimeOffset dateTimeOffset) static methods
DateOnly
andTimeOnly
both already exposeFromDateTime()
.8) DateTime.FromDateOnly(DateOnly dateOnly) and DateTimeOffset.FromDateOnly(DateOnly dateOnly) static methods
DateOnly
andTimeOnly
both already exposeFromDateTime()
.9) dateTime.ToDateOnly() instance method
DateOnly
instances already exposeToDateTime()
.10) dateTimeOffset.ToDateOnly() instance method
DateOnly
instances already exposeToDateTime()
.DateTime
instances would also exposeToDateOnly()
.11) dateOnly.ToDateTimeOffset() and dateOnly.ToDateTimeOffset(TimeSpan offset) instance methods
DateOnly
instances already exposeToDateTime()
.DateTime
instances would also exposeToDateOnly()
andDateTimeOffset
instances would also expose.ToDateOnly()
.12) dateTime.ToTimeOnly() and dateTimeOffset.ToTimeOnly() instance methods
DateOnly
instances already exposeToDateTime()
.DateTime
instances would also exposeToDateOnly()
,DateTimeOffset
instances would also expose.ToDateOnly()
, andDateOnly
instances would also expose.ToDateTimeOffset()
.13) Implicit cast from DateOnly instances to DateTimeOffset instances
DateTime
instances already implicitly cast toDateTimeOffset
.Expanding Today API
14) DateTime.UtcToday, DateTimeOffset.UtcToday, DateOnly.UtcToday
DateTime
andDateTimeOffset
already expose the propertiesNow
andUtcNow
, this would be natural pairing to the already exposedToday
property.DateTimeOffset
andDateOnly
would also exposeToday
.New Substract APIs
15) Substract instance method variants of Add to DateTime, DateTimeOffset, DateOnly, and TimeOnly
SubstractYears(int value)
instance method toDateTime
,DateTimeOffset
, andDateOnly
.SubstractMonths(int value)
instance method toDateTime
,DateTimeOffset
, andDateOnly
.SubstractDays(double value)
instance method toDateTime
,DateTimeOffset
, andSubstractDays(int value)
instance methodDateOnly
.SubstractHours(double value)
instance method toDateTime
,DateTimeOffset
, andTimeOnly
.SubstractMinutes(double value)
instance method toDateTime
,DateTimeOffset
, andTimeOnly
.SubstractSeconds(double value)
instance method toDateTime
,DateTimeOffset
, andTimeOnly
.SubstractMilliseconds(double value)
instance method toDateTime
,DateTimeOffset
, andTimeOnly
.SubstractMicroseconds(double value)
instance method toDateTime
,DateTimeOffset
, andTimeOnly
.SubstractTicks(long value)
instance method toDateTime
,DateTimeOffset
, andTimeOnly
.Expand TimeSpan API
#### 16)TimeSpan.FromYears(int value)
andTimeSpan.FromMonths(int value)
static methodsTimeSpan
already exposes all otherFrom
methods.17)
TimeSpan.Add
variants andTimeSpan.Substract
variantsThe text was updated successfully, but these errors were encountered: