How to parse 0000-00-00 00:00:00?

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

How to parse 0000-00-00 00:00:00?

问题

我正在解析一些来自MySQL数据库的时间值,其中一些值为零。为什么Go无法解析这些值,我该如何修复?

checkTime := "0000-00-00 00:00:00"
t, err := time.Parse("2006-01-02 15:04:05", checkTime)
if err != nil {
    log.Errorf("err %v, checkTime %v, ID %v", err, checkTime, bk.ID)
}

我得到的错误信息是:

err parsing time "0000-00-00 00:00:00": month out of range, checkin 0000-00-00 00:00:00

Go语言无法解析"0000-00-00 00:00:00"这个时间值,因为月份超出了有效范围。你可以尝试使用其他值来表示零值,或者在解析之前先进行一些预处理来处理这种情况。

英文:

I'm parsing some time values from a mysql database and some of them are zero. Why does Go fail to parse this and how can I fix it?

checkTime := "0000-00-00 00:00:00"
t, err := time.Parse("2006-01-02 15:04:05", checkTime)
if err !=nil{
	log.Errorf("err %v, checkTime %v, ID %v", err, checkTime, bk.ID)
}

I get:

err parsing time "0000-00-00 00:00:00": month out of range, checkin 0000-00-00 00:00:00

答案1

得分: 6

首先,在你的代码中有一个编译时错误:没有log.Errorf()函数(如果你使用标准的log包的话)。相反,你可以使用log.Printf()

其次,正如错误提示所说:“月份超出范围”。月份的范围是1..12。月份值为000是无效的。可以在这里查看有效的月份

零时间(或更精确的术语:类型time.Time零值)的打印方式如下:

log.Println("零时间是:", time.Time{}.Format("2006-01-02 15:04:05"))

输出结果为:

2009/11/10 23:00:00 零时间是: 0001-01-01 00:00:00

请注意,年、月和日都是1

还请注意,即使日期从1开始,也接受日期0,并将其解释为指定年份和月份的第一天之前的-1天。

因此,虽然"0000-01-01 00:00:00"可以解析并正确地给出"0000-01-01 00:00:00 +0000 UTC",但时间"0000-01-00 00:00:00"将给出"-0001-12-31 00:00:00 +0000 UTC"

确切的日期"0000-00-00 00:00:00"表示来自你的源(MySql数据库?)的零日期,在Go中,当检测到这个日期时,我会返回Go的time.Time的零值,这样你就可以使用Time.IsZero()方法在Go中进行检查。使用这个简单的辅助函数来解析你的时间:

func parseTime(s string) (time.Time, error) {
	if s == "0000-00-00 00:00:00" {
		return time.Time{}, nil
	}
	return time.Parse("2006-01-02 15:04:05", s)
}

Go Playground上查看示例的运行情况。

英文:

First a comile time error in your code: there is no log.Errorf() function (that is if you're using the standard log package). Instead use for example log.Printf().

Second, as your error states: month out of range. Month is in range if 1..12. A month value of 0 or 00 is invalid. See valid months here.

The zero time (or the more precise term: the zero value of the type time.Time) is as printed by:

log.Println("Zero time is:", time.Time{}.Format("2006-01-02 15:04:05"))

Output:

2009/11/10 23:00:00 Zero time is: 0001-01-01 00:00:00

Note that year, month and day are all 1.

Also note that even though days start from 1, day 0 is also accepted and is interpreted as -1 day from the first day of month specified by the year and month parts.

So while "0000-01-01 00:00:00" can be parsed and properly gives "0000-01-01 00:00:00 +0000 UTC", the time "0000-01-00 00:00:00" will give "-0001-12-31 00:00:00 +0000 UTC".

The exact date "0000-00-00 00:00:00" represents the zero date from your source (MySql db?), and so in Go when this date is detected I would return the zero value for the Go time.Time so you can check it from Go with the Time.IsZero() method. Use this simple helper function to parse your times:

func parseTime(s string) (time.Time, err) {
	if s == "0000-00-00 00:00:00" {
		return time.Time{}, nil
	}
	return time.Parse("2006-01-02 15:04:05", s)
}

See the examples in action on the Go Playground.

答案2

得分: 4

这是翻译好的内容:

这是因为没有公元0年,所以这个解析是错误的。公元前1年之后是公元1年。

在大多数情况下,为了准确表示,应该特别检查是否有公元0年,并在该位置存储一个NULL值。

从实际角度来看,任何给定为公元1年至公元600年左右的日期都缺乏充分的文献记录,很可能是错误的。在这段时间中有一些缺失的年份。学者们不确定有多少年份缺失,但他们大多数人都同意至少缺失了4年,可能多达8或10年。也就是说,实际上2015年应该是2019年至2029年。

英文:

That does not parse because there was no year zero. After 1 B.C. is 1 A.D.

With most contexts, it probably makes sense to make a special check for year zero and store a NULL value in its place.


From a practical viewpoint, any dates given as being in a year between 1 AD and around 600 A.D. are poorly documented and likely wrong. Somewhere in there are a few missed years. Academics aren't sure how many, but they mostly agree that at least 4 years are missing and maybe as many as 8 or 10. That is, the year 2015 actually should be 2019 to 2029.

答案3

得分: 1

类型Time的零值是公元1年1月1日,00:00:00.000000000 UTC。因此,在Go语言中没有0月。

英文:

The zero value of type Time is January 1, year 1, 00:00:00.000000000 UTC. So there is no month 0 in Go.

huangapple
  • 本文由 发表于 2015年9月7日 13:39:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/32431751.html
匿名

发表评论

匿名网友

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

确定