program story

유닉스 타임 스탬프를 time.Time으로 구문 분석하는 방법

inputbox 2020. 8. 19. 08:02
반응형

유닉스 타임 스탬프를 time.Time으로 구문 분석하는 방법


Unix 타임 스탬프 를 구문 분석하려고하는데 범위를 벗어났습니다. 레이아웃이 정확하기 때문에 (Go 문서에서와 같이) 실제로 의미가 없습니다.

package main

import "fmt"
import "time"

func main() {
    tm, err := time.Parse("1136239445", "1405544146")
    if err != nil{
        panic(err)
    }

    fmt.Println(tm)
}

운동장


time.Parse함수는 Unix 타임 스탬프를 수행하지 않습니다. 대신 다음을 사용 strconv.ParseInt하여 문자열을 구문 분석 int64하고 타임 스탬프를 만들 수 있습니다 time.Unix.

package main

import (
    "fmt"
    "time"
    "strconv"
)

func main() {
    i, err := strconv.ParseInt("1405544146", 10, 64)
    if err != nil {
        panic(err)
    }
    tm := time.Unix(i, 0)
    fmt.Println(tm)
}

산출:

2014-07-16 20:55:46 +0000 UTC

플레이 그라운드 : http://play.golang.org/p/v_j6UIro7a

편집하다:

32 비트 시스템에서 int 오버플 strconv.Atoistrconv.ParseInt를 방지하기 위해 에서 변경되었습니다 .


time.Unix 타임 스탬프를 UTC로 변환하는 시간의 Unix 기능

package main

import (
  "fmt"
  "time"
)


func main() {
    unixTimeUTC:=time.Unix(1405544146, 0) //gives unix time stamp in utc 

    unitTimeInRFC3339 :=unixTimeUTC.Format(time.RFC3339) // converts utc time to RFC3339 format

    fmt.Println("unix time stamp in UTC :--->",unixTimeUTC)
    fmt.Println("unix time stamp in unitTimeInRFC3339 format :->",unitTimeInRFC3339)
}

산출

unix time stamp in UTC :---> 2014-07-16 20:55:46 +0000 UTC
unix time stamp in unitTimeInRFC3339 format :----> 2014-07-16T20:55:46Z

Go Playground에서 확인 : https://play.golang.org/p/5FtRdnkxAd


날짜에 대해 만든 몇 가지 기능 공유 :

Please note that I wanted to get time for a particular location (not just UTC time). If you want UTC time, just remove loc variable and .In(loc) function call.

func GetTimeStamp() string {
     loc, _ := time.LoadLocation("America/Los_Angeles")
     t := time.Now().In(loc)
     return t.Format("20060102150405")
}
func GetTodaysDate() string {
    loc, _ := time.LoadLocation("America/Los_Angeles")
    current_time := time.Now().In(loc)
    return current_time.Format("2006-01-02")
}

func GetTodaysDateTime() string {
    loc, _ := time.LoadLocation("America/Los_Angeles")
    current_time := time.Now().In(loc)
    return current_time.Format("2006-01-02 15:04:05")
}

func GetTodaysDateTimeFormatted() string {
    loc, _ := time.LoadLocation("America/Los_Angeles")
    current_time := time.Now().In(loc)
    return current_time.Format("Jan 2, 2006 at 3:04 PM")
}

func GetTimeStampFromDate(dtformat string) string {
    form := "Jan 2, 2006 at 3:04 PM"
    t2, _ := time.Parse(form, dtformat)
    return t2.Format("20060102150405")
}

According to the go documentation, Unix returns a local time.

Unix returns the local Time corresponding to the given Unix time

This means the output would depend on the machine your code runs on, which, most often is what you need, but sometimes, you may want to have the value in UTC.

To do so, I adapted the snippet to make it return a time in UTC:

i, err := strconv.ParseInt("1405544146", 10, 64)
if err != nil {
    panic(err)
}
tm := time.Unix(i, 0)
fmt.Println(tm.UTC())

This prints on my machine (in CEST)

2014-07-16 20:55:46 +0000 UTC

Check this repo: https://github.com/araddon/dateparse

You can use

t, err := dateparse.ParseAny(timestampString)

Just use time.Parse

example:

package main

import (
    "fmt"
    "time"
)

func main() {
    fromString := "Wed, 6 Sep 2017 10:43:01 +0300"
    t, e := time.Parse("Mon, _2 Jan 2006 15:04:05 -0700", fromString)
    if e != nil {
        fmt.Printf("err: %s\n", e)
    }
    fmt.Printf("UTC time: %v\n", t.UTC())
}

Working example on play.golang.org.

참고URL : https://stackoverflow.com/questions/24987131/how-to-parse-unix-timestamp-to-time-time

반응형