Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

time: Parse defaults year is 0000 while Time{} is 0001 #40925

Closed
mtt0 opened this issue Aug 20, 2020 · 3 comments
Closed

time: Parse defaults year is 0000 while Time{} is 0001 #40925

mtt0 opened this issue Aug 20, 2020 · 3 comments
Milestone

Comments

@mtt0
Copy link

@mtt0 mtt0 commented Aug 20, 2020

What version of Go are you using (go version)?

$ go version
go version go1.15 darwin/amd64

Does this issue reproduce with the latest release?

yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
irrelevant

What did you do?

code on Playgroud

package main

import (
	"fmt"
	"time"
)

func main() {
	fmt.Println("default time", time.Time{})

	if t, err := time.Parse("03:04:05.000000", "00:00:02.816500"); err != nil {
		fmt.Println(err)
		return
	} else {
		fmt.Println("parsed time: ", t, " duration: ", t.Sub(time.Time{}).Milliseconds())
	}

	// workaround
	if t, err := time.Parse("2006-01-02 03:04:05.000000", "0001-01-01 00:00:02.816500"); err != nil {
		fmt.Println(err)
		return
	} else {
		fmt.Println("workaround parsed time: ", t, " duration: ", t.Sub(time.Time{}).Milliseconds())
	}
}

What did you expect to see?

Output:

default time 0001-01-01 00:00:00 +0000 UTC
parsed time:  0001-01-01 00:00:02.8165 +0000 UTC  duration:  2816
workaround parsed time:  0001-01-01 00:00:02.8165 +0000 UTC  duration:  2816

What did you see instead?

Output:

default time 0001-01-01 00:00:00 +0000 UTC
parsed time:  0000-01-01 00:00:02.8165 +0000 UTC  duration:  -31622397183
workaround parsed time:  0001-01-01 00:00:02.8165 +0000 UTC  duration:  2816
@dmitshur dmitshur changed the title time.Parse defaults year is 0000 while time.Time{} is 0001 time: Parse defaults year is 0000 while Time{} is 0001 Aug 20, 2020
@dmitshur dmitshur added this to the Backlog milestone Aug 20, 2020
@dmitshur
Copy link
Member

@dmitshur dmitshur commented Aug 20, 2020

/cc @rsc per owners.

@dmitshur
Copy link
Member

@dmitshur dmitshur commented Aug 20, 2020

time.Time documentation states:

The zero value of type Time is January 1, year 1, 00:00:00.000000000 UTC.

time.Parse documentation says:

Elements omitted from the value are assumed to be zero or, when zero is impossible, one, so parsing "3:04pm" returns the time corresponding to Jan 1, year 0, 15:04:00 UTC (note that because the year is 0, this time is before the zero Time).

@ianlancetaylor
Copy link
Contributor

@ianlancetaylor ianlancetaylor commented Aug 20, 2020

Yes, there is no bug here. The code is behaving as documented and intended. Closing, but please comment if you disagree.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.