In my program, I am parsing/decoding XML files and storing values in structs, using the Decoder.Decode()
function. The files contain several malformed UTC dates, intended to be parsed as time.Time
values and stored in the database.
The two types of dates in the XML content are –
- Correctly formatted –
"2015-03-12T20:05:18+05:30"
- Incorrect formatting –
"2020-06-29T11:11:04"
The incorrect dates are missing the timezone offset (that is present in the correctly formatted dates) or the "Z"
at the end, indicating 00:00
.
During parsing, the decoder fails to parse the malformed dates, and throws an error, complaining about the missing offset.
Before storing the parsed Time
values in the database, I am using .format()
on them to format it in the layout of 2006-01-02 15:04:05 +0000
(basically storing without the offset). This .format()
function also fails, throwing a similar error –
error: parsing time "2020-06-29T11:11:04" as "2006-01-02T15:04:05Z07:00": cannot parse "" as "Z07:00"
If I could somehow get an exact string representation of these malformed dates, I could append a “Z” at the end, and then parse it, which solves the issue. Here is that code on the playground – https://go.dev/play/p/IbwGgnNtw6P
However, using the in-built String()
function does not work, as it either throws the same error or returns a string with an incorrect offset added twice, as described here – Golang time – time zone showing twice
How do I parse the incorrectly formatted dates?