You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am relatively new to Go and I am trying out the different built-in libraries to see how they work. I was trying out time.Truncate and came across an unexpected result. I am in New Zealand and was truncating all the time fields from a local datetime value. Rather than just truncate the time fields, the new value has also gone to the previous day. I suppose it did the truncation on the underlying value in UTC.
The sample program below demonstrates the issue and produces the following output:
dateOnly(2015-02-21 11:30:00 +1300 NZDT) was 2015-02-20 13:00:00 +1300 NZDT, but wanted 2015-02-21 00:00:00 +1300 NZDT
package main
import (
"fmt""log""time"
)
constlayout="2006 Jan 02 15:04:05 MST"funcmain() {
dateTime, dateWithTimeTruncated:=parseTestDates("2015 Feb 21 11:30:00.0 NZDT", "2015 Feb 21 00:00:00.0 NZDT")
if!dateOnly(dateTime).Equal(dateWithTimeTruncated) {
fmt.Printf("dateOnly(%v) was %v, but wanted %v", dateTime, dateOnly(dateTime), dateWithTimeTruncated)
}
}
// Truncate the time component and leave only the date fieldsfuncdateOnly(t time.Time) time.Time {
returnt.Truncate(time.Hour*24)
}
funcparseTestDates(dateWithTimestring, dateWithoutTimestring) (dateTime time.Time, dateWithTimeTruncated time.Time) {
dateTime, err:=time.Parse(layout, dateWithTime)
iferr!=nil {
log.Fatal(err)
}
dateWithTimeTruncated, err=time.Parse(layout, dateWithoutTime)
iferr!=nil {
log.Fatal(err)
}
returndateTime, dateWithTimeTruncated
}
The text was updated successfully, but these errors were encountered:
mikioh
changed the title
time.Truncate in NZ timezone changes the day when removing all time fields
time: Truncate in NZ timezone changes the day when removing all time fields
Feb 21, 2015
func (t Time) Truncate(d Duration) Time
Truncate returns the result of rounding t down to a multiple of d (since
the zero time). If d <= 0, Truncate returns t unchanged.
The important part is "since the zero time", which specifies a particular instant. That instant was not a day boundary in NZ, so truncating to a day boundary does not land on midnight NZ.
You could truncate the day info by calling t.Date() and then passing it back to time.Date with some zeros.
Hi,
I am relatively new to Go and I am trying out the different built-in libraries to see how they work. I was trying out
time.Truncate
and came across an unexpected result. I am in New Zealand and was truncating all the time fields from a local datetime value. Rather than just truncate the time fields, the new value has also gone to the previous day. I suppose it did the truncation on the underlying value in UTC.The sample program below demonstrates the issue and produces the following output:
The text was updated successfully, but these errors were encountered: