Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to parse month in go from non-english String with

Tags:

time

go

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?

like image 369
Benjamin Avatar asked Feb 11 '14 08:02

Benjamin


People also ask

How do you parse a date in Go?

When it comes to parsing Date strings in Go, we can use the Parse function that is provided by the time package. In Go, we don't use codes like most other languages to represent the component parts of a date/time string. Instead, Go uses the mnemonic device - standard time as a reference.

How do you parse a string to a date?

parse() The Date. parse() method parses a string representation of a date, and returns the number of milliseconds since January 1, 1970, 00:00:00 UTC or NaN if the string is unrecognized or, in some cases, contains illegal date values (e.g. 2015-02-31). Only the ISO 8601 format ( YYYY-MM-DDTHH:mm:ss.

What is parse in Go?

Number parsing in Go is about converting the numbers that are present in string form to number form. By number form, we mean that these numbers can either be converted into integers, floats, etc. The most widely used package for number parsing is the "strconv" package that Go library provides us with.


1 Answers

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:

github.com/goodsign/monday

As stated by monday's documentation:

Monday is not an alternative to standard time package. It is a temporary solution to use while the internationalization features are not ready.

That's why monday doesn't create any additional parsing algorithms, layout identifiers. It is just a wrapper for time.Format and time.ParseInLocation and uses all the same layout IDs, constants, etc.

Here is your example using monday:

package main

import (
    "fmt"
    "github.com/goodsign/monday"
    "time"
)

func findReleaseDateString(raw string) time.Time {
    loc, _ := time.LoadLocation("Europe/Berlin")
    t, err := monday.ParseInLocation("Dieser Artikel wird am 2. January 2006 erscheinen.", raw, loc, monday.LocaleDeDE)
    if err != nil {
        panic(err)
    }

    return t
}

func main() {
    t := findReleaseDateString("Dieser Artikel wird am 9. März 2014 erscheinen.")
    fmt.Println(t)
}

Output:

2014-03-09 00:00:00 +0100 CET

like image 166
ANisus Avatar answered Sep 20 '22 19:09

ANisus