0

I have a string from db , say

dbString := "2020-03-16 14:46:13 +0530 IST"

My requirement is to insert this string as Timestamptz into another table

I am trying to convert dbString into Time

timeToBeInserted := time.Parse(time.RFC3339,t.VO.DateLastModified)

I see the below error

+0000 UTC parsing time "2020-03-16 14:46:13 +0530 IST" as "2006-01-02T15:04:05Z07:00": cannot parse " 14:46:13 +0530 IST" as "T"
Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189

2 Answers2

7

Your date string does not match RFC3339 format:

RFC3339     = "2006-01-02T15:04:05Z07:00"

You should use a custom format. The following one works with your string:

dbString := "2020-03-16 14:46:13 +0530 IST"
fmt.Println(time.Parse("2006-01-02 15:04:05 -0700 MST", dbString))


// Output:
// 2020-03-16 14:46:13 +0530 IST <nil>
Yury Fedorov
  • 14,508
  • 6
  • 50
  • 66
3

Your time string isn't in RFC3339 format, so don't tell time.Parse that it is. Instead use

time.Parse("2006-01-02 15:04:05 -0700 MST", t.VO.DateLastModified)
hobbs
  • 223,387
  • 19
  • 210
  • 288