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.
MSDN says:
IMO you should add your minutes in UTC and then convert to local kind.
Here’s the (updated) code:
@WaltiD: I can’t comment but the code above prints 02:58 before and after DST shift.