Add configuration and CLI foundation
This commit is contained in:
49
internal/timeutil/parse.go
Normal file
49
internal/timeutil/parse.go
Normal file
@@ -0,0 +1,49 @@
|
||||
package timeutil
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
const DateLayout = "2006-01-02"
|
||||
const LocalDateTimeLayout = "2006-01-02T15:04"
|
||||
|
||||
func LoadLocation(name string) (*time.Location, error) {
|
||||
location, err := time.LoadLocation(name)
|
||||
if err == nil {
|
||||
return location, nil
|
||||
}
|
||||
if strings.Contains(name, "/") {
|
||||
return nil, fmt.Errorf("load timezone %q: %w", name, err)
|
||||
}
|
||||
location, chicagoErr := time.LoadLocation("America/" + name)
|
||||
if chicagoErr == nil {
|
||||
return location, nil
|
||||
}
|
||||
return nil, fmt.Errorf("load timezone %q: %w", name, err)
|
||||
}
|
||||
|
||||
func LocalDate(now time.Time, location *time.Location) time.Time {
|
||||
local := now.In(location)
|
||||
return time.Date(local.Year(), local.Month(), local.Day(), 0, 0, 0, 0, location)
|
||||
}
|
||||
|
||||
func ParseLocalDate(value string, location *time.Location) (time.Time, error) {
|
||||
parsed, err := time.ParseInLocation(DateLayout, value, location)
|
||||
if err != nil {
|
||||
return time.Time{}, fmt.Errorf("parse date %q as YYYY-MM-DD: %w", value, err)
|
||||
}
|
||||
return parsed, nil
|
||||
}
|
||||
|
||||
func ParseStormTime(value string, location *time.Location) (time.Time, error) {
|
||||
if parsed, err := time.Parse(time.RFC3339, value); err == nil {
|
||||
return parsed, nil
|
||||
}
|
||||
parsed, err := time.ParseInLocation(LocalDateTimeLayout, value, location)
|
||||
if err != nil {
|
||||
return time.Time{}, fmt.Errorf("parse storm time %q as YYYY-MM-DDTHH:MM or RFC3339: %w", value, err)
|
||||
}
|
||||
return parsed, nil
|
||||
}
|
||||
Reference in New Issue
Block a user