Skip to content

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

@mtt0

Description

@mtt0

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions