I am porting code from python and have a function that takes a formatting string and equivalent datetime string and creates a datetime object:
import datetime
def retrieve_object(file_name, fmt_string):
datetime = datetime.strptime(file_name, fmt_string)
// Do additional datetime calculations here
I tried creating the equivalent function in Go:
import(
"time"
)
func retrieve_object(file_name string, fmt_string string) {
time_out, _ := time.Parse(fmt_string, file_name)
// Do additional time.Time calculations here
This parses the time.Time correctly in this case:
file_name := "KICT20170307_000422"
fmt_string := "KICT20060102_150405"
// returns 2017-03-07 00:04:22 +0000 UTC
But fails to correctly parse in this case:
file_name := "KICT20170307_000422_V06.nc"
fmt_string := "KICT20060102_150405_V06.nc"
// returns 2006-03-07 00:04:22 +0000 UTC
I suspect this is due to the additional non-date number ("06") in the datetime string. Is it possible to create a function that can create a time.Time object given an arbitrary formatting string and datetime string representation using the built-in time.Parse function? If not, are there any third-party solutions that could work?