英文:
time.Parse offset parsing fails with "cannot parse <x>00 as -0700"
问题
我希望将一个没有提供时区的字符串时间戳转换为带有时区为UTC -08:00的time
。
代码:
package main
import (
"fmt"
"log"
"time"
)
func main() {
layout := "1/02/2006 15:04:05 -700"
cellContent := "7/28/2021 22:45:34"
t, err := time.Parse(layout, fmt.Sprintf("%s %s", cellContent, "-800"))
if err == nil {
fmt.Println(t.String())
} else {
log.Fatal(err)
}
}
这个代码运行失败,显示以下错误信息:
将时间“7/28/2021 22:45:34 -800”解析为“1/02/2006 15:04:05 -700”时出错:
无法将“800”解析为“-700”。
我认为在我的布局字符串中有一个错误,但我还没有找出来。我做错了什么?
英文:
I wish to convert a string timestamp (for which no timezone was provided) to a time
with timezone of UTC -08:00.
Code:
package main
import (
"fmt"
"log"
"time"
)
func main() {
layout := "1/02/2006 15:04:05 -700"
cellContent := "7/28/2021 22:45:34"
t, err := time.Parse(layout, fmt.Sprintf("%s %s", cellContent, "-800"))
if err == nil {
fmt.Println(t.String())
} else {
log.Fatal(err)
}
}
This fails with message:
> parsing time "7/28/2021 22:45:34 -800" as "1/02/2006 15:04:05 -700":
> cannot parse "800" as " -700"
I believe I have an error in my layout string, but haven't been able to identify it. What am I doing wrong?
答案1
得分: 2
从@Adrian的评论中可以看出,他说得很准确。
布局时区必须有前导零。谢谢!
英文:
See comment from @Adrian, who nailed it.
The layout timezone must have a leading zero. Thanks!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论