rspec/rules/S6563/why-dotnet.adoc

12 lines
1.2 KiB
Plaintext

== Why is this an issue?
You should avoid recording time instants with the use of property `DateTime.Now`.
The property `DateTime.Now` returns the current date and time expressed in the machine's local time without containing any timezone-related information (for example, the offset from Coordinated Universal Time).
Not having this information means that if you need to display this `DateTime` object or use it for computations in another machine placed in a different time zone, you won't be able to reconstruct it in the second machine's local time without knowing the origin's offset. This will likely lead to confusion and potential bugs.
Instead, you should record the `DateTime` instants in UTC, which gives you the date and time as it is in the Coordinated Universal Time. UTC is a time standard for all time zones and is not subjected to Daylight Saving Time (DST).
Similarly, the use of the `DateTime.Today` property should also be avoided, as it can return different date values depending on the time zone.
Generally, unless the purpose is to only display the Date and Time to a user on their local machine, you should always use UTC (for example, when storing dates in a datebase or using them for calculations).