Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Datetime 如何从国家/地区获取时区_Datetime_Go_Timezone_Country Codes - Fatal编程技术网

Datetime 如何从国家/地区获取时区

Datetime 如何从国家/地区获取时区,datetime,go,timezone,country-codes,Datetime,Go,Timezone,Country Codes,我正在将一个函数从PHP转换为Go 我想从Go中的国家代码中获取时区。这与PHP中的此函数类似 public static array DateTimeZone::listIdentifiers ([ int $what = DateTimeZone::ALL [, string $country = NULL ]] ) Go中的哪个功能具有类似功能?时区可从以下位置访问: 如中所示,它在Windows上不起作用,因为默认情况下不包括$GOROOT/lib/time/zoneinfo.zip,

我正在将一个函数从PHP转换为Go

我想从Go中的国家代码中获取时区。这与PHP中的此函数类似

public static array DateTimeZone::listIdentifiers ([ int $what = DateTimeZone::ALL [, string $country = NULL ]] )

Go中的哪个功能具有类似功能?

时区可从以下位置访问:


如中所示,它在Windows上不起作用,因为默认情况下不包括
$GOROOT/lib/time/zoneinfo.zip
,因此需要像.Go标准库这样的项目。寻找一个开源的围棋软件包来为您实现这一点。或者,既然这是Go,自己编写一个简单的Go函数。您必须下载IANA tzdata区域交叉参考文件

比如说,

package main

import (
    "bufio"
    "fmt"
    "os"
    "path/filepath"
    "strings"
    "time"
)

// Download IANA tzdata zone file:
// $ wget https://raw.githubusercontent.com/eggert/tz/master/zone1970.tab -O zone1970.tab

// countryZones returns a map of IANA Time Zone Database (tzdata) zone names
// by ISO 3166 2-character country code: map[country][]zone.
func countryZones(dir string) (map[string][]string, error) {
    fname := filepath.Join(dir, `zone1970.tab`)
    f, err := os.Open(fname)
    if err != nil {
        return nil, err
    }
    defer f.Close()

    countries := make(map[string][]string)
    s := bufio.NewScanner(f)
    for s.Scan() {
        line := s.Text()
        if strings.HasPrefix(line, "#") {
            continue
        }
        n := 3
        fields := strings.SplitN(line, "\t", n+1)
        if len(fields) < n {
            continue
        }
        zone := fields[2]
        for _, country := range strings.Split(fields[0], ",") {
            country = strings.ToUpper(country)
            zones := countries[country]
            zones = append(zones, zone)
            countries[country] = zones
        }
    }
    if err = s.Err(); err != nil {
        return nil, err
    }
    return countries, nil
}

func main() {
    zones, err := countryZones("")
    if err != nil {
        fmt.Fprintln(os.Stderr, err)
        return
    }

    utc := time.Now().UTC()
    for _, country := range []string{"RU", "CA"} {
        fmt.Println("Country:", country)
        zones := zones[country]
        fmt.Println(utc, "UTC")
        for _, zone := range zones {
            loc, err := time.LoadLocation(zone)
            if err != nil {
                fmt.Fprintln(os.Stderr, err)
                continue
            }
            fmt.Println(utc.In(loc), zone)
        }
    }
}

@不,我只想从国家代码中获取时区,而不是从时区中获取当前时间戳。俄罗斯大约有8个时区,您希望通过“ru”代码获取哪个时区?美国有我想3个。@AlexanderTrakhimenok,我想在那个列表中找到第一个时区。第一个意思是太阳升起得更早,或者是首都时区,或者是与同一个国家的其他时区偏差最小的时区?你们最好得到这个国家的首都并使用它的时区。美国至少有5个地理时区。如果你算上那些在一年中不同时间改变DST的奇怪州,或者根本不改变DST的话,情况会更糟。如果你算上DST,几乎世界上每个国家都至少有两个。嗨@VonC,我只想从国家代码而不是时区中获取时区。这似乎比我自己的答案更完整+1.
package main

import (
    "bufio"
    "fmt"
    "os"
    "path/filepath"
    "strings"
    "time"
)

// Download IANA tzdata zone file:
// $ wget https://raw.githubusercontent.com/eggert/tz/master/zone1970.tab -O zone1970.tab

// countryZones returns a map of IANA Time Zone Database (tzdata) zone names
// by ISO 3166 2-character country code: map[country][]zone.
func countryZones(dir string) (map[string][]string, error) {
    fname := filepath.Join(dir, `zone1970.tab`)
    f, err := os.Open(fname)
    if err != nil {
        return nil, err
    }
    defer f.Close()

    countries := make(map[string][]string)
    s := bufio.NewScanner(f)
    for s.Scan() {
        line := s.Text()
        if strings.HasPrefix(line, "#") {
            continue
        }
        n := 3
        fields := strings.SplitN(line, "\t", n+1)
        if len(fields) < n {
            continue
        }
        zone := fields[2]
        for _, country := range strings.Split(fields[0], ",") {
            country = strings.ToUpper(country)
            zones := countries[country]
            zones = append(zones, zone)
            countries[country] = zones
        }
    }
    if err = s.Err(); err != nil {
        return nil, err
    }
    return countries, nil
}

