I want to parse the following string to a date in go:
"This item will be released on March 9, 2014."
I followed this and came up whith:
func findReleaseDateString(raw string) time.Time {
test, err := time.Parse("This item will be released on January 2, 2006.", raw)
if err != nil {
panic(err)
}
return test
}
Which works like a charm for english strings.
My problem: I would like to parse german strings. Like:
"Dieser Artikel wird am 9. März 2014 erscheinen."
I am aware, that I could match day, month and year via a regex and then parse it. But is there any possibility to tell time.Parse to use a different set of constants for month?
As you can see from the sourcecode of format.go from package time, the names of days/months are defined as variables - (long|short)(Day|Month)Names. Unfortunately, the names are lowercase, which means they are not exported. If they were, you could modify them before calling time.Parse(). Maybe you should file a bug to make these variables public or provide a way to modify them, or do it yourself and submit a patch. You can also, of course, create a private copy of the time package and make the needed changes there.
Edit: the wrapper package suggested by ANisus solves the problem in a more pragmatic way than my suggestions. Still, my answer shows how you can use the fact that Go is open source to investigate possible solutions to issues in the standard library yourself.
There is currently no i18n support for the time package. While waiting for that to happen, you can try using a wrapper package such as:
As stated by
monday
's documentation:Here is your example using
monday
:Output: