time.Parse偏移解析失败,显示”无法将00解析为-0700″。

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

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 (
	&quot;fmt&quot;
	&quot;log&quot;
	&quot;time&quot;
)

func main() {
	layout := &quot;1/02/2006 15:04:05 -700&quot;
	cellContent := &quot;7/28/2021 22:45:34&quot;
	t, err := time.Parse(layout, fmt.Sprintf(&quot;%s %s&quot;, cellContent, &quot;-800&quot;))
	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?

Go Playground

答案1

得分: 2

从@Adrian的评论中可以看出,他说得很准确。
布局时区必须有前导零。谢谢!

英文:

See comment from @Adrian, who nailed it.
The layout timezone must have a leading zero. Thanks!

huangapple
  • 本文由 发表于 2022年3月30日 20:59:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/71677717.html
匿名

发表评论

匿名网友

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

确定