From 102a280dda84e29d428bee275fac688f646351cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20Schw=C3=B6rer?= Date: Sun, 10 Mar 2024 15:25:30 +0100 Subject: [PATCH] v0.0.404 --- goextVersion.go | 4 ++-- rfctime/date.go | 43 ++++++++++++++++++++++++++++++++++++++++-- sq/converterDefault.go | 24 +++++------------------ 3 files changed, 48 insertions(+), 23 deletions(-) diff --git a/goextVersion.go b/goextVersion.go index 414950d..25dc374 100644 --- a/goextVersion.go +++ b/goextVersion.go @@ -1,5 +1,5 @@ package goext -const GoextVersion = "0.0.403" +const GoextVersion = "0.0.404" -const GoextVersionTimestamp = "2024-03-10T12:58:59+0100" +const GoextVersionTimestamp = "2024-03-10T15:25:30+0100" diff --git a/rfctime/date.go b/rfctime/date.go index 38f9ef3..86444b3 100644 --- a/rfctime/date.go +++ b/rfctime/date.go @@ -9,6 +9,8 @@ import ( "go.mongodb.org/mongo-driver/bson/bsonrw" "go.mongodb.org/mongo-driver/bson/bsontype" "reflect" + "strconv" + "strings" "time" ) @@ -212,11 +214,48 @@ func (t Date) Format(layout string) string { } func (t Date) GoString() string { - return t.TimeUTC().GoString() + return fmt.Sprintf("rfctime.Date{Year: %d, Month: %d, Day: %d}", t.Year, t.Month, t.Day) } func (t Date) String() string { - return t.TimeUTC().String() + return fmt.Sprintf("%04d-%02d-%02d", t.Year, t.Month, t.Day) +} + +func (t *Date) ParseString(v string) error { + split := strings.Split(v, "-") + if len(split) != 3 { + return errors.New("invalid date format: " + v) + } + year, err := strconv.ParseInt(split[0], 10, 32) + if err != nil { + return errors.New("invalid date format: " + v + ": " + err.Error()) + } + month, err := strconv.ParseInt(split[0], 10, 32) + if err != nil { + return errors.New("invalid date format: " + v + ": " + err.Error()) + } + day, err := strconv.ParseInt(split[0], 10, 32) + if err != nil { + return errors.New("invalid date format: " + v + ": " + err.Error()) + } + + if year < 0 { + return errors.New("invalid date format: " + v + ": year is negative") + } + + if month < 1 || month > 12 { + return errors.New("invalid date format: " + v + ": month is out of range") + } + + if day < 1 || day > 31 { + return errors.New("invalid date format: " + v + ": day is out of range") + } + + t.Year = int(year) + t.Month = int(month) + t.Day = int(day) + + return nil } func NewDate(t time.Time) Date { diff --git a/sq/converterDefault.go b/sq/converterDefault.go index 4d7bd7e..e791a0a 100644 --- a/sq/converterDefault.go +++ b/sq/converterDefault.go @@ -7,8 +7,6 @@ import ( "gogs.mikescher.com/BlackForestBytes/goext/langext" "gogs.mikescher.com/BlackForestBytes/goext/rfctime" "gogs.mikescher.com/BlackForestBytes/goext/timeext" - "strconv" - "strings" "time" ) @@ -79,24 +77,12 @@ var ConverterRFC339NanoTimeToString = NewDBTypeConverter[rfctime.RFC3339NanoTime var ConverterRFCDateToString = NewDBTypeConverter[rfctime.Date, string](func(v rfctime.Date) (string, error) { return fmt.Sprintf("%04d-%02d-%02d", v.Year, v.Month, v.Day), nil }, func(v string) (rfctime.Date, error) { - split := strings.Split(v, "-") - if len(split) != 3 { - return rfctime.Date{}, errors.New("invalid date format: " + v) + d := rfctime.Date{} + if err := d.ParseString(v); err != nil { + return rfctime.Date{}, err + } else { + return d, nil } - year, err := strconv.ParseInt(split[0], 10, 32) - if err != nil { - return rfctime.Date{}, errors.New("invalid date format: " + v + ": " + err.Error()) - } - month, err := strconv.ParseInt(split[0], 10, 32) - if err != nil { - return rfctime.Date{}, errors.New("invalid date format: " + v + ": " + err.Error()) - } - day, err := strconv.ParseInt(split[0], 10, 32) - if err != nil { - return rfctime.Date{}, errors.New("invalid date format: " + v + ": " + err.Error()) - } - - return rfctime.Date{Year: int(year), Month: int(month), Day: int(day)}, nil }) var ConverterRFCTimeToString = NewDBTypeConverter[rfctime.Time, string](func(v rfctime.Time) (string, error) {