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: Truncate in NZ timezone changes the day when removing all time fields #9949

Closed
jennerm opened this issue Feb 21, 2015 · 2 comments

Comments

Projects
None yet
4 participants
@jennerm
Copy link

commented Feb 21, 2015

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:

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"
)

const layout = "2006 Jan 02 15:04:05 MST"

func main() {
    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 fields
func dateOnly(t time.Time) time.Time {
    return t.Truncate(time.Hour * 24)
}

func parseTestDates(dateWithTime string, dateWithoutTime string) (dateTime time.Time, dateWithTimeTruncated time.Time) {
    dateTime, err := time.Parse(layout, dateWithTime)
    if err != nil {
        log.Fatal(err)
    }
    dateWithTimeTruncated, err = time.Parse(layout, dateWithoutTime)
    if err != nil {
        log.Fatal(err)
    }
    return dateTime, dateWithTimeTruncated
}

@mikioh 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

@adg

This comment has been minimized.

Copy link
Contributor

commented Feb 22, 2015

On my machine (darwin) I'm not able to parse NZDT as a valid time zone. I wonder why that is?

@rsc

This comment has been minimized.

Copy link
Contributor

commented Apr 10, 2015

The docs say:

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.

@rsc rsc closed this Apr 10, 2015

@golang golang locked and limited conversation to collaborators Jun 25, 2016

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
You can’t perform that action at this time.