dateparse icon indicating copy to clipboard operation
dateparse copied to clipboard

dateparse.ParseAny and dateparse.ParseLocal does not adhere timezone

Open u007 opened this issue 7 years ago • 3 comments

input: 2017-12-31T16:00:00Z

result: 2017-12-31 16:00:00 +0800

Expected: 2018-01-01 00:00:00 +0800

u007 avatar Oct 22 '18 03:10 u007

I wrote this, which seems to have a different Expected output, trying to understand the difference between your expected and what golang seems to output, any thoughts?

https://play.golang.org/p/VA5IDWKOcJA

package main

import (
	"fmt"
	"time"
)

func main() {

	loc, err := time.LoadLocation("Australia/Perth")
	if err != nil {
		panic(err.Error())
	}
	t, err := time.ParseInLocation("2006-01-02T15:04:05Z","2017-12-31T16:00:00Z", loc)
	if err != nil {
		panic(err.Error())
	}
	fmt.Printf("time: %s", t)
}

outputs time: 2017-12-31 16:00:00 +0800 AWST

  • for los-angeles https://play.golang.org/p/B2mnMB1s-4q

araddon avatar Oct 22 '18 19:10 araddon

Alittle more built out: https://play.golang.org/p/3b3l8BS81rD

time.ParseInLocation() : 2017-12-31 16:00:00 +0800 AWST 

time.ParseInLocation() : t.In(perth) => 2017-12-31 16:00:00 +0800 AWST 

time.Parse() (w time.Local=Australia/Perth) : 2017-12-31 16:00:00 +0000 UTC 

time.Parse() (w time.Local=Australia/Perth, t.In(perth)) : 2018-01-01 00:00:00 +0800 AWST 

araddon avatar Oct 22 '18 19:10 araddon

I'd also like to vote for this being an issue. consider the comparison to the behavior of "time"

package main

import ( "fmt" "github.com/araddon/dateparse" "time" )

func main() { loc, err := time.LoadLocation("US/Pacific") fmt.Println(loc.String(), err) fmt.Println()

const longForm = "Jan 2, 2006 at 3:04pm (MST)"
tstr := "Jul 9, 2012 at 5:02am (EST)"
fmt.Println("parse in...")
t, err := time.ParseInLocation(longForm, tstr, loc)
fmt.Println(t, err)

t, err = dateparse.ParseIn(tstr, loc)
fmt.Println(t, err)

fmt.Println("Z")
t, err = dateparse.ParseIn("Jul 9, 2012 at 5:02Z", loc)
fmt.Println(t, err)

fmt.Println()
fmt.Println("parse...")

t, err = time.Parse(longForm, tstr)
fmt.Println(t, err)

t, err = dateparse.ParseAny(tstr)
fmt.Println(t, err)

fmt.Println("Z")
t, err = dateparse.ParseAny("Jul 9, 2012 at 5:02Z")
fmt.Println(t, err)

}

evan$ go run ~/ex.go US/Pacific

parse in... 2012-07-09 05:02:00 +0000 EST 2012-07-09 05:02:00 -0700 PDT Z 2012-07-09 05:02:00 -0700 PDT

parse... 2012-07-09 05:02:00 +0000 EST 2012-07-09 05:02:00 +0000 UTC Z 2012-07-09 05:02:00 +0000 UTC

Note, that ParseIn() is essentially discarding the timezone (even for "Z"), while ParseInLocation() honors it, even though they have the goofy (but documented) 0 for the offset.

ejb123 avatar Oct 07 '20 23:10 ejb123