Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse Weekday string into time.Weekday

Tags:

string

time

go

I've came across a problem to convert a Day of Week string into a time.Weekday value. I couldn't find anything built into the time package.

Then I've written this simple function (that covers my needs):

var daysOfWeek = [...]string{
    "Sunday",
    "Monday",
    "Tuesday",
    "Wednesday",
    "Thursday",
    "Friday",
    "Saturday",
}

func parseWeekday(v string) (time.Weekday, error) {
    for i := range daysOfWeek {
        if daysOfWeek[i] == v {
            return time.Weekday(i), nil
        }
    }

    return time.Sunday, fmt.Errorf("invalid weekday '%s'", v)
}

Is there any other recommended or more idiomatic way to perform this conversion in Golang?

Thanks!

like image 895
cvbarros Avatar asked Sep 22 '18 11:09

cvbarros


1 Answers

Foreword: I released this utility in github.com/icza/gox, see timex.ParseWeekday().


Yes, use a map instead of an array, so lookups are faster and more straight-forward:

var daysOfWeek = map[string]time.Weekday{
    "Sunday":    time.Sunday,
    "Monday":    time.Monday,
    "Tuesday":   time.Tuesday,
    "Wednesday": time.Wednesday,
    "Thursday":  time.Thursday,
    "Friday":    time.Friday,
    "Saturday":  time.Saturday,
}

func parseWeekday(v string) (time.Weekday, error) {
    if d, ok := daysOfWeek[v]; ok {
        return d, nil
    }

    return time.Sunday, fmt.Errorf("invalid weekday '%s'", v)
}

Testing it:

fmt.Println(parseWeekday("Monday"))
fmt.Println(parseWeekday("Friday"))
fmt.Println(parseWeekday("invalid"))

Output (try it on the Go Playgorund):

Monday <nil>
Friday <nil>
Sunday invalid weekday 'invalid'

Tip:

You can even use a for loop to initialize safely the daysOfWeek map like this:

var daysOfWeek = map[string]time.Weekday{}

func init() {
    for d := time.Sunday; d <= time.Saturday; d++ {
        daysOfWeek[d.String()] = d
    }
}

Testing and output is the same. Try this one on the Go Playground.

Another nice property of this map-solution (compared to your array-solution) is that you may list additional valid values in the same map that may be parsed into time.Weekday without additional parsing code.

For example, let's also parse the 3-letter short weekday names into their time.Weekday equivalent, e.g. "Mon" to time.Monday.

This extension can be added with a simple loop:

var daysOfWeek = map[string]time.Weekday{}

func init() {
    for d := time.Sunday; d <= time.Saturday; d++ {
        name := d.String()
        daysOfWeek[name] = d
        daysOfWeek[name[:3]] = d
    }
}

Testing it:

fmt.Println(parseWeekday("Monday"))
fmt.Println(parseWeekday("Friday"))
fmt.Println(parseWeekday("Mon"))
fmt.Println(parseWeekday("Fri"))
fmt.Println(parseWeekday("invalid"))

Output (try it on the Go Playground):

Monday <nil>
Friday <nil>
Monday <nil>
Friday <nil>
Sunday invalid weekday 'invalid'

See similar question: Get integer month value from string

like image 70
icza Avatar answered Sep 21 '22 17:09

icza