Current date time in specific timezone with and without DST offset with NodaTime

I need to sync a device internal clock on a device according to which timezone the users has selected. I'm using the following code to get the current time for that zone with respect to the system clock:

var currentInstant = SystemClock.Instance.GetCurrentInstant().InZone(userTimezone);

But I also need to be able to toggle a flag that enables/disables DST for the zone, meaning if the zone currently has DST active I need to be able to subtract the offset it adds, and only set the internal clock to the zone time with the standard offset.

I found the standard offset with (not 100% this is correct though):

currentInstant.GetZoneInterval().StandardOffset;

But I'm not sure how to get the zone time using it?

Jon Skeet
people
quotationmark

It sounds like you're nearly there. You presumably want the OffsetDateTime or LocalDateTime, which you'd get like this:

var now = SystemClock.Instance.GetCurrentInstant();
var zoneInterval = zone.GetZoneInterval(now);
var offset = zoneInterval.StandardOffset; // Ignore any savings
OffsetDateTime offsetNow = now.WithOffset(offset);

You can get the LocalDateTime with offsetNow.LocalDateTime if you need that.

At that point, you don't have a ZonedDateTime, because you don't really have a time zone as such - your "zone without DST" isn't a regular time zone. But it sounds like it's what you need.

As Matt said, this is a fundamentally bad idea - I know you've tried to convince your client of that already, but it's probably worth thinking ahead and working out what you'll do when your client understands that it's a bad idea too...

people

See more on this question at Stackoverflow