Home > database >  why does time.Date() give different results when using hardcoded int values vs the values returned b
why does time.Date() give different results when using hardcoded int values vs the values returned b

Time:11-04

I am trying to retrieve data from an API in which my request must have a start and end unix timestamp, and I a getting really strange results with my methodology.

(I'll use dates in my example based on the Go Playground, which always reports time.Now().UTC() as 2009-11-10 23:00:00)

I will be scheduling this to run daily, and I'd like it to always retrieve all records from 2 days ago, so I need to send a start time of 2009-11-08 00:00:00 and an end time of 2009-11-08 23:59:59.

Here's a summary of my methodology:

// Retrieve the time now in UTC 
now := time.Now().UTC()

// Subtract two days from now 
twoDaysAgo := now.AddDate(0, 0, -2)

// Extract the day, month, and year individually, 
// which will be used to "rebuild" or "reset" our clock to 00:00:00
day, month, year := twoDaysAgo.Date()

// Build our start time using the extracted day, month, and year 
startTime := time.Date(year, month, day, 0, 0, 0, 0, time.UTC)

when startTime is printed, I expect to get 2009-11-08 00:00:00 but instead get 0014-05-02 00:00:00

Playground link

package main

import (
    "fmt"
    "time"
)

func main() {
    // we are trying to "reset the clock" on a specified date, 2 days in the past from today.
    // this time.Time will be used as a "start" timestamp for a request we want to make.

    // first, get the current time, which is hardcoded in Go Playground to be 2009-11-10 23:00:00, 
    // and prints the expected value
    now := time.Now().UTC()
    fmt.Println("Now:", now)

    // subtract two days, which should be 2009-11-08 23:00:00 and prints the expected value
    twoDaysAgo := now.AddDate(0, 0, -2)
    fmt.Println("Two days ago:", twoDaysAgo)

    // extract the day, month, and year from twoDaysAgo, which should be 2009 November 8
    // this prints the expected values so we can confirm that day, month, and year each contain what we need
    day, month, year := twoDaysAgo.Date()
    fmt.Println("Extracted day, month, and year:", day, month, year)

    // create a new time.Time using time.Date() so we can "reset" the clock to 00:00:00
    // we use day, month, and year extracted earlier as parameters to achieve this.
    // this prints a wildly inaccurate date of 0014-05-02 00:00:00 for a reason I don't understand.
    dateFromDateExtraction := time.Date(year, month, day, 0, 0, 0, 0, time.UTC)
    fmt.Println("This date is weird: ", dateFromDateExtraction)

    // if instead we hardcode the parameters for time.Date(),
    // the result is what you expect and this prints 2009-11-08 00:00:00
    dateHardcoded := time.Date(2009, time.November, 8, 0, 0, 0, 0, time.UTC)
    fmt.Println("This date appears as expected: ", dateHardcoded)
}

CodePudding user response:

You've reversed the return values from Date(). It should be this:

year, month, day := twoDaysAgo.Date()
  • Related