Attempt to figure out date if not given for TIME

master
Bel LaPointe 2020-02-17 15:09:33 -07:00
parent 8b2b095c10
commit c77264f85a
1 changed files with 18 additions and 0 deletions

18
arg.go
View File

@ -2,6 +2,7 @@ package args
import ( import (
"fmt" "fmt"
"math"
"strconv" "strconv"
"strings" "strings"
"time" "time"
@ -168,6 +169,10 @@ func (a *Arg) Set(value interface{}) error {
"06-01-02", "06-01-02",
"06/01/02", "06/01/02",
"2006-01-02 15:04:05.999999999 -0700 MST", "2006-01-02 15:04:05.999999999 -0700 MST",
"15:04:05.999999999 -0700 MST",
"15:04:05 -0700 MST",
"15:04 -0700 MST",
"15:04",
} }
for k := range layouts { for k := range layouts {
if j, err = time.Parse(layouts[k], s); err == nil { if j, err = time.Parse(layouts[k], s); err == nil {
@ -176,6 +181,19 @@ func (a *Arg) Set(value interface{}) error {
break break
} }
} }
if err == nil && s != "" {
j, _ := a.Value.(time.Time)
if j.Year() < 1980 {
n := time.Now()
j = j.AddDate(n.Year()-j.Year(), int(n.Month()-j.Month()), n.Day()-j.Day())
j = j.In(time.Local)
t := time.Date(2000, 02, 02, 12, 02, 02, 02, time.UTC)
l := t.Local()
tzDiff := int(math.Abs(float64(t.Hour() - l.Hour())))
j = j.Add(time.Hour * time.Duration(tzDiff))
}
a.Value = j
}
} }
case FLOAT: case FLOAT:
if i, ok := value.(float32); ok { if i, ok := value.(float32); ok {