我试图解析一个Unix时间戳,但我得到了超出范围的错误。这对我来说没有意义,因为布局是正确的(就像在Go文档中一样):

package main

import "fmt"
import "time"

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

    fmt.Println(tm)
}

操场上


当前回答

分享一些我为日期创建的函数:

请注意,我想获取特定位置的时间(而不仅仅是UTC时间)。如果你想要UTC时间,只需删除loc变量和. in (loc)函数调用。

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")
}

其他回答

时间。解析函数不做Unix时间戳。相反,您可以使用strconv。ParseInt将字符串解析为int64并创建带有时间的时间戳。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

编辑:

从strconv更改。Atoi呼叫strconv。ParseInt避免32位系统上的int溢出。

根据go文档,Unix返回一个本地时间。

Unix返回与给定的Unix时间对应的本地时间

这意味着输出将取决于运行代码的机器,这通常是您所需要的,但有时,您可能希望使用UTC格式的值。

为此,我调整了代码片段,使其返回UTC时间:

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

在我的机器上打印(CEST)

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

这是一个老问题,但我注意到缺少一个实用的答案。

例如,我们正在使用MavLink协议,我们需要用这里定义的结构来处理消息。

如果我们有这样的数据结构:

Field Name Type Units Description
time_boot_ms uint64_t ms Timestamp (time since system boot).
press_abs float hPa Absolute pressure
press_diff float hPa Differential pressure 1
temperature int16_t cdegC Absolute pressure temperature
temperature_press_diff ** int16_t cdegC Differential pressure temperature (0, if not available). Report values of 0 (or 1) as 1 cdegC.

因此,我们接收需要使用time_boot_ms作为参考来处理的不断更新,以便将它们插入数据库并与其他消息同步。

我们能做什么?

正如我们所注意到的,时间是以毫秒为单位的,每个有Go经验的人都知道,由于某些未知的原因,将毫秒分辨率的Unix时间戳转换为time. time太复杂了。内置的time.Unix()函数只支持秒级和纳秒级精度。

我们如何才能达到毫秒级的精度?

好吧,我们可以等到他们发布1.7版的Go,或者我们必须把毫秒乘以纳秒,或者把它们分成秒和纳秒。

让我们实现第二个想法,把时间分成秒和纳秒:

unixUTCtime := time.Unix(ms/int64(1000), (ms%int64(1000))*int64(1000000))

现在我们可以将它封装在func中,并像这样在main中使用它:

package main

import (
    "fmt"
    "time"
)

const msInSecond int64 = 1e3
const nsInMillisecond int64 = 1e6

// UnixToMS Converts Unix Epoch from milliseconds to time.Time
func UnixToMS (ms int64) time.Time {
    return time.Unix(ms/msInSecond, (ms%msInSecond)*nsInMillisecond)
}


func main() {
    unixTimes := [...]int64{758991688, 758992188, 758992690, 758993186}
    var unixUTCTimes []time.Time
    for index, unixTime := range unixTimes {
        unixUTCTimes = append(unixUTCTimes, UnixToMS(unixTime))
        if index > 0 {
            timeDifference := unixUTCTimes[index].Sub(unixUTCTimes[index-1])
            fmt.Println("Time difference in ms :--->", timeDifference)
        }
    }
}

输出将是:

Time difference in ms :---> 500ms
Time difference in ms :---> 502ms
Time difference in ms :---> 496ms

Check in Go游乐场

我做了很多日志记录,其中时间戳是float64,并使用这个函数来获取时间戳作为字符串:

func dateFormat(layout string, d float64) string{
    intTime := int64(d)
    t := time.Unix(intTime, 0)
    if layout == "" {
        layout = "2006-01-02 15:04:05"
    }
    return t.Format(layout)
}

你可以直接利用时间。Unix时间函数,将Unix时间戳转换为UTC时间戳

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

Check in Go Playground: https://play.golang.org/p/5FtRdnkxAd