Golang의 시간대 전환

2891 단어

코드

package main

import (
    "fmt"
    "time"
)

const TIME_LAYOUT = "2006-01-02 15:04:05"

func parseWithLocation(name string, timeStr string) (time.Time, error) {
    locationName := name
    if l, err := time.LoadLocation(locationName); err != nil {
        return time.Time{}, err
    } else {
        //        
        lt, _ := time.ParseInLocation(TIME_LAYOUT, timeStr, l)
        //        
        fmt.Println(time.Now().In(l).Format(TIME_LAYOUT))
        return lt, nil
    }
}
func testTime() {
    str:=time.Now().Format("2006-01-02 15:04:05")
    //    
    t1,err:=parseWithLocation("America/Cordoba", str)
    if err !=nil{
        fmt.Println(err)
    }
    fmt.Println(t1)

    t2,err:=parseWithLocation("Asia/Shanghai", str)
    if err !=nil{
        fmt.Println(err)
    }
    fmt.Println(t2)

    t3,err:=parseWithLocation("Asia/Chongqing", str)
    if err !=nil{
        fmt.Println(err)
    }
    fmt.Println(t3)
}

func main() {
    testTime()
}

 
전재 대상:https://www.cnblogs.com/angelyan/p/11103226.html

좋은 웹페이지 즐겨찾기