You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I would like to propose that DateTime.Now be deprecated. This is most developer's first introduction to working with time, and is also a root cause of bugs related to daylight saving time and time zones.
Developers should get in the habit of thinking globally, rather than locally - especially when designing for the web. Even on desktop and mobile applications that only run in a single time zone, this can create the kind of bugs that pull developers out of bed at 2AM on the morning of a daylight saving time transition.
Given the widespread nature of this API, I suggest not removing it from coreclr - but rather marking it with an [Obsolete] attribute. I can send a PR if the suggestion is approved.
Reacted by Tugberk Ugurlu, Dave Van den Eynde, Giorgio Galante, Lee Campbell, Trevor Poppen and ddprince17
I'm sorry but I sincerely believe this is a HORRIBLE idea. I read your "Case Against" argument - you're making all sorts of assumptions that are just not valid (or perhaps more accurately push your somewhat myopic view on time.)
While I agree there are many instances where capturing the timezone would be appropriate, it certainly isn't necessary in all cases.
Your argument that local time is without context is just false. There are many systems that operate purely within a single timezone, and the 'local' context is adequate.
I work in an industry that really pays no attention whatsoever to timezones, and we interact with data shipped from around the country. We deal with dozens of standards that do not even have room for timezone data, so having to manage it would be a complete waste of time.
Not to mention the fact that I'll now have to deal with hundreds of obsolete warnings.
Two people's ideas of what datetimes should be, automatically mean this is a bug/issue? I would strongly disagree with this change. The reality is that while I think Utc time is probably more valid/relevant date time value/format, I think the reasoning for this change is trivial. As jake points out.. .there are many .net systems out there with no concept/context of timezones or needing to run on servers.
I'd much rather have things stay the way they are... my .02c
I'm not arguing for everyone using UTC in all cases. I'm well aware of the use cases of using local time, and that many people work within a single time zone.
Let me restate the problem another way. Imagine an application that's developed offshore, as many applications are. Perhaps it was developed in a location that does not use daylight saving time, so the developers may not even be familiar with the concept. (As an example, consider UTC+05:30 that is used in India.) The application records some transactions in the local time zone on a fairly regular basis. It gets the current time using DateTime.Now, and stores it in a datetime field in a SQL Server.
Everything works well, and the application is delivered to a client in the US, or Europe, or elsewhere in the world. As an example, let's say it's installed on a server that is set to US Eastern Standard Time.
It continues to work well, until the date of the fall-back daylight saving time transition. When the clock was approaching 2:00 AM, it jumped backward to 1:00 AM and repeated the hour. How does the application behave? Well, it depends on what is done with that data. Say, for example, there is part of the application that returns records sorted by time. For this hour, it will return twice the usual amount of data, and the values from the first one-o'clock hour will be interleaved with the second one-o'clock hour. What happens from there is highly dependent on the application. Perhaps just totals are off on some report. But perhaps it triggers a false alarm on some critical tripwire.
This is just one example where ignoring the behavior of the local time zone can have unintended consequences. There doesn't need to be any concept of time zone in the application, nor does it need to have users in multiple time zones in order to encounter these types of problems. The fact is, local time is an artifact of the local time zone. In fact, we can see in the implementation of DateTime.Now that TimeZoneInfo is used with the local time zone.
In the example given, and in many of the other examples, the problem is mitigated by using DateTimeOffset.Now to get the local time including the offset - and persisting the entire DateTimeOffset object.
The only cases where you would not have these types of problems is if one of the following is true:
The application will never run in a time zone with daylight saving time
The application never runs in the middle of the night (near the transition times)
The application has low activity during the transition time
The data isn't recorded or isn't used to filter, sort, or group
In the case where you really want the current local DateTime, and one of the above is true, then you could use DateTimeOffset.Now.DateTime to be explicit that you don't care about the offset.
And yes - I agree that this is opinionated. However, I believe it's an opinion that is well informed and would help guide developers to making the right decisions. I bring it here as a proposal for discussion for the betterment of the framework - not as a bug.
Reacted by Tugberk Ugurlu, Nicholas Lydon, Trevor Poppen, Naing Ye Lin Thein and Abhinav Galodha
So you're suggesting the framework be optimized for offshore development? You're not, obviously, but that sure is an absurd example.
You're also dismissing 10+ years of code written around DateTime.Now. You're going to have developers maintaining code bases and arbitrarily changing Now to UtcNow to fix build warnings. Now we have a whole new raft of bugs to fix, thank you very much.
The problem is less the idea of using local time and more the fact that a developer can easily use DateTime.Now and not realize the issues. I think though that is should be more of a documentation issue than an API one. Just like string concatenation vs. StringBuilder you allow the developer to chose which is the best for their situation.
So you suppose i should always have to write DateTime.UtcNow.ToLocalTime() or DateTimeOffset.Now.DateTime everytime i need to present my user on a local operating desktop system the current time? Developers developing enterprise server applications accessed around the world should be aware of the concept of Utc.
Marking DateTime.Now as [Obsolete] will trigger a build warning with the implications of people having warnings as errors to fail the build which would be undesired and also not accurate since the concept of a local time will never fade away, the world will always have local times and requiring everyone to add explicit code to access local time would be contradictory to the basic principle of .NET to be local aware first (e.g.: string comparisons).
This proposal is anything except reasonable. Yes, ideally UTC should be used but obsoleting DateTime.Now won't do that, the only thing this would achieve is making old code produce a bunch of warnings that needs to be fixed, at the cost of introducing bugs, or silenced. Or ignored, I've seen plenty of warnings ignored just like I've seen plenty of bad uses of local time.
This should be handled like other globalization issues, with code analysis tools. For example FxCop has CA1303 for string literals, something like that could probably be added for DateTime.Now.
Personally, I'd prefer a new date/time API in general (whether we ask John Skeet for NodaTime, or go with something closer to Java's new JSR310, or some third option, I don't much care - although I think NodaTime would likely need to have extensible calendars).
This is more about providing things scoped for least-use, and clearing up ambiguity.
Have a "business date", that is used globally (ie, "all sales for the 25th") - that's a LocalDate, with only year/month/day values (and note that they usually aren't strictly aligned to other calendars, either).
Have a log? Everything should be an instant in UTC, and then formatted when necessary.
...At least this isn't Java (%shudder%...that original date/time API)...
You end up with different types, depending on what you're doing, usually. So stuff like LocalDate.Today (no time portion, so "now" isn't quite right), or Instant.Now (which incorporates no notion of timezones, or even year/month/day divisions - it's an offset from a specific earlier instant).
This doesn't prevent somebody from being an idiot while misusing a type, but it does allow conscientious people to be more specific and clear about what they plan to allow.
it would be nice if there was a rating thumbs up or down to get the general sense of the community. oblivating aol me too message like this.
I dont think this is a good idea.
But as an aside @aggsol DateTime.LocalNow and DateTime.UtcNow are great additions but as extension methods.. there is opensource .net library for date time that is fairly popular. that would the thing to reccomend
@mj1856 Can you justify your assertion that DateTime.Now is almost always in error with empirical evidence? Because honestly I have rarely come across the issues you raise, and when I have the fixes have been trivial. You're making this out to be a huge issue, and I'm just not seeing it. @SamuelEnglard pretty much hit the nail on the head - Let the developer make the decision. Contrary to popular belief we know what we're doing.
I have witnessed countless bugs over the years caused by developers using DateTime.Now where they should have used DateTime.UtcNow. I would therefore love to see DateTime.Now marked as obsolete, and DateTime.LocalNow introduced in it's place. A simple, non-breaking change that increases clarity and reduces the chance of bugs.
Marking DateTime.Now as [Obsolete] will trigger a build warning with the implications of people having warnings as errors to fail the build which would be undesired
Also it's pretty much default that .NET APIs are by default locale aware unless specified otherwise.
The overall settlement of this issue was to opt-in for this in a less intrusive way by using roslyn analyzers which could optionally auto-fix all occurrences.
@Suchiman I'm aware about the warnings as errors issue discussed above, but I maintain that in this scenario the benefits outweigh the costs - since the fix for such a warning would be trivial (replace usages of DateTime.Now with DateTime.LocalNow. In fact - addressing such warnings may actually result in undiscovered bugs being fixed by forcing developers to consider whether they actually wanted to use local time in the first place.
I also don't think this change has any bearing on whether the DateTime API is locale aware or not. It's simply a case of being clearer about what the properties represent.
@mj1856 I'm not proposing that the name of the property should be changed, I'm proposing that DateTime.Now is marked as obsolete, and a new property is introduced called DateTime.LocalNow with the exact same value. For example:
You are correct that it may not solve the problem, but it certainly increases the clarity of what the value actually represents, and would force the developer to consider whether local time is actually what they mean.
I'm based in the UK, where we recently entered BST (British Summer Time). In the past 2 days I have witnessed 2 separate projects encounter issues directly caused by using DateTime.Now where DateTime.UtcNow should have been used. These are generally easy to fix and you could argue that they could have been avoided in the first place with better coding or test coverage, but the reality is that such issues will continue to crop up - and I've seen plenty of them in my time as a .NET developer. I fail to see how this change could do anything but improve matters, even considering the warnings as errors problem.
@liddellj - Yep. I hear you loud and clear. However, I raised the issue and the community responded quite clearly that this change would have too far reaching of an impact without a whole lot of direct benefit. Say we did as you proposed. All it would do is create a lot of warnings, and many people would either ignore them or just change the name without considering the impact.
I'm still advocating for the best practice to be: Never use DateTime.Now in a web application. (Or any of the other methods that assume the local time zone). I am investigating where to fit this into code analysis and other guidance. But it's not going to be obsoleted in the framework.
The language + framework should be designed to support the entire set of programs which will be built upon them; they should not be optimized for a specific (but important) subset.
Your goal is to avoid an extremely common error that is made in globalized "information" systems. This is a very common use case on the .Net platform, so it's clear that there should be some way improve the handling for this situation.
Luckily the new .Net compiler offers just that: you can create a custom Analyzer to identify these issues in your project at compile time. You can catch the bug without having to change anything in the .Net framework. Why wait 1-2 years for a framework-level fix when you have the power to do that now?
This is something that we, the community, should pick up and deliver.
@ryanbnl The framework is the root and needs to be fixed regardless how long it takes of changes to emerge. I find arguing that way risky because you are arguing for "custom Analyzer" bandaid that can only be used by the knowing,
Strongly disagree... besides "needing to be fixed" and what constitutes the fix are totally subjective... To me this still feels an awful lot like someone latched on to one teeny tiny little concept, and is suggesting big breaking changes to get that one little thing working how THEY want it.
Frankly, I like the idea of analyzer over just changing objects, and deprecating (or just removing them all together).
Its worked, and worked better than Dates on any other platform I've dealt with dates, and we've got 10yrs 14'ish years of information on the net that talks about using datetime way x, or way y.
In any case the issue is closed, lets hope this stays that way.
Activity
jakesays-old commentedon Feb 4, 2015
I'm sorry but I sincerely believe this is a HORRIBLE idea. I read your "Case Against" argument - you're making all sorts of assumptions that are just not valid (or perhaps more accurately push your somewhat myopic view on time.)
While I agree there are many instances where capturing the timezone would be appropriate, it certainly isn't necessary in all cases.
Your argument that local time is without context is just false. There are many systems that operate purely within a single timezone, and the 'local' context is adequate.
I work in an industry that really pays no attention whatsoever to timezones, and we interact with data shipped from around the country. We deal with dozens of standards that do not even have room for timezone data, so having to manage it would be a complete waste of time.
Not to mention the fact that I'll now have to deal with hundreds of obsolete warnings.
ronnyek commentedon Feb 4, 2015
Two people's ideas of what datetimes should be, automatically mean this is a bug/issue? I would strongly disagree with this change. The reality is that while I think Utc time is probably more valid/relevant date time value/format, I think the reasoning for this change is trivial. As jake points out.. .there are many .net systems out there with no concept/context of timezones or needing to run on servers.
I'd much rather have things stay the way they are... my .02c
mattjohnsonpint commentedon Feb 4, 2015
I'm not arguing for everyone using UTC in all cases. I'm well aware of the use cases of using local time, and that many people work within a single time zone.
Let me restate the problem another way. Imagine an application that's developed offshore, as many applications are. Perhaps it was developed in a location that does not use daylight saving time, so the developers may not even be familiar with the concept. (As an example, consider UTC+05:30 that is used in India.) The application records some transactions in the local time zone on a fairly regular basis. It gets the current time using
DateTime.Now, and stores it in adatetimefield in a SQL Server.Everything works well, and the application is delivered to a client in the US, or Europe, or elsewhere in the world. As an example, let's say it's installed on a server that is set to US Eastern Standard Time.
It continues to work well, until the date of the fall-back daylight saving time transition. When the clock was approaching 2:00 AM, it jumped backward to 1:00 AM and repeated the hour. How does the application behave? Well, it depends on what is done with that data. Say, for example, there is part of the application that returns records sorted by time. For this hour, it will return twice the usual amount of data, and the values from the first one-o'clock hour will be interleaved with the second one-o'clock hour. What happens from there is highly dependent on the application. Perhaps just totals are off on some report. But perhaps it triggers a false alarm on some critical tripwire.
This is just one example where ignoring the behavior of the local time zone can have unintended consequences. There doesn't need to be any concept of time zone in the application, nor does it need to have users in multiple time zones in order to encounter these types of problems. The fact is, local time is an artifact of the local time zone. In fact, we can see in the implementation of
DateTime.NowthatTimeZoneInfois used with the local time zone.In the example given, and in many of the other examples, the problem is mitigated by using
DateTimeOffset.Nowto get the local time including the offset - and persisting the entireDateTimeOffsetobject.The only cases where you would not have these types of problems is if one of the following is true:
In the case where you really want the current local
DateTime, and one of the above is true, then you could useDateTimeOffset.Now.DateTimeto be explicit that you don't care about the offset.And yes - I agree that this is opinionated. However, I believe it's an opinion that is well informed and would help guide developers to making the right decisions. I bring it here as a proposal for discussion for the betterment of the framework - not as a bug.
jakesays-old commentedon Feb 4, 2015
So you're suggesting the framework be optimized for offshore development? You're not, obviously, but that sure is an absurd example.
You're also dismissing 10+ years of code written around DateTime.Now. You're going to have developers maintaining code bases and arbitrarily changing Now to UtcNow to fix build warnings. Now we have a whole new raft of bugs to fix, thank you very much.
shmuelie commentedon Feb 4, 2015
The problem is less the idea of using local time and more the fact that a developer can easily use
DateTime.Nowand not realize the issues. I think though that is should be more of a documentation issue than an API one. Just like string concatenation vs.StringBuilderyou allow the developer to chose which is the best for their situation.I vote for clearer documentation.
Suchiman commentedon Feb 4, 2015
So you suppose i should always have to write
DateTime.UtcNow.ToLocalTime()orDateTimeOffset.Now.DateTimeeverytime i need to present my user on a local operating desktop system the current time? Developers developing enterprise server applications accessed around the world should be aware of the concept of Utc.Marking
DateTime.Nowas[Obsolete]will trigger a build warning with the implications of people having warnings as errors to fail the build which would be undesired and also not accurate since the concept of a local time will never fade away, the world will always have local times and requiring everyone to add explicit code to access local time would be contradictory to the basic principle of .NET to be local aware first (e.g.: string comparisons).mikedn commentedon Feb 4, 2015
This proposal is anything except reasonable. Yes, ideally UTC should be used but obsoleting DateTime.Now won't do that, the only thing this would achieve is making old code produce a bunch of warnings that needs to be fixed, at the cost of introducing bugs, or silenced. Or ignored, I've seen plenty of warnings ignored just like I've seen plenty of bad uses of local time.
This should be handled like other globalization issues, with code analysis tools. For example FxCop has CA1303 for string literals, something like that could probably be added for DateTime.Now.
Clockwork-Muse commentedon Feb 4, 2015
Personally, I'd prefer a new date/time API in general (whether we ask John Skeet for NodaTime, or go with something closer to Java's new JSR310, or some third option, I don't much care - although I think NodaTime would likely need to have extensible calendars).
This is more about providing things scoped for least-use, and clearing up ambiguity.
Have a "business date", that is used globally (ie, "all sales for the 25th") - that's a
LocalDate, with only year/month/day values (and note that they usually aren't strictly aligned to other calendars, either).Have a log? Everything should be an instant in UTC, and then formatted when necessary.
...At least this isn't Java (%shudder%...that original date/time API)...
aggsol commentedon Feb 4, 2015
What would be the alternative then? Calls like
DateTime.LocalNowandDateTime.UtcNow?Clockwork-Muse commentedon Feb 4, 2015
You end up with different types, depending on what you're doing, usually. So stuff like
LocalDate.Today(no time portion, so "now" isn't quite right), orInstant.Now(which incorporates no notion of timezones, or even year/month/day divisions - it's an offset from a specific earlier instant).This doesn't prevent somebody from being an idiot while misusing a type, but it does allow conscientious people to be more specific and clear about what they plan to allow.
nydehi commentedon Feb 4, 2015
it would be nice if there was a rating thumbs up or down to get the general sense of the community. oblivating aol me too message like this.
mattjohnsonpint commentedon Feb 4, 2015
@Suchiman - No, I'm proposing you would write
DateTimeOffset.Nowfor that. Converting back to aDateTimedefeats the purpose.mattjohnsonpint commentedon Feb 4, 2015
@Clockwork-Muse NodaTime is great. But I don't think introducing new types into the clr is going to stop people from misusing the existing ones.
mattjohnsonpint commentedon Feb 4, 2015
@mikedn - yes - it would and should produce a warning. Using
DateTime.Nowis very easy - and is almost always in error. (Especially in an ASP.net app)I like your idea though that perhaps this should be a Code Analysis warning instead of a compiler warning. That feels like a better balance.
jakesays-old commentedon Feb 4, 2015
@mj1856 Can you justify your assertion that DateTime.Now is almost always in error with empirical evidence? Because honestly I have rarely come across the issues you raise, and when I have the fixes have been trivial. You're making this out to be a huge issue, and I'm just not seeing it. @SamuelEnglard pretty much hit the nail on the head - Let the developer make the decision. Contrary to popular belief we know what we're doing.
8 remaining items
liddellj commentedon Mar 31, 2015
I have witnessed countless bugs over the years caused by developers using
DateTime.Nowwhere they should have usedDateTime.UtcNow. I would therefore love to seeDateTime.Nowmarked as obsolete, andDateTime.LocalNowintroduced in it's place. A simple, non-breaking change that increases clarity and reduces the chance of bugs.Any chance of re-opening this bug on that basis?
Suchiman commentedon Mar 31, 2015
@liddellj As already mentioned:
Also it's pretty much default that .NET APIs are by default locale aware unless specified otherwise.
The overall settlement of this issue was to opt-in for this in a less intrusive way by using roslyn analyzers which could optionally auto-fix all occurrences.
mattjohnsonpint commentedon Mar 31, 2015
Also, changing the name of the property would be a breaking change, and wouldn't necessarily solve the problem.
liddellj commentedon Mar 31, 2015
@Suchiman I'm aware about the warnings as errors issue discussed above, but I maintain that in this scenario the benefits outweigh the costs - since the fix for such a warning would be trivial (replace usages of
DateTime.NowwithDateTime.LocalNow. In fact - addressing such warnings may actually result in undiscovered bugs being fixed by forcing developers to consider whether they actually wanted to use local time in the first place.I also don't think this change has any bearing on whether the DateTime API is locale aware or not. It's simply a case of being clearer about what the properties represent.
@mj1856 I'm not proposing that the name of the property should be changed, I'm proposing that
DateTime.Nowis marked as obsolete, and a new property is introduced calledDateTime.LocalNowwith the exact same value. For example:You are correct that it may not solve the problem, but it certainly increases the clarity of what the value actually represents, and would force the developer to consider whether local time is actually what they mean.
I'm based in the UK, where we recently entered BST (British Summer Time). In the past 2 days I have witnessed 2 separate projects encounter issues directly caused by using
DateTime.NowwhereDateTime.UtcNowshould have been used. These are generally easy to fix and you could argue that they could have been avoided in the first place with better coding or test coverage, but the reality is that such issues will continue to crop up - and I've seen plenty of them in my time as a .NET developer. I fail to see how this change could do anything but improve matters, even considering the warnings as errors problem.mattjohnsonpint commentedon Apr 1, 2015
@liddellj - Yep. I hear you loud and clear. However, I raised the issue and the community responded quite clearly that this change would have too far reaching of an impact without a whole lot of direct benefit. Say we did as you proposed. All it would do is create a lot of warnings, and many people would either ignore them or just change the name without considering the impact.
I'm still advocating for the best practice to be: Never use
DateTime.Nowin a web application. (Or any of the other methods that assume the local time zone). I am investigating where to fit this into code analysis and other guidance. But it's not going to be obsoleted in the framework.ryanbnl commentedon Apr 1, 2015
The language + framework should be designed to support the entire set of programs which will be built upon them; they should not be optimized for a specific (but important) subset.
Your goal is to avoid an extremely common error that is made in globalized "information" systems. This is a very common use case on the .Net platform, so it's clear that there should be some way improve the handling for this situation.
Luckily the new .Net compiler offers just that: you can create a custom Analyzer to identify these issues in your project at compile time. You can catch the bug without having to change anything in the .Net framework. Why wait 1-2 years for a framework-level fix when you have the power to do that now?
This is something that we, the community, should pick up and deliver.
aggsol commentedon Apr 1, 2015
@ryanbnl The framework is the root and needs to be fixed regardless how long it takes of changes to emerge. I find arguing that way risky because you are arguing for "custom Analyzer" bandaid that can only be used by the knowing,
ronnyek commentedon Apr 1, 2015
Strongly disagree... besides "needing to be fixed" and what constitutes the fix are totally subjective... To me this still feels an awful lot like someone latched on to one teeny tiny little concept, and is suggesting big breaking changes to get that one little thing working how THEY want it.
Frankly, I like the idea of analyzer over just changing objects, and deprecating (or just removing them all together).
Its worked, and worked better than Dates on any other platform I've dealt with dates, and we've got 10yrs 14'ish years of information on the net that talks about using datetime way x, or way y.
In any case the issue is closed, lets hope this stays that way.
DaveVdE commentedon Jun 29, 2016
The whole
DateTimetype needs to be made obsolete IMHO.Merge pull request #626 from leecow/master