mirror of
https://github.com/stashapp/stash.git
synced 2025-12-17 20:34:37 +03:00
Improved date handling
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user