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
Add Today property to DateOnly #53498
Comments
Ok, sounds nice except one problem: What time would we use, 12AM, or 12PM or a different time? Or null? |
|
Why not (probably the |
And why not using this directly? What's the real big major benefit from saving a word plus point in code (and maybe two brackets)? |
One word: convenience. DateOnly is essentially a convenience wrapper, so adding one certainly very common use case doesn't hurt, even if the implementation is stupidly trivial. |
I agree wit @stefanloerwald. Reduction of boilerplate is the key thing here. And having convenience functions also helps people who are still developing their C# coding skills, rather than forcing them to understand the subtle relationship between |
Sure but, is that worth adding another property to the |
Interestingly, the mentioned issue of "does |
Oh, my bad. |
Tagging subscribers to this area: @tannergooding Issue DetailsBackground and MotivationThe .NET 6 Preview 4 Proposed API
|
I prefer |
Cc @tarekgh |
@ufccp is right here. We are trying to keep DateOnly not relate to time zones in general. That is by design. That is why we didn't expose Today, Now, and UtcNow in DateOnly/TimeOnly. I am not seeing using |
A major reason I see in favour of adding these properties is for discoverability and adoption. IMO having a Date type (or Time type) is a huge step forward in modelling and developers should be encouraged to use I would still use it but I could imagine for a lot of folk who don't see the problem with using midnight to represent dates that this impression and any friction with using it would lead them to quickly fall back on just using midnight Also, while it makes sense to decouple date instances from time zones, I would argue it's a different matter to decouple static members on the type (the factories) from time zones. It's unfortunate the C# team seem unwilling to consider static extension methods as they would solve this problem. |
I don't see how And also I don't see the issue with marginally timezone-aware utilities such as a |
@stefanloerwald As I mentioned we are trying to keep DateOnly/TimeOnly not related to time zones in genral especially @adamjones1 I want to clarify we are not exposing DateOnly/TimeOnly to encourage users not using DateTime/DateTimeOffset. Every type is addressing specific scenarios and it is up to the user to choose which type is best fit for the scenario need to achieve. I want to ask here, what exactly the scenario you need DateOnly.Today/UtcToday/Now/UtcNow which using DateTime[Offset] wouldn't be enough for that scenario? Is it something nice to have for convenience? Or you think this is crucial to have to avoid other problems? P.S. I am not trying to push back here on the proposal but want to understand the scenarios and reasoning. |
I don't quite see the point of being so strict about time-zone-free code here. When you think about the concept of "today", you cannot at all think of it without also thinking about time zones. And when you think about dates, naturally there will be a point where you will want to relate it to "today", at least in many use cases. So in my opinion, the goal of keeping the DateOnly code time-zone-free is limiting its usefulness. If you want to motivate people to use that new type, give it useful features. Today is one of them to me, and I don't seem to be alone with that. |
Imagine we exposed Today/UtcToday. Then the next expected request we'll get is user will need to know if the DateOnly is created using local or Utc zones. This will be fair ask to have at that time. no? I am trying to avoid going this route and repeat the concerns we saw with DateTime.
Frankly, I disagree with that. We exposed Date/Time[Only] to address specific scenarios which would be extremely useful to use for. I am not sure why Today is going to limit the usage of such type? |
Which is why "today" is not a concept that fits well on a type that explicitly does not have a concept of time zones, IMO. |
I agree with Tarek, we shouldn't have such properties on Instead, this is where a IClock clock = SystemClock.Local;
DateOnly date = clock.Today; With such objects, we could have clocks that work with local time, UTC, or an arbitrary time zone, and properties that return any of |
It's a convenience and discoverability thing yep, I certainly don't think it's crucial. My concern is the disparity where it's trivial to obtain the current date in the local/UTC time zone in the 'wrong' representation for most use cases (I assert), but non-trivial to obtain it in the 'right' representation. My feeling is the API should nudge people towards using the best fitting model for their data, and typically a nominative instance in time isn't that for a calendar date. I like @mattjohnsonpint's suggestion of getting both the current date and date-time via an |
I am closing this issue as looks agreeing DateOnly is not the right type to expose the Today functionality. For @mattjohnsonpint suggestion, it is tracked already in the issue #36617. Thanks all for your feedback and discussion. |
Background and Motivation
The .NET 6 Preview 4
DateOnly
struct lacks aToday
property as found inDateTime
Proposed API
The text was updated successfully, but these errors were encountered: