eliotools/tools/tools.go

42 lines
1.2 KiB
Go

package main
import (
"errors"
"os"
"regexp"
"time"
)
func IsFilenameValid(filename string) bool {
pattern := `^20[2-9][0-9]-(0[1-9]|1[0-2])-(0[0-9]|1[0-9]|2[0-9]|3[0-1])T(0[0-9]|1[0-9]|2[0-3]):([0-5][0-9]:[0-5][0-9])((\+00:00)|(\-00:00)|(\-0[3,9]:30)|(\-1[0-2]:00)|(-0[0-9]:00)|(\+0[0-9]:00)|(\+0[09,03,04,05,06]:30)|(\+10:30)|(\+1[0-4]:00)|(\+0[5,8]:45)|(\+12:45))-(CH|DE|EU|AT)-(stock|delivery).csv$`
re := regexp.MustCompile(pattern)
return re.MatchString(filename)
}
func CheckDir(path string) (bool, error) {
info, err := os.Stat(path)
if err == nil {
return info.IsDir(), nil
}
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
func RFC3339StringToTime(rfc3339str string) (time.Time, error) {
const MINUS12STUNDEN = -43200
const PLUS24STUNDEN = 50400
t, err := time.Parse("2006-01-02T15:04:05-07:00", rfc3339str)
if err != nil {
happybirthday := time.Date(1981, time.March, 1, 4, 15, 0, 0, time.UTC)
return happybirthday, err
}
_, offset := t.Zone()
if offset < MINUS12STUNDEN || offset > PLUS24STUNDEN {
return time.Date(1981, time.March, 1, 4, 15, 0, 0, time.UTC), errors.New("timezone offset must be between -12:00 and +14:0")
}
return t, nil
}