How do you convert a string timestamp to a UTC date that I can display, in go?

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

How do you convert a string timestamp to a UTC date that I can display, in go?

问题

我正在尝试以人类可读的形式显示一个时间戳数组。数组来自数据库,并且我将其发送到模板中进行迭代。

t.Timestamp 是一个字符串,来自数据库。

我尝试了 t.TimeStamp = strconv.Atoi(t.TimeStamp),但出现了错误:multiple-value strconv.Atoi() in single-value context

我不明白为什么会这样。如果有人能帮我解决这个问题,我将不胜感激。
另外,如果你知道将字符串时间戳转换为UTC日期的更好和更快的方法,我将非常感谢你的帮助。

谢谢。

英文:

I am trying to display an array of timestamps in human readable form. Array comes from db, and I send it into the template, where the iterations happens.

t.Timestamp is a string, comes from the db

I tried t.TimeStamp = strconv.Atoi(t.TimeStamp) and error came up: multiple-value strconv.Atoi() in single-value context

I don't understand why it does that. If someone could help me figure this out, please?
Also if you know a better and quicker way of turning a string timestamp into a UTC date, I would greatly appreciate the help.

Thanks.

答案1

得分: 2

strconv/#Atoi 返回多个值。

func Atoi(s string) (i int, err error)

你需要检查错误值。

ts, ok := strconv.Atoi(t.TimeStamp)
if ok != nil {
  ts = 0
}

说到时间戳和数据库,你可以查看mgo/bson项目
它有一个在timestamp.go中使用的Timestamp

int64时间戳,你可以使用time.Unix()来获取一个Time

然后你可以格式化这个时间

t := time.Unix(ts, 0)
fmt.Println(t.Format("2006-01-02 15:04:05 -0700"))
英文:

strconv/#Atoi returns multiple values.

func Atoi(s string) (i int, err error)

You need to check for the error value.

ts, ok := strconv.Atoi(t.TimeStamp)
if ok != nil {
  ts = 0
}

Speaking of timestamp and db, you can check out the mgo/bson project.
It has a Timestamp class used in timestamp.go.

From a int64 timestamp, you can use time.Unix() to get a Time.

And you can then format that Time.

t := time.Unix(ts, 0)
fmt.Println(t.Format("2006-01-02 15:04:05 -0700"))

huangapple
  • 本文由 发表于 2014年6月30日 19:19:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/24488952.html
匿名

发表评论

匿名网友

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

确定