Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to simulate daylight saving time transition in a unit test?

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.

like image 894
WaltiD Avatar asked Jul 17 '11 19:07

WaltiD


People also ask

How do you transition to Daylight Savings?

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.

How does UTC handle Daylight Savings?

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.

How do you check if DST daylight saving time is in effect and if so the offset?

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.

How do you explain daylight savings time?

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.


1 Answers

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.

like image 66
Archie Avatar answered Oct 01 '22 12:10

Archie