Skip to content

time: geographical time zone names and Daylight saving time (DST) #3385

@zephyr

Description

@zephyr
The function `time.Parse(alayout, avalue string)` already accepts numerical (`-0700`,
`+07:00`, …) and ‘named’ (`EST`, `CET`, …) time zones, but it should also accept
geographical ones like `Europe/Berlin` or `America/New_York` as specified by the
[https://en.wikipedia.org/wiki/Tz_database tz database]. Right now, it doesn’t:
{{{
parsing time "23.03.2012 22:14 Europe/Berlin" as "02.01.2006 15:04
MST": cannot parse "Europe/Berlin" as "MST"
}}}

This is important because in some parts of the world (like the USA or Europe) rules for
[https://en.wikipedia.org/wiki/Daylight_saving_time Daylight saving time (DST)] are in
force, but most people does’t care to write down timezones because they only
‘think’ in local time. This is why the tz database works with geographical time zone
names – it delivers the programmer from DSR stuff:

`2012-03-23T22:14 Europe/Berlin` should evaluate into `+01:00` (CET/Winter time), but

`2012-06-23T15:00 Europe/Berlin` should evaluate into `+02:00` (CEST/Summer time)


This file `time-dst.go` illustrates the problem:
{{{
package main

import (
    "fmt"
    "time"
)

func main() {
    
    // working:
    mytime("23.03.2012 22:14 CET")
    mytime("23.06.2012 15:00 CEST")
    
    // failing:
    mytime("23.03.2012 22:14 Europe/Berlin")
    mytime("23.06.2012 15:00 Europe/Berlin")
    
}

func mytime(s string) {
    var format = "02.01.2006 15:04 MST" // Reference: Mon Jan 2 15:04:05 MST 2006 (MST is GMT-0700)
    
    var d, err = time.Parse(format, s)
    if err == nil {
        fmt.Println(d.Format(time.RFC3339))
    } else {
        fmt.Println(err)
    }
    fmt.Println()
}
}}}

Finally, i’m using the `6g` compiler (c1702f36df03 (release-branch.r60)
release/release.r60.3) in a Debian based GNU/Linux distribution who must not be named ;).

Attachments:

  1. time-dst.go (502 bytes)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions