dromara / carbon

A simple, semantic and developer-friendly golang package for time.
https://pkg.go.dev/github.com/golang-module/carbon/v2
MIT License
4.81k stars 242 forks source link

Incorrect Parse of 0001-01-01 00:00:00 #232

Closed lesichkovm closed 7 months ago

lesichkovm commented 7 months ago

Hello,

I encountered an issue with the following code:

0001-01-01 00:00:00 +0000 GMT

golang version: 1.22 (latest)

carbon version: 2.3.10 (latest)

time zone: GMT

I expected to get:

0001-01-01 00:00:00

But I actually get:

nothing

Example of issue: https://play.golang.com/p/L6HDUXlCDEz

How it is supposed to work: image

Thanks!

gouguoyin commented 7 months ago

After testing, in golang, 0001-01-01 00:00:00+0000 GMT is a zero value, but 0000-01-01 00:00:00+0000 GMT or 0002-01-01 00:00:00+0000 GMT is not

image

https://play.golang.com/p/Ozr7GaWc0sD

lesichkovm commented 7 months ago

This looks very wrong. According to AI:

"0000-01-01 00:00:00" is not a valid date according to the most common date and time  standards. Here's why: 
Year Zero Doesn't Exist: Our calendar system doesn't have a year zero. There was no year 0 between 1 BC and 1 AD.
lesichkovm commented 7 months ago

We should add support for '0001-01-01 00:00:00' to Carbon, as it is a valid datetime, and can be saved in i.e. MySQL. For instance, when getting the data from the database it reads as '0001-01-01 00:00:00', but once it is passed into Carbon it gets blank.

lesichkovm commented 7 months ago

After testing, in golang, 0001-01-01 00:00:00+0000 GMT is a zero value, but 0000-01-01 00:00:00+0000 GMT or 0002-01-01 00:00:00+0000 GMT is not

Your tests will pass if you update the time layout:

https://play.golang.com/p/AUKnroJAxlj

gouguoyin commented 7 months ago

After testing, in golang, 0001-01-01 00:00:00+0000 GMT is a zero value, but 0000-01-01 00:00:00+0000 GMT or 0002-01-01 00:00:00+0000 GMT is not

Your tests will pass if you update the time layout:

https://play.golang.com/p/AUKnroJAxlj

The layout does not match the time string,the layout contains a time zone, but the time string does not

lesichkovm commented 7 months ago

Sorted by using 0002-01-01 00:00:00 which is the earliest valid date

gouguoyin commented 7 months ago

V2.3.11 has been optimized and released