在Golang中解析时间(将字符串解析为日期格式)时出现了错误。

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

Time parse error in Golang (parse string to date format)

问题

我正在尝试在Go语言中从字符串中解析时间,但出现了下面显示的错误。有人可以解释一下我做错了什么吗?

代码片段:

date := "2023-02-20T17:16:51.535133Z"
fmt.Println("date ", date)
date_parsed, err := time.Parse("2023-02-20T17:16:51.535133Z", date)
fmt.Println("date_parsed ", date_parsed)
if err != nil {
    fmt.Println(err)
    return
}

输出结果:

date  2023-02-20T17:16:51.535133Z
date_parsed 0001-01-01 00:00:00 +0000 UTC
parsing time "2023-02-20T17:16:51.535133Z" as "2023-02-20T17:16:51.535133Z": cannot parse "-02-20T17:16:51.535133Z" as "3"

你需要将时间格式字符串传递给time.Parse函数的第一个参数,该参数指定了你要解析的时间格式。在你的代码中,你传递的时间格式字符串与实际的时间字符串不匹配,导致解析失败。

根据你的时间字符串,正确的时间格式应该是"2006-01-02T15:04:05.999999Z"。请修改你的代码如下:

date := "2023-02-20T17:16:51.535133Z"
fmt.Println("date ", date)
date_parsed, err := time.Parse("2006-01-02T15:04:05.999999Z", date)
fmt.Println("date_parsed ", date_parsed)
if err != nil {
    fmt.Println(err)
    return
}

这样应该可以成功解析时间字符串。

英文:

I am trying to parse the time from a string in Go lang, but it shows the error shown below. Can someone please explain what I'm doing wrong?

Code snippet:

date := "2023-02-20T17:16:51.535133Z"
fmt.Println("date ", date)
date_parsed, err := time.Parse("2023-02-20T17:16:51.535133Z", date)
fmt.Println("date_parsed ", date_parsed)
if err != nil {
		fmt.Println(err)
		return
	}

Output:

date  2023-02-20T17:16:51.535133Z
date_parsed 0001-01-01 00:00:00 +0000 UTC
parsing time "2023-02-20T17:16:51.535133Z" as "2023-02-20T17:16:51.535133Z": cannot parse "-02-20T17:16:51.535133Z" as "3"

答案1

得分: 0

布局的元素必须使用文档中提到的参考时间来进行识别。

每个元素都通过示例显示了参考时间的格式。只有这些值才会被识别。

年份: "2006" "06"
月份: "Jan" "January" "01" "1"
星期几: "Mon" "Monday"
月份中的日期: "2" "_2" "02"
年份中的日期: "__2" "002"
小时: "15" "3" "03" (PM 或 AM)
分钟: "4" "04"
秒钟: "5" "05"
上午/下午标记: "PM"

换句话说:

  1. 月份
  2. 月份中的日期,年份中的日期
  3. 小时
  4. 分钟
  5. 秒钟
  6. 年份
  7. 时区

并且秒的小数部分是9个9。

你的布局是 2006-01-02T15:04:05.999999Z。你也可以使用现有的 time.RFC3339Nano

英文:

Elements of the layout must use the documented reference times to be recognizable.

> Each element shows by example the formatting of an element of the reference time. Only these values are recognized.

Year: "2006" "06"
Month: "Jan" "January" "01" "1"
Day of the week: "Mon" "Monday"
Day of the month: "2" "_2" "02"
Day of the year: "__2" "002"
Hour: "15" "3" "03" (PM or AM)
Minute: "4" "04"
Second: "5" "05"
AM/PM mark: "PM"

In other words:

  1. Month
  2. Day of month, day of year.
  3. Hour
  4. Minute
  5. Second
  6. Year
  7. Time zone

And fractions of a second are 9's.

Your layout is 2006-01-02T15:04:05.999999Z. You can also use the existing time.RFC3339Nano.

答案2

得分: 0

答案是,你的日期时间格式实际上不是一个格式,而是日期时间本身。对于time包,你必须为每个字段提供特定的值,以便知道哪个字段是年、小时、分钟等。你可以在time包文档中的布局常量中看到这些值:

Layout      = "01/02 03:04:05PM '06 -0700" // 参考时间,按照数字顺序排列。

在稍微更抽象的层面上,如果你可以将要解析的时间作为布局传递进去,那么布局字符串将完全没有意义。它将依赖于time包知道你要解析的日期时间字符串的格式,以解析布局字符串,然后用于解析日期时间字符串。那么为什么还需要布局字符串呢?如果time知道你的意图而不需要你告诉它,不仅会使这个字段变得多余,而且还会大大减少解析日期时间字符串的灵活性。如果你可以只传递01/01/01来解析10/02/232023年2月10日,那么你如何解析以美国格式表示的日期,比如MM/DD/YY?或者如何将02/12解析为2012年2月而不是2月12日12月2日

英文:

The answer is that your datetime format isn't actually a format; it's the datetime itself. There are particular values you have to give for each field for the time package to know what field is the year, hours, mins, etc. You can see it in the Layout constant in the time package documentation:

Layout      = "01/02 03:04:05PM '06 -0700" // The reference time, in numerical order.

At a slightly more meta level, the layout string would be entirely pointless if you could pass in the time you are trying to parse as the layout. It would rely on the time package knowing the format of the datetime string you are trying to parse in order to parse the layout string, which would then be used to parse the datetime string -- so then why would the layout string be needed at all, if time knew what you meant without you telling it? Not only would it make this field redundant, but it would also remove a huge amount of flexibility to parse a datetime string in whatever format you want. If you could just pass in 01/01/01 to parse 10/02/23 as Feb 10, 2023, how would you parse a date in the American format with MM/DD/YY? Or how would you parse 02/12 to mean Feb 2012 rather than Feb 12 or Dec 2?

huangapple
  • 本文由 发表于 2023年2月21日 03:17:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/75513333.html
匿名

发表评论

匿名网友

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

确定