英文:
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"))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论