Determine if a time is daylight savings

3,020 views
Skip to first unread message

Dustin Norlander

unread,
Feb 12, 2013, 6:29:41 PM2/12/13
to golan...@googlegroups.com
I want to find out whether a time is in dst for Location "America/New_York".

Looking at the code, this is what I want, but this func is not visible:

_, _, isDST, _, _ := nycLocation.lookup(mytime.Unix())


is there any way to do this?

Thanks much,
Dustin

Ian Lance Taylor

unread,
Feb 12, 2013, 8:05:28 PM2/12/13
to Dustin Norlander, golan...@googlegroups.com
Doesn't look like it. Please file an issue for this. Thanks.

Ian

Russ Cox

unread,
Feb 12, 2013, 10:44:38 PM2/12/13
to Ian Lance Taylor, Dustin Norlander, golang-nuts
If you specifically care about America/New_York, you can check whether Zone() returns "EST" or "EDT".

Russ

Tor Langballe

unread,
Feb 13, 2013, 9:01:25 AM2/13/13
to golan...@googlegroups.com
What if you created a location using:

location := FixedZone("FixedNew_York", -5)

and set it to the same time (using date/hour, minute, seconds) and also with the normal America/New_York location.

If there is a time-difference between the two times, it's daylight savings.

tor 

Tor Langballe

unread,
Feb 13, 2013, 9:17:40 AM2/13/13
to golan...@googlegroups.com
Actually, my original post only works for a known location.

How about this (semi real code):

_, timeOffset := time.Zone() //with time already in correct myLocation
_, winterOffset := time.Time(1, jan, 2013, 0, 0, 0).In(myLocation).Zone()
_, summerOffset := time.Time(1, july, 2013, 0, 0, 0).In(myLocation).Zone()

if winterOffset != summerOffset { // the location has daylight saving
  if timeOffset != winterOffset {
    fmt.Println("Daylight Saving")
  }
}

Can be made better by using year of time you are checking.




On Wednesday, February 13, 2013 12:29:41 AM UTC+1, Dustin Norlander wrote:

Kyle Lemons

unread,
Feb 13, 2013, 11:25:37 AM2/13/13
to Tor Langballe, golang-nuts
This assumes that January is not in daylight saving time and that july is.  That might seem like a safe assumption now, but so does assuming that a week has 7 days and that a day has 24 hours.  (Time is hard)


--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Kyle Lemons

unread,
Feb 13, 2013, 11:26:17 AM2/13/13
to Tor Langballe, golang-nuts
On Wed, Feb 13, 2013 at 8:25 AM, Kyle Lemons <kev...@google.com> wrote:
This assumes that January is not in daylight saving time and that july is.
er, the reverse, but you get the idea.

Dustin Norlander

unread,
Feb 13, 2013, 12:57:42 PM2/13/13
to golan...@googlegroups.com
Using Russ's suggestion seems to work:

var USEast, err = time.LoadLocation("America/New_York")
func IsEasternDayLight(time time.Time) bool {
    t := time.In(USEast)
    zone, _ := t.Zone()
    if zone == "EDT" {
        return true
    }
    return false;
}

Thanks!

Tor Langballe

unread,
Feb 13, 2013, 1:08:36 PM2/13/13
to golan...@googlegroups.com
Kyle wrote: This assumes that January is not in daylight saving time and that july is.

-Good point, I guess that's the case in the southern hemisphere. 

Dan Kortschak

unread,
Feb 13, 2013, 2:40:20 PM2/13/13
to Kyle Lemons, Tor Langballe, golang-nuts
That's exactly how it is here. Time is hard.

Peter Kleiweg

unread,
Feb 13, 2013, 2:54:32 PM2/13/13
to golan...@googlegroups.com
Op woensdag 13 februari 2013 15:17:40 UTC+1 schreef Tor Langballe het volgende:
Actually, my original post only works for a known location.

How about this (semi real code):

_, timeOffset := time.Zone() //with time already in correct myLocation
_, winterOffset := time.Time(1, jan, 2013, 0, 0, 0).In(myLocation).Zone()
_, summerOffset := time.Time(1, july, 2013, 0, 0, 0).In(myLocation).Zone()

if winterOffset != summerOffset { // the location has daylight saving
  if timeOffset != winterOffset {
    fmt.Println("Daylight Saving")
  }
}



(Well, it works at home, not on the playground)

Tor Langballe

unread,
Feb 14, 2013, 8:37:57 AM2/14/13
to golan...@googlegroups.com
Nice, good check for winterOffset > summerOffset.

Reply all
Reply to author
Forward
0 new messages