-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Update Docs for DateTime.Ticks to explain it depends on the time zone of the object instance. #3355
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
Conversation
Corrected documentation of Ticks property to include information that it is related to the number of ticks since 0:00:00 on January 1, 0001 in the time zone of the the DateTime instance, and not UTC. Thus using ToUniversalTime() and ToLocalTime() can change the number of ticks, even though it represents the same instance of time. As documented here: https://social.msdn.microsoft.com/Forums/vstudio/en-US/fde7e5b0-e2b9-4d3b-8a63-c2ae75e316d8/datetimeticks-bug?forum=netfxbcl And tested on .NET 4.5.1. If this has changed in the latest .NET runtimes, the documentation says that it has not and is the same functionality as 4.5.1
@AWilco Thanks a bunch for correcting this. I'll get someone to double check it. 👍 |
@tarekgh I appreciate that what I have written might not be the factual truth of how DateTime works. What I intended to convey was that the Ticks property is NOT always the number of ticks since 0:00:00 UTC on January 1, 0001. As I understand it, given limited testing myself and reviewing the discussion thread I linked to, if a timezone is set then the Ticks property is the number of ticks since 0:00:00 on January 1, 0001 in that timezone. Practically what I was attempting to solve was that when I read that description, I then assume that if I take a specific instant in time represented in a DateTime Object, e.g. 2019-10-01T00:00:00UTC Then the Ticks property would stay the same, as the elapsed time between either of those dates and 0:00:00 UTC on January 1, 0001. However this is not true. As a test:
My new description may not be 100% accurate, I don't know enough about DateTime to ensure what I write is fully accurate, I would appreciate help in improving my new text. Regarding what happens if you use DateTime.Unspecified, converting to UTC or to local appears to avoid adjusting the ticks at all, and just changes the DateTimeKind property. This means that the instant of time that is represented may be different, but the Ticks property will stay the same. |
@AWilco Thanks for tying to clarify the docs. what about the following? do you think it is good enough or we need to specify more clarification?
|
HI Tarek, Yes, I think what you have is good, although your words about an unspecified time zone seemed to suggest it would be random. In actuality, if the timezone is unspecified, then the whole issue is moot, because there isn't any way to take into account a time zone. The starting instant of 12:00:00 midnight, January 1, 0001 is in the same unspecified time zone. You could aso say that it assumes the time is in UTC for the purposes of this property.
Either way, what you have proposed is an improvement and doesn't introduce any new inaccuracies. |
@AWilco let me know when you make the changes with the new suggestions. Thanks. |
@AWilco thanks for the feedback.
We cannot be specific about this assumption here. There are some contexts that unspecified treated as UTC and in other contexts treated as local. Feel free to suggest something different. My goal is to have anyone reading to understand how to use it. thanks again. |
@AWilco do you have an update on the feedback received on this PR? |
Incorporated feedback from review.
@tarekgh @carlossanlop I've updated the change with the new text as discussed. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I made a couple small tweaks to Carlos' suggestions.
Co-Authored-By: Genevieve Warren <[email protected]>
Co-Authored-By: Genevieve Warren <[email protected]>
Co-Authored-By: Carlos Sanchez Lopez <[email protected]>
Co-Authored-By: Carlos Sanchez Lopez <[email protected]>
Thanks, I believe I have accepted all the proposed changes. |
Co-Authored-By: Genevieve Warren <[email protected]>
@AWilco Awesome! Thanks a bunch for the updates. |
Summary
Corrected documentation of Ticks property to include information that it is related to the number of ticks since 0:00:00 on January 1, 0001 in the time zone of the the DateTime instance, and not always UTC. Thus using ToUniversalTime() and ToLocalTime() can change the number of ticks, even though it represents the same instant in time.
As discussed here: https://social.msdn.microsoft.com/Forums/vstudio/en-US/fde7e5b0-e2b9-4d3b-8a63-c2ae75e316d8/datetimeticks-bug?forum=netfxbcl
And tested on .NET 4.5.1.
If this has changed in the latest .NET runtimes, the documentation says that it has not and is the same functionality as 4.5.1
Not known to be an open issue.