在尝试解析日期时,出现了找不到时间符号值的错误。

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

Getting error could not find symbol value of time when trying to parse the date

问题

当我尝试运行以下语句时:

eolTime, tErr := time.Parse(time.RFC3339, eolDetails.EOL.(string))
// eolDetails.EOL.(string) 的值为 2021-04-30

我收到一个错误,错误信息是 error evaluating "time.RFC3339" as argument layout in function time.Parse: could not find symbol value for time" (unused functions are not included into executable)

这个错误是什么意思?我需要怎么解析日期为 Go 日期格式?

这里还有一个调试时显示错误的截图:
在尝试解析日期时,出现了找不到时间符号值的错误。

奇怪的是,在 playground 上运行正常。有什么想法可能出了什么问题吗?

英文:

When I try to run the following statement:

eolTime, tErr := time.Parse(time.RFC3339, eolDetails.EOL.(string))
// eolDetails.EOL.(string) evaluates to 2021-04-30

I get an error saying error evaluating "time.RFC3339" as argument layout in function time.Parse: could not find symbol value for time" (unused functions are not included into executable)"

What does this error mean? What do I need to do parse the date into go date?

Here is also the screenshot while debugging that shows the error:
在尝试解析日期时,出现了找不到时间符号值的错误。

It is strange, that it works fine on the playground. Any idea what could be wrong here?

答案1

得分: 3

Go版本1.20+

Go版本1.20添加了三个新的布局常量:DateTimeDateOnlyTimeOnly <sup>(发布说明)</sup>。因此,如果您要解析的字符串只包含日期,您可以使用DateOnly布局。

eolDetails := "2021-04-30"
eolTime, tErr := time.Parse(time.DateOnly, eolDetails)
if tErr != nil {
    panic(err)
}
fmt.Println(eolTime)

https://go.dev/play/p/qrBiETSmRrs


Go版本1.19及更早版本

如果您使用的版本早于1.20,则可以使用自定义布局。在使用自定义布局时,您只需要确保它们是一个有效的“参考时间”,如包的文档所解释的那样:

> 要定义自己的格式,请__写下参考时间按您的方式格式化的样子__;参考ANSIC、StampMicro或Kitchen等常量的值以获取示例。模型是为了演示参考时间的样子,以便Format和Parse方法可以将相同的转换应用于一般时间值。

其中“参考时间”定义为:

> ... 这些布局中使用的参考时间是特定的时间戳:
> none &gt; 01/02 03:04:05PM '06 -0700 &gt;

因此,要解析您的日期字符串,您可以简单地执行以下操作:

eolLayout := "2006-01-02"
eolDetails := "2021-04-30"
eolTime, tErr := time.Parse(eolLayout, eolDetails)
if tErr != nil {
    panic(tErr)
}
fmt.Println(eolTime)

https://go.dev/play/p/-6SqMz94VMT?v=goprev


相关的StackOverflow帖子:

英文:

Go version 1.20+

Go version 1.20 adds three new layout constants: DateTime, DateOnly, and TimeOnly <sup>(release notes)</sup>. So, if the string you want to parse contains only a date you can use the DateOnly layout.

eolDetails := &quot;2021-04-30&quot;
eolTime, tErr := time.Parse(time.DateOnly, eolDetails)
if tErr != nil {
	panic(err)
}
fmt.Println(eolTime)

https://go.dev/play/p/qrBiETSmRrs


Go version 1.19 and older

If you are using a version that is older than 1.20, then you can use a custom layout. When using custom layouts you only need to make sure that they are a valid reference time, as explained by the package's documentation:

> To define your own format, write down what the reference time would look like formatted your way; see the values of constants like ANSIC, StampMicro or Kitchen for examples. The model is to demonstrate what the reference time looks like so that the Format and Parse methods can apply the same transformation to a general time value.

Where the "reference time" is defined as:

> ... The reference time used in these layouts is the specific time stamp:
> none
&gt; 01/02 03:04:05PM &#39;06 -0700
&gt;

So to parse your date string you can simply do the following:

eolLayout := &quot;2006-01-02&quot;
eolDetails := &quot;2021-04-30&quot;
eolTime, tErr := time.Parse(eolLayout, eolDetails)
if tErr != nil {
	panic(tErr)
}
fmt.Println(eolTime)

https://go.dev/play/p/-6SqMz94VMT?v=goprev


Related StackOverflow posts:

huangapple
  • 本文由 发表于 2023年3月17日 02:13:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/75760143.html
匿名

发表评论

匿名网友

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

确定