0

I'm trying to compare two time.Time dates in Go using the Before function. The issue is that one of these dates comes from a MySQL datetime field.

When I Scan the result, it is saved as a UTC timezone, but I wrote in database in my local time (UTC +1). This is a problem when I compare the database date with a time.Now() that is in my local time.

I extracted the date with a scan into a time.Time variable with ?parseTime=true on the connection string.

Do you have any suggestion? I don't want to add 1 hour after reading the date from the DB because it can cause me problems with summer time (UTC +2).

I tried to extract the date from DB and convert with UTC function:

data, _ = time.Parse("2006-01-02 15:04:05", data.UTC().Format("2006-01-02 15:04:05"))

but without any success.

blackgreen
  • 34,072
  • 23
  • 111
  • 129

1 Answers1

-2

to convert a mysql datetime value to a Go time.Time correctly I recommend this code:

datesplit := strings.Split(mysql_datetime, " ")
rfc3339_datetime = datesplit[0] + "T" + datesplit[1] + "Z"
godate, _ = time.Parse(time.RFC3339, datetime)
Max
  • 39
  • 3