I have a code, that compare the last timestamp with the actual timestamp. If the actual timestamp is before the last timestamp, the systemtime has been manipulated. Because of the transition from or to daylight saving time, I work with UTC.
Now I would like to write an unit test for this special situation.
public void TransitionFromDSTToNonDST()
{
var dayLightChangeEnd= TimeZone.CurrentTimeZone.GetDaylightChanges(DateTime.Now.Year).End;
var stillInDaylightSavingTime= dayLightChangeEnd.Subtract(TimeSpan.FromMinutes(62));
//stillInDaylightSavingTime.IsDaylightSavingTime() returns true
//stillInDaylightSavingTime is 01.58 am
var noDaylightSavingTimeAnymore= dayLightChangeEnd.Subtract(TimeSpan.FromMinutes(32));
//noDaylightSavingTimeAnymore.IsDaylightSavingTime() returns false
//noDaylightSavingTimeAnymore is 02.28 am
}
But actually I would like to simulate the following:
var stillInDaylightSavingTime = //for example 02.18 am BEFORE switching to Non-DST
var noDaylightSavingTimeAnymore = //for example 02.10 am AFTER switching to Non-DST
So the question is how can I define that a 02.18 am is either DST or not.
You can get ready to “spring forward” in March by gradually shifting your schedule in the week leading up to the time change. The American Academy of Sleep Medicine advises trying to slowly adjust your schedule5 by going to bed around 15-20 minutes earlier each day.
The switch to daylight saving time does not affect UTC. It refers to time on the zero or Greenwich meridian, which is not adjusted to reflect changes either to or from Daylight Saving Time.
To check if DST (Daylight Saving time) is in effect:Use the getTimezoneOffset() method to get the timezone offset for the 2 dates. Check if the greater of the two values is not equal to the offset for the original date.
Daylight Saving Time (DST) is the practice of turning the clock ahead as warmer weather approaches and back as it becomes colder again. The goal of Daylight Saving Time is to make better use of daylight by prolonging the amount of time we can spend outside during daylight hours.
MSDN says:
Conversion operations between time zones (such as between UTC and local time, or between one time zone and another) take daylight saving time into account, but arithmetic and comparison operations do not.
IMO you should add your minutes in UTC and then convert to local kind.
Here's the (updated) code:
var dayLightChangeEnd = TimeZone.CurrentTimeZone.GetDaylightChanges(DateTime.Now.Year).End.ToUniversalTime();
var stillInDaylightSavingTime = dayLightChangeEnd.Subtract(TimeSpan.FromMinutes(62)).ToLocalTime();
Console.WriteLine(stillInDaylightSavingTime);
Console.WriteLine(stillInDaylightSavingTime.IsDaylightSavingTime());
var noDaylightSavingTimeAnymore = dayLightChangeEnd.Subtract(TimeSpan.FromMinutes(2)).ToLocalTime();
Console.WriteLine(noDaylightSavingTimeAnymore);
Console.WriteLine(noDaylightSavingTimeAnymore.IsDaylightSavingTime());
@WaltiD: I can't comment but the code above prints 02:58 before and after DST shift.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With