将毫秒转换为Golang中的时间。

huangapple go评论76阅读模式
英文:

Convert to time in Golang from milliseconds

问题

我有一些 JSON 数据,其中有一个字段叫做 lastModified,它包含以毫秒为单位的时间。我想使用 json.Unmarshaller 将这些数据转换为结构体类型。我已经将字段映射到了 JSON 字段上,但是转换似乎不起作用。

例如:

我的 JSON 数据如下:

{
   "name" : "hello",
   "lastModified" : 1438167001716
}

结构体如下:

type Model struct {
	Name         string    `json:"name"`
	Lastmodified time.Time `json:"lastModified"`
}

看起来时间没有正确转换。我该如何从这些毫秒中获取时间?

注:lastModifiedTime 的毫秒值是从 Java 的 System.currentTimeMillis(); 获取的。

英文:

I have a some json data where there is a field called lastModifed contains time in millis. I wanted to convert this data into a struct type with json.UnMarshaller. I have mapped the field with json filed. But the conversion seems not working.

IE :

My Json looks like this:

{
   "name" : "hello",
   "lastModified" : 1438167001716
}

and struct Looks like

type Model struct {
	Name         string    `json:"name"`
	Lastmodified time.Time `json:"lastModified"`
}

looks not converting the time properly. how can i get the time from those millis??

NB: The millis of lastModifiedTime are getting from java System.currentTimeMillis();

答案1

得分: 47

在Go语言中,time.Time类型通过RFC3339的字符串表示形式将其编组为JSON。因此,您需要使用int64而不是time.Time来解组您的JSON,并在之后自行进行转换:

type Model struct {
    Name    string `json:"name"`
    Millis  int64  `json:"lastModified"`
}

func (m Model) Lastmodified() time.Time {
    return time.Unix(0, m.Millis*int64(time.Millisecond))
}

Go playground

另外,您还可以使用time.Time的特殊包装器,并在其中重写UnmarshalJSON方法:

type Model struct {
    Name         string   `json:"name"`
    Lastmodified javaTime `json:"lastModified"`
}

type javaTime time.Time

func (j *javaTime) UnmarshalJSON(data []byte) error {
    millis, err := strconv.ParseInt(string(data), 10, 64)
    if err != nil {
        return err
    }
    *j = javaTime(time.Unix(0, millis*int64(time.Millisecond)))
    return nil
}

Go playground

英文:

In golang time.Time marshals to JSON using RFC3339, string representation. So you need to unmarshal your json using int64 instead of time.Time and convert after it by yourself:

type Model struct {
	Name   string `json:"name"`
	Millis int64  `json:"lastModified"`
}

func (m Model) Lastmodified() time.Time {
	return time.Unix(0, m.Millis * int64(time.Millisecond))
}

<kbd>Go playground</kbd>

Also you can use special wrapper above time.Time and override UnmarshalJSON there:

type Model struct {
	Name         string   `json:&quot;name&quot;`
	Lastmodified javaTime `json:&quot;lastModified&quot;`
}

type javaTime time.Time

func (j *javaTime) UnmarshalJSON(data []byte) error {
	millis, err := strconv.ParseInt(string(data), 10, 64)
	if err != nil {
		return err
	}
	*j = javaTime(time.Unix(0, millis * int64(time.Millisecond)))
	return nil
}

<kbd>Go playground</kbd>

答案2

得分: 16

你可以使用time包中的UnixMilli方法:

myTime := time.UnixMilli(myMilliseconds)

参考链接:https://pkg.go.dev/time#UnixMilli

英文:

You can use the UnixMilli method in time:

myTime := time.UnixMilli(myMilliseconds)

Reference: https://pkg.go.dev/time#UnixMilli

答案3

得分: 6

尝试这个:

func ParseMilliTimestamp(tm int64) time.Time {
    sec := tm / 1000
    msec := tm % 1000
    return time.Unix(sec, msec*int64(time.Millisecond))
}

这段代码的作用是将以毫秒为单位的时间戳解析为Go语言中的时间对象。它将输入的时间戳分为秒和毫秒两部分,然后使用time.Unix函数将其转换为对应的时间对象。

英文:

Try this:

func ParseMilliTimestamp(tm int64) time.Time {
	sec := tm / 1000
	msec := tm % 1000
	return time.Unix(sec, msec*int64(time.Millisecond))
}

huangapple
  • 本文由 发表于 2015年7月31日 19:22:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/31744970.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定