Quoting Lucio (2018-10-04 23:19:48)
>    On Tuesday, 2 October 2018 15:21:08 UTC+2, Eric Raymond wrote:
>
>    Is promised in the thread on pytogo, I have blogged on the general
>    topic of rule-swarm attacks in the domain of language transformation.
>    [1]http://esr.ibiblio.org/?p=8153
>
>    I'm no pragmatist, I bow to ESR for shining that light. But here's my
>    own, tiny contribution to sledge-hammering human-readable dates into
>    internal Go format:
> //      Function ParseDate() attempts all known legal date formats
>     79� � //
>     80� � //    More formats can be added as the need arises.  It would not 
> be t
> he
>     81� � //    first time.
>     82� � //
>     83� � func ParseDate(s string) (d time.Time, err error) {
>     84� �       s = strings.Trim(s, " \t")
>     85� �       if d, err = time.Parse("Mon, 02 Jan 2006 15:04:05 -0700 
> (MST)",
> s); err == nil {
>     86� �               return d, nil
>     87� �       }
>     88� �       if d, err = time.Parse("Mon, 02 Jan 2006 15:04:05 -0700", s); 
> er
> r == nil {
>     89� �               return d, nil
>     90� �       }
>     91� �       if d, err = time.Parse("Mon, 2 Jan 2006 15:04:05 -0700 
> (MST)", s
> ); err == nil {
>     92� �               return d, nil
>     93� �       }
>     94� �       if d, err = time.Parse("Mon, 2 Jan 2006 15:04:05 -0700", s); 
> err
>  == nil {
>     95� �               return d, nil
>     96� �       }
>     97� �       if d, err = time.Parse("02 Jan 2006 15:04:05 -0700", s); err 
> ==
> nil {
>     98� �               return d, nil
>     99� �       }
>    100� �       if d, err = time.Parse("2 Jan 2006 15:04:05 -0700", s); err 
> == n
> il {
>    101� �               return d, nil
>    102� �       }
>    103� �       return d, err
>    104� � }
>    105� �
>    106� �
>
>    Not pretty, I grant. This one was in the context of email headers
>    (RFC-822 and later) parsing, I just haven't read enough Go code to know
>    if there is a better way. The curious thing is that in my
>    life-before-Go, I have frequently wanted something like this, but it
>    took what I presume (perhaps unfairly) to be Rob Pike's date parsing to
>    give it shape.
>    Lucio.

The low-hanging fruit in the "pretty" department I see is:

    formats := []string{
        "Mon, 02 Jan 2006 15:04:05 -0700 (MST)",
        "Mon, 02 Jan 2006 15:04:05 -0700",
        ...
    }
    for _, format  := range formats {
        if d, err = time.Parse(format), s); err = nil {
            return d, nil
        }
    }

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to