func main() {
    zones, err := countryZones("")
    if err != nil {
        fmt.Fprintln(os.Stderr, err)
        return
    }

    utc := time.Now().UTC()
    for _, country := range []string{"RU", "CA"} {
        fmt.Println("Country:", country)
        zones := zones[country]
        fmt.Println(utc, "UTC")
        for _, zone := range zones {
            loc, err := time.LoadLocation(zone)
            if err != nil {
                fmt.Fprintln(os.Stderr, err)
                continue
            }
            fmt.Println(utc.In(loc), zone)
        }
    }
}
$ wget https://raw.githubusercontent.com/eggert/tz/master/zone1970.tab -O zone1970.tab
--2018-07-15 09:44:02--  https://raw.githubusercontent.com/eggert/tz/master/zone1970.tab
Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 151.101.184.133
Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|151.101.184.133|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 17810 (17K) [text/plain]
Saving to: ‘zone1970.tab’

zone1970.tab             100%[==================================>]  17.39K  --.-KB/s    in 0.03s   

2018-07-15 09:44:02 (582 KB/s) - ‘zone1970.tab’ saved [17810/17810]

$ go run zones.go
Country: RU
2018-07-15 17:40:03.09524872 +0000 UTC UTC
2018-07-15 19:40:03.09524872 +0200 EET Europe/Kaliningrad
2018-07-15 20:40:03.09524872 +0300 MSK Europe/Moscow
2018-07-15 20:40:03.09524872 +0300 MSK Europe/Simferopol
2018-07-15 20:40:03.09524872 +0300 +03 Europe/Volgograd
2018-07-15 20:40:03.09524872 +0300 +03 Europe/Kirov
2018-07-15 21:40:03.09524872 +0400 +04 Europe/Astrakhan
2018-07-15 21:40:03.09524872 +0400 +04 Europe/Saratov
2018-07-15 21:40:03.09524872 +0400 +04 Europe/Ulyanovsk
2018-07-15 21:40:03.09524872 +0400 +04 Europe/Samara
2018-07-15 22:40:03.09524872 +0500 +05 Asia/Yekaterinburg
2018-07-15 23:40:03.09524872 +0600 +06 Asia/Omsk
2018-07-16 00:40:03.09524872 +0700 +07 Asia/Novosibirsk
2018-07-16 00:40:03.09524872 +0700 +07 Asia/Barnaul
2018-07-16 00:40:03.09524872 +0700 +07 Asia/Tomsk
2018-07-16 00:40:03.09524872 +0700 +07 Asia/Novokuznetsk
2018-07-16 00:40:03.09524872 +0700 +07 Asia/Krasnoyarsk
2018-07-16 01:40:03.09524872 +0800 +08 Asia/Irkutsk
2018-07-16 02:40:03.09524872 +0900 +09 Asia/Chita
2018-07-16 02:40:03.09524872 +0900 +09 Asia/Yakutsk
2018-07-16 02:40:03.09524872 +0900 +09 Asia/Khandyga
2018-07-16 03:40:03.09524872 +1000 +10 Asia/Vladivostok
2018-07-16 03:40:03.09524872 +1000 +10 Asia/Ust-Nera
2018-07-16 04:40:03.09524872 +1100 +11 Asia/Magadan
2018-07-16 04:40:03.09524872 +1100 +11 Asia/Sakhalin
2018-07-16 04:40:03.09524872 +1100 +11 Asia/Srednekolymsk
2018-07-16 05:40:03.09524872 +1200 +12 Asia/Kamchatka
2018-07-16 05:40:03.09524872 +1200 +12 Asia/Anadyr
Country: CA
2018-07-15 17:40:03.09524872 +0000 UTC UTC
2018-07-15 15:10:03.09524872 -0230 NDT America/St_Johns
2018-07-15 14:40:03.09524872 -0300 ADT America/Halifax
2018-07-15 14:40:03.09524872 -0300 ADT America/Glace_Bay
2018-07-15 14:40:03.09524872 -0300 ADT America/Moncton
2018-07-15 14:40:03.09524872 -0300 ADT America/Goose_Bay
2018-07-15 13:40:03.09524872 -0400 AST America/Blanc-Sablon
2018-07-15 13:40:03.09524872 -0400 EDT America/Toronto
2018-07-15 13:40:03.09524872 -0400 EDT America/Nipigon
2018-07-15 13:40:03.09524872 -0400 EDT America/Thunder_Bay
2018-07-15 13:40:03.09524872 -0400 EDT America/Iqaluit
2018-07-15 13:40:03.09524872 -0400 EDT America/Pangnirtung
2018-07-15 12:40:03.09524872 -0500 EST America/Atikokan
2018-07-15 12:40:03.09524872 -0500 CDT America/Winnipeg
2018-07-15 12:40:03.09524872 -0500 CDT America/Rainy_River
2018-07-15 12:40:03.09524872 -0500 CDT America/Resolute
2018-07-15 12:40:03.09524872 -0500 CDT America/Rankin_Inlet
2018-07-15 11:40:03.09524872 -0600 CST America/Regina
2018-07-15 11:40:03.09524872 -0600 CST America/Swift_Current
2018-07-15 11:40:03.09524872 -0600 MDT America/Edmonton
2018-07-15 11:40:03.09524872 -0600 MDT America/Cambridge_Bay
2018-07-15 11:40:03.09524872 -0600 MDT America/Yellowknife
2018-07-15 11:40:03.09524872 -0600 MDT America/Inuvik
2018-07-15 10:40:03.09524872 -0700 MST America/Creston
2018-07-15 10:40:03.09524872 -0700 MST America/Dawson_Creek
2018-07-15 10:40:03.09524872 -0700 MST America/Fort_Nelson
2018-07-15 10:40:03.09524872 -0700 PDT America/Vancouver
2018-07-15 10:40:03.09524872 -0700 PDT America/Whitehorse
2018-07-15 10:40:03.09524872 -0700 PDT America/Dawson
$