在Go语言中将日期格式化为dd_mm_yyyy会得到奇怪的值。

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

Formatting dates as dd_mm_yyyy in Go gives strange values

问题

所以,如标题所述,我正在尝试使用time.Now().Format("02_01_2006")将日期格式化为dd_mm_yy格式,就像在这个playground会话中所示:

http://play.golang.org/p/alAj-OcRZt

首先的问题是,dd_mm_yyyy不是一个可接受的格式,只有dd_mm_yy是可接受的,这没问题,我可以自己处理返回的字符串。

我想请你帮我弄清楚Go在处理这个输入时到底在做什么。

你应该注意到你得到的结果是:

10_1110009

相差了几千年,而且它丢失了下划线,只有_2才会这样。这个字符序列在这里代表了什么特殊的意义吗?

将最后一个下划线替换为连字符或空格会返回一个有效的结果。dd_mm_yy可以正常工作。只有这个特殊情况似乎完全无法处理。

在我的本地机器上(Go playground上有一个特定的日期),今天(第5天)的结果是:

05_01 5016

同样奇怪,甚至更奇怪,因为它用一个空格替换了一个似乎是ANSIC的东西。

英文:

So as in the title, I'm trying to format a date in dd_mm_yy format using time.Now().Format("02_01_2006") as shown in this playground session:

http://play.golang.org/p/alAj-OcRZt

First problem, dd_mm_yyyy isn't an acceptable format, only dd_mm_yy is, which is fine I can manipulate the returned string myself.

The problem I have for you is to help me figure out what Go is even trying to do with this input.

You should notice the result you get is:

10_1110009

A good few thousand years off and it's lost the underscore which it only does it for _2. Does this character sequence represent something special here?

Replacing the last underscore with a hyphen or space returns a valid result. dd_mm_yy works fine. Just this particular case seems to completely fly off the handle.

On my local machine (Go playground is on a specific date) the result for today (the 5th) is:

05_01 5016

Which is equally strange, if not moreso as it's substituted in a space which seems to be an ANSIC thing.

答案1

得分: 5

这很可能是由于以下错误引起的:https://github.com/golang/go/issues/11334

这个问题已经在Go 1.6beta1中修复了。

英文:

This is very likely due to the following bug: https://github.com/golang/go/issues/11334

This has been fixed in Go 1.6beta1

答案2

得分: 0

在他们的 GitHub 上发现了一个问题:

https://github.com/golang/go/issues/11334

基本上,_2 将 2 视为参考时间的日期值,然后尝试解析剩余部分(006),但它无法识别,所以从那里开始出错了。

英文:

Found an issue from their github:

https://github.com/golang/go/issues/11334

Basically _2 is taking the 2 as the day value from the reference time and then trying to parse the rest (006) which it doesn't recognise so it all goes wrong from there.

huangapple
  • 本文由 发表于 2016年1月6日 00:06:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/34616089.html
匿名

发表评论

匿名网友

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

确定