Compare commits

...

2 Commits

Author SHA1 Message Date
0927fdc4d7 v0.0.405
All checks were successful
Build Docker and Deploy / Run goext test-suite (push) Successful in 1m36s
2024-03-10 15:28:26 +01:00
102a280dda v0.0.404
All checks were successful
Build Docker and Deploy / Run goext test-suite (push) Successful in 1m37s
2024-03-10 15:25:30 +01:00
3 changed files with 53 additions and 44 deletions

View File

@@ -1,5 +1,5 @@
package goext
const GoextVersion = "0.0.403"
const GoextVersion = "0.0.405"
const GoextVersionTimestamp = "2024-03-10T12:58:59+0100"
const GoextVersionTimestamp = "2024-03-10T15:28:26+0100"

View File

@@ -9,6 +9,8 @@ import (
"go.mongodb.org/mongo-driver/bson/bsonrw"
"go.mongodb.org/mongo-driver/bson/bsontype"
"reflect"
"strconv"
"strings"
"time"
)
@@ -65,36 +67,20 @@ func (t *Date) UnmarshalJSON(data []byte) error {
if err := json.Unmarshal(data, &str); err != nil {
return err
}
t0, err := time.Parse(t.FormatStr(), str)
if err != nil {
return err
}
t.Year = t0.Year()
t.Month = int(t0.Month())
t.Day = t0.Day()
return nil
return t.ParseString(str)
}
func (t Date) MarshalJSON() ([]byte, error) {
str := t.TimeUTC().Format(t.FormatStr())
str := t.String()
return json.Marshal(str)
}
func (t Date) MarshalText() ([]byte, error) {
b := make([]byte, 0, len(t.FormatStr()))
return t.TimeUTC().AppendFormat(b, t.FormatStr()), nil
return []byte(t.String()), nil
}
func (t *Date) UnmarshalText(data []byte) error {
var err error
v, err := time.Parse(t.FormatStr(), string(data))
if err != nil {
return err
}
t.Year = v.Year()
t.Month = int(v.Month())
t.Day = v.Day()
return nil
return t.ParseString(string(data))
}
func (t *Date) UnmarshalBSONValue(bt bsontype.Type, data []byte) error {
@@ -164,7 +150,7 @@ func (t Date) DecodeValue(dc bsoncodec.DecodeContext, vr bsonrw.ValueReader, val
}
func (t Date) Serialize() string {
return t.TimeUTC().Format(t.FormatStr())
return t.String()
}
func (t Date) FormatStr() string {
@@ -212,11 +198,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 {

View File

@@ -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) {