Подтвердить что ты не робот

Перевести UTC в "местное" время в Го

Как я могу преобразовать время UTC в местное время?

Я создал карту с разницей в UTC для всех стран, где мне нужно местное время. Затем я добавляю эту разницу как длительность к текущему времени (UTC) и распечатываю результат в надежде на местное время этой конкретной страны.

По некоторым причинам результат неправильный. Например, с Венгрией разница в один час. Есть идеи, почему я получаю неправильные результаты?

package main

import "fmt"
import "time"

func main() {

    m := make(map[string]string)
    m["Hungary"] = "+01.00h"

    offSet, err := time.ParseDuration(m["Hungary"])
    if err != nil {
        panic(err)
    }
    t := time.Now().UTC().Add(offSet)
    nice := t.Format("15:04")

    fmt.Println(nice)
}
4b9b3361

Ответ 1

Имейте в виду, что игровая площадка имеет время, установленное на 2009-11-10 23:00:00 +0000 UTC, поэтому оно работает.

Правильный способ - использовать time.LoadLocation, хотя вот пример:

var countryTz = map[string]string{
    "Hungary": "Europe/Budapest",
    "Egypt":   "Africa/Cairo",
}

func timeIn(name string) time.Time {
    loc, err := time.LoadLocation(countryTz[name])
    if err != nil {
        panic(err)
    }
    return time.Now().In(loc)
}

func main() {
    utc := time.Now().UTC().Format("15:04")
    hun := timeIn("Hungary").Format("15:04")
    eg := timeIn("Egypt").Format("15:04")
    fmt.Println(utc, hun, eg)
}

Ответ 2

Ваш подход ошибочен. Страна может иметь несколько часовых поясов, например, США и Россию. Из-за летнего времени (DST) часовой пояс может иметь более одного раза, например, в Венгрии. Венгрия - это UTC +1: 00, а также UTC + 2: 00 для DST.

Для каждого местоположения, в котором вы хотите локальное время для данного времени UTC, используйте местоположение часового пояса IANA (tzdata). Например,

package main

import (
    "fmt"
    "time"
)

func main() {
    utc := time.Now().UTC()
    fmt.Println(utc)
    local := utc
    location, err := time.LoadLocation("Europe/Budapest")
    if err == nil {
        local = local.In(location)
    }
    fmt.Println("UTC", utc.Format("15:04"), local.Location(), local.Format("15:04"))
    local = utc
    location, err = time.LoadLocation("America/Los_Angeles")
    if err == nil {
        local = local.In(location)
    }
    fmt.Println("UTC", utc.Format("15:04"), local.Location(), local.Format("15:04"))
}

Вывод:

2014-08-14 23:57:09.151377514 +0000 UTC
UTC 23:57 Europe/Budapest 01:57
UTC 23:57 America/Los_Angeles 16:57

Литература:

База данных часовых поясов IANA

база данных tz

временные зоны базы данных tz

Часовой пояс

Время в Венгрии

Ответ 3

Спасите себя от беспорядка в определенных зонах, используйте локацию "Local". Здесь полный и практичный пример локального и UTC-преобразования:

package main

import (
    "fmt"
    "log"
    "time"
)

const (
    dateTimeFormat = "2006-01-02 15:04 MST"
    dateFormat    = "2006-01-02"
    timeFormat    = "15:04"
)

// A full cycle example of receiving local date and time,
// handing off to a database, retrieving as UTC, and formatting as local datetime
// This should be good in *any* timezone
func main() {
    // If using a form for entry, I strongly suggest a controlled format input like
    // <input type="date" ... > and <input type="time" ... >
    locallyEnteredDate := "2017-07-16"
    locallyEnteredTime := "14:00"

    // Build a time object from received fields (time objects include zone info)
    // We are assuming the code is running on a server that is in the same zone as the current user
    zone, _ := time.Now().Zone() // get the local zone
    dateTimeZ := locallyEnteredDate + " " + locallyEnteredTime + " " + zone
    dte, err := time.Parse(dateTimeFormat, dateTimeZ)
    if err != nil {
        log.Fatal("Error parsing entered datetime", err)
    }
    fmt.Println("dte:", dte) // dte is a legit time object
    // Perhaps we are saving this in a database.
    // A good database driver should save the time object as UTC in a time with zone field,
    // and return a time object with UTC as zone.

    // For the sake of this example, let assume an object identical to `dte` is returned
    // dte := ReceiveFromDatabase()

    // Convert received date to local.
    // Note the use of the convenient "Local" location https://golang.org/pkg/time/#LoadLocation.
    localLoc, err := time.LoadLocation("Local")
    if err != nil {
        log.Fatal(`Failed to load location "Local"`)
    }
    localDateTime := dte.In(localLoc)

    fmt.Println("Date:", localDateTime.Format(dateFormat))
    fmt.Println("Time:", localDateTime.Format(timeFormat))
}