How can I compare a parsed date to an empty date?

600 Views Asked by At

I receive a JSON message coming from an API written in Go. One of the fields is a date, and it may be a time.Time{} that is turned into "0001-01-01T00:00:00Z" when creating the JSON.

On the receiving end, I will unmarshal the JSON message into a variable that matches the structure of the message, one of the field being the date one I mention above (of type time.Time).

I would like to check it against an empty one.

I expected that the code below would be OK, but it is not:

package main

import (
    "fmt"
    "time"
)

func main() {
    receivedDate := "0001-01-01T00:00:00Z"  // this mocks the content of the received field
    receivedDateParsed, _ := time.Parse(time.RFC3339, receivedDate) // this is equivlent to the unmarshalling into the message stricture
    if receivedDateParsed == time.Time{} {  // my comparison
        fmt.Print("date is empty")
    }
}

The error is Type 'time.Time' is not an expression but I am not sure what this means in the context of the time.Time{} I used.

0

There are 0 best solutions below