Compare commits
2 Commits
Author | SHA1 | Date | |
---|---|---|---|
0927fdc4d7
|
|||
102a280dda
|
@@ -1,5 +1,5 @@
|
|||||||
package goext
|
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"
|
||||||
|
@@ -9,6 +9,8 @@ import (
|
|||||||
"go.mongodb.org/mongo-driver/bson/bsonrw"
|
"go.mongodb.org/mongo-driver/bson/bsonrw"
|
||||||
"go.mongodb.org/mongo-driver/bson/bsontype"
|
"go.mongodb.org/mongo-driver/bson/bsontype"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -65,36 +67,20 @@ func (t *Date) UnmarshalJSON(data []byte) error {
|
|||||||
if err := json.Unmarshal(data, &str); err != nil {
|
if err := json.Unmarshal(data, &str); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
t0, err := time.Parse(t.FormatStr(), str)
|
return t.ParseString(str)
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
t.Year = t0.Year()
|
|
||||||
t.Month = int(t0.Month())
|
|
||||||
t.Day = t0.Day()
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Date) MarshalJSON() ([]byte, error) {
|
func (t Date) MarshalJSON() ([]byte, error) {
|
||||||
str := t.TimeUTC().Format(t.FormatStr())
|
str := t.String()
|
||||||
return json.Marshal(str)
|
return json.Marshal(str)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Date) MarshalText() ([]byte, error) {
|
func (t Date) MarshalText() ([]byte, error) {
|
||||||
b := make([]byte, 0, len(t.FormatStr()))
|
return []byte(t.String()), nil
|
||||||
return t.TimeUTC().AppendFormat(b, t.FormatStr()), nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Date) UnmarshalText(data []byte) error {
|
func (t *Date) UnmarshalText(data []byte) error {
|
||||||
var err error
|
return t.ParseString(string(data))
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Date) UnmarshalBSONValue(bt bsontype.Type, data []byte) error {
|
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 {
|
func (t Date) Serialize() string {
|
||||||
return t.TimeUTC().Format(t.FormatStr())
|
return t.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Date) FormatStr() string {
|
func (t Date) FormatStr() string {
|
||||||
@@ -212,11 +198,48 @@ func (t Date) Format(layout string) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (t Date) GoString() 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 {
|
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 {
|
func NewDate(t time.Time) Date {
|
||||||
|
@@ -7,8 +7,6 @@ import (
|
|||||||
"gogs.mikescher.com/BlackForestBytes/goext/langext"
|
"gogs.mikescher.com/BlackForestBytes/goext/langext"
|
||||||
"gogs.mikescher.com/BlackForestBytes/goext/rfctime"
|
"gogs.mikescher.com/BlackForestBytes/goext/rfctime"
|
||||||
"gogs.mikescher.com/BlackForestBytes/goext/timeext"
|
"gogs.mikescher.com/BlackForestBytes/goext/timeext"
|
||||||
"strconv"
|
|
||||||
"strings"
|
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -79,24 +77,12 @@ var ConverterRFC339NanoTimeToString = NewDBTypeConverter[rfctime.RFC3339NanoTime
|
|||||||
var ConverterRFCDateToString = NewDBTypeConverter[rfctime.Date, string](func(v rfctime.Date) (string, error) {
|
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
|
return fmt.Sprintf("%04d-%02d-%02d", v.Year, v.Month, v.Day), nil
|
||||||
}, func(v string) (rfctime.Date, error) {
|
}, func(v string) (rfctime.Date, error) {
|
||||||
split := strings.Split(v, "-")
|
d := rfctime.Date{}
|
||||||
if len(split) != 3 {
|
if err := d.ParseString(v); err != nil {
|
||||||
return rfctime.Date{}, errors.New("invalid date format: " + v)
|
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) {
|
var ConverterRFCTimeToString = NewDBTypeConverter[rfctime.Time, string](func(v rfctime.Time) (string, error) {
|
||||||
|
Reference in New Issue
Block a user