Added *mo time support

This commit is contained in:
Trial97
2020-06-17 10:50:18 +03:00
committed by Dan Christian Bogos
parent 8b81b6b8e4
commit cfc88400a7
3 changed files with 42 additions and 9 deletions

View File

@@ -176,6 +176,15 @@ func Round(x float64, prec int, method string) float64 {
return rounder / pow
}
func getAddDuration(tmStr string) (addDur time.Duration, err error) {
eDurIdx := strings.Index(tmStr, "+")
if eDurIdx == -1 {
return
}
return time.ParseDuration(tmStr[eDurIdx+1:])
}
// ParseTimeDetectLayout returns the time from string
func ParseTimeDetectLayout(tmStr string, timezone string) (time.Time, error) {
tmStr = strings.TrimSpace(tmStr)
var nilTime time.Time
@@ -197,14 +206,18 @@ func ParseTimeDetectLayout(tmStr string, timezone string) (time.Time, error) {
return time.Now().AddDate(1, 0, 0), nil // add one year
case strings.HasPrefix(tmStr, "*month_end"):
expDate := GetEndOfMonth(time.Now())
if eDurIdx := strings.Index(tmStr, "+"); eDurIdx != -1 {
var extraDur time.Duration
if extraDur, err = time.ParseDuration(tmStr[eDurIdx+1:]); err != nil {
return nilTime, err
}
expDate = expDate.Add(extraDur)
extraDur, err := getAddDuration(tmStr)
if err != nil {
return nilTime, err
}
expDate = expDate.Add(extraDur)
return expDate, nil
case strings.HasPrefix(tmStr, "*mo"): // add one month and extra duration
extraDur, err := getAddDuration(tmStr)
if err != nil {
return nilTime, err
}
return time.Now().AddDate(0, 1, 0).Add(extraDur), nil
case astTimestamp.MatchString(tmStr):
return time.Parse("2006-01-02T15:04:05.999999999-0700", tmStr)
case rfc3339Rule.MatchString(tmStr):
@@ -265,7 +278,7 @@ func ParseTimeDetectLayout(tmStr string, timezone string) (time.Time, error) {
return nilTime, errors.New("Unsupported time format")
}
// returns a number equal or larger than the amount that exactly
// RoundDuration returns a number equal or larger than the amount that exactly
// is divisible to whole
func RoundDuration(whole, amount time.Duration) time.Duration {
a, w := float64(amount), float64(whole)

View File

@@ -391,6 +391,18 @@ func TestParseTimeDetectLayout(t *testing.T) {
} else if expected.Sub(date).Seconds() > 1 {
t.Errorf("received: %+v", date)
}
expected = time.Now().AddDate(0, 1, 0)
if date, err := ParseTimeDetectLayout("*mo", ""); err != nil {
t.Error(err)
} else if expected.Sub(date).Seconds() > 1 {
t.Errorf("received: %+v", date)
}
expected = time.Now().AddDate(0, 1, 0).Add(time.Hour + 2*time.Minute)
if date, err := ParseTimeDetectLayout("*mo+1h2m", ""); err != nil {
t.Error(err)
} else if expected.Sub(date).Seconds() > 1 {
t.Errorf("received: %+v", date)
}
expected = time.Now().AddDate(1, 0, 0)
if date, err := ParseTimeDetectLayout("*yearly", ""); err != nil {
t.Error(err)
@@ -416,6 +428,12 @@ func TestParseTimeDetectLayout(t *testing.T) {
t.Errorf("Expecting nilTime, received: %+v", date)
}
if date, err := ParseTimeDetectLayout("*mo+xyz", ""); err == nil {
t.Error("Expecting error 'time: invalid time duration', received: nil")
} else if date != nilTime {
t.Errorf("Expecting nilTime, received: %+v", date)
}
date, err = ParseTimeDetectLayout("2013-07-30T19:33:10Z", "")
expected = time.Date(2013, 7, 30, 19, 33, 10, 0, time.UTC)
if err != nil || !date.Equal(expected) {