mercredi 28 septembre 2016

How to use NodaTime FakeClock to time travel and test different virtual time instances

For the last 30 mins I've been trying to get my FakeClock to start up at a certain time. I'm using it in a Stock Market app and I just use New York timezone. The stock market closes at 8PM and opens at 4AM, so I thought I could avoid ever dealing with daytime changes (at least for now). And since I'm only concentrating on US stocks, I'd also like to avoid any other timezones and confusion.

Anyways, here is how I'm starting up the app and using the Zoned

var zoneInfo = DateTimeZoneProviders.Tzdb["America/New_York"];
var  NewYorkZonedClock = new ZonedClock((IClock)SystemClock.Instance, zoneInfo, CalendarSystem.Iso);

For my tests, I want to set time to July 1, 2016 03:59:59 am. FakeClock constructor argument is of type Instance. I tried to get this instance a bunch a different ways and I keep getting more and more confused and not sure what's the safest and correct way of doing this.

 FakeClock.FromUtc(2016, 07, 01, 03, 59, 59);

First I tried this and couldn't find a natural way without manually moving the time to adjust for the zoneoffset.

 var dateTimeInstance=new LocalDateTime(2016, 07, 01, 03, 59, 59);
 var fakeClock = new     FakeClock(dateTimeInstance.InZone(DateTimeZoneProviders.Tzdb["America/New_York"]))

I thought this would definitely work but the method InZone has a second parameter called ZoneLocalMapping and after poking around in the NodaTime library and googling I can't make sense of what's expected here.

Finally I found a way to make it compile time acceptable by using InZoneLeniently()

            var fakeClock = new FakeClock(new LocalDateTime(2016, 07, 01, 03, 59, 59).InZoneLeniently(zoneInfo).ToInstant());

But, there is also an InZoneStrictly(). Which one should I be using?

Aucun commentaire:

Enregistrer un commentaire