when icon indicating copy to clipboard operation
when copied to clipboard

Common US dates not working.

Open JamesHaskell opened this issue 7 years ago • 2 comments

Not getting a date back for standard U.S. format: "10/20" or "10/20/17". Getting a panic on "10/20/2017":

package main

import (
	"fmt"
	"os"
	"time"

	"github.com/olebedev/when"
	"github.com/olebedev/when/rules/common"
	"github.com/olebedev/when/rules/en"
)

func main() {
	w := when.New(nil)
	w.Add(en.All...)
	w.Add(common.All...)

	text := "10/20/2017"
	r, err := w.Parse(text, time.Now())
	if err != nil {
		fmt.Printf("Error: %s\n", err)
		os.Exit(1)
	}
	if r == nil {
		fmt.Println("NO MATCH")
		os.Exit(1)
	}

	fmt.Println(
		"the time",
		r.Time.String(),
		"mentioned in",
		text[r.Index:r.Index+len(r.Text)],
	)
}

JamesHaskell avatar Nov 04 '17 20:11 JamesHaskell

Hi @JamesHaskell, thanks for the report! I will dig into it soon. It seems we have to add yet another rule for US date format.

olebedev avatar Nov 06 '17 07:11 olebedev

@olebedev, as a solution you can just pass everything that looks like a date ((\d\s\:\.\-)) into dateparse. Let's do OSS "Stand on the shoulders of giants" :)

orsinium avatar Feb 27 '19 09:02 orsinium

The panic situation is resolved by #37. A wider discussion around parsing of the US data format is happening here #19.

olebedev avatar May 31 '23 22:05 olebedev