Improved date handling

This commit is contained in:
Stash Dev
2019-03-04 17:14:52 -08:00
parent 9aba952dbe
commit b70d5f33d2
9 changed files with 93 additions and 18 deletions

View File

@@ -1,9 +1,47 @@
package utils
import "time"
import (
"fmt"
"time"
)
const railsTimeLayout = "2006-01-02 15:04:05 MST"
func GetYMDFromDatabaseDate(dateString string) string {
t, _ := time.Parse(time.RFC3339, dateString)
// https://stackoverflow.com/a/20234207 WTF?
return t.Format("2006-01-02")
result, _ := ParseDateStringAsFormat(dateString, "2006-01-02")
return result
}
func ParseDateStringAsFormat(dateString string, format string) (string, error) {
t, e := ParseDateStringAsTime(dateString)
if t != nil {
return t.Format(format), e
}
return "", fmt.Errorf("ParseDateStringAsFormat failed: dateString <%s>, format <%s>", dateString, format)
}
func ParseDateStringAsTime(dateString string) (*time.Time, error) {
// https://stackoverflow.com/a/20234207 WTF?
t, e := time.Parse(time.RFC3339, dateString)
if e == nil {
return &t, nil
}
t, e = time.Parse("2006-01-02", dateString)
if e == nil {
return &t, nil
}
t, e = time.Parse("2006-01-02 15:04:05", dateString)
if e == nil {
return &t, nil
}
t, e = time.Parse(railsTimeLayout, dateString)
if e == nil {
return &t, nil
}
return nil, fmt.Errorf("ParseDateStringAsTime failed: dateString <%s>", dateString)
}