v0.0.392
Some checks failed
Build Docker and Deploy / Run goext test-suite (push) Has been cancelled

This commit is contained in:
Mike Schwörer 2024-02-21 18:32:06 +01:00
parent 4b55dbaacf
commit c25da03217
Signed by: Mikescher
GPG Key ID: D3C7172E0A70F8CF
6 changed files with 182 additions and 3 deletions

2
go.mod
View File

@ -28,7 +28,7 @@ require (
github.com/golang/snappy v0.0.4 // indirect
github.com/google/uuid v1.5.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.17.6 // indirect
github.com/klauspost/compress v1.17.7 // indirect
github.com/klauspost/cpuid/v2 v2.2.7 // indirect
github.com/leodido/go-urn v1.4.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect

2
go.sum
View File

@ -63,6 +63,8 @@ github.com/klauspost/compress v1.17.4 h1:Ej5ixsIri7BrIjBkRZLTo6ghwrEtHFk7ijlczPW
github.com/klauspost/compress v1.17.4/go.mod h1:/dCuZOvVtNoHsyb+cuJD3itjs3NbnF6KH9zAO4BDxPM=
github.com/klauspost/compress v1.17.6 h1:60eq2E/jlfwQXtvZEeBUYADs+BwKBWURIY+Gj2eRGjI=
github.com/klauspost/compress v1.17.6/go.mod h1:/dCuZOvVtNoHsyb+cuJD3itjs3NbnF6KH9zAO4BDxPM=
github.com/klauspost/compress v1.17.7 h1:ehO88t2UGzQK66LMdE8tibEd1ErmzZjNEqWkjLAKQQg=
github.com/klauspost/compress v1.17.7/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw=
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
github.com/klauspost/cpuid/v2 v2.2.6 h1:ndNyv040zDGIDh8thGkXYjnFtiN02M1PVVF+JE/48xc=
github.com/klauspost/cpuid/v2 v2.2.6/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws=

View File

@ -1,5 +1,5 @@
package goext
const GoextVersion = "0.0.391"
const GoextVersion = "0.0.392"
const GoextVersionTimestamp = "2024-02-21T16:18:04+0100"
const GoextVersionTimestamp = "2024-02-21T18:32:06+0100"

139
rfctime/time.go Normal file
View File

@ -0,0 +1,139 @@
package rfctime
import (
"fmt"
"gogs.mikescher.com/BlackForestBytes/goext/langext"
"strconv"
"strings"
"time"
)
type Time struct {
Hour int
Minute int
Second int
NanoSecond int
}
func (t Time) Serialize() string {
return fmt.Sprintf("%04d:%02d:%02d.%09d", t.Hour, t.Minute, t.Second, t.NanoSecond)
}
func (t Time) SerializeShort() string {
if t.NanoSecond == 0 && t.Second == 0 {
return fmt.Sprintf("%04d:%02d", t.Hour, t.Minute)
} else if t.NanoSecond == 0 {
return fmt.Sprintf("%04d:%02d:%02d", t.Hour, t.Minute, t.Second)
} else {
return fmt.Sprintf("%04d:%02d:%02d.%09d", t.Hour, t.Minute, t.Second, t.NanoSecond)
}
}
func (t *Time) Deserialize(v string) error {
var h, m, s, ns string
split1 := strings.Split(v, ".")
if len(split1) == 2 {
split2 := strings.Split(split1[0], ":")
if len(split2) == 3 {
h = split2[0]
m = split2[1]
s = split2[2]
ns = split1[1]
} else {
return fmt.Errorf("invalid time format: %s", v)
}
} else if len(split1) == 1 {
split2 := strings.Split(split1[0], ":")
if len(split2) == 2 {
h = split2[0]
m = split2[1]
s = "0"
ns = "0"
} else if len(split2) == 3 {
h = split2[0]
m = split2[1]
s = split2[2]
ns = "0"
} else {
return fmt.Errorf("invalid time format: %s", v)
}
} else {
return fmt.Errorf("invalid time format: %s", v)
}
ns = langext.StrPadRight(ns, "0", 9)
hh, err := strconv.ParseInt(h, 10, 32)
if err != nil {
return fmt.Errorf("invalid time format: %s", v)
}
mm, err := strconv.ParseInt(m, 10, 32)
if err != nil {
return fmt.Errorf("invalid time format: %s", v)
}
ss, err := strconv.ParseInt(s, 10, 32)
if err != nil {
return fmt.Errorf("invalid time format: %s", v)
}
nss, err := strconv.ParseInt(ns, 10, 32)
if err != nil {
return fmt.Errorf("invalid time format: %s", v)
}
t.Hour = int(hh)
t.Minute = int(mm)
t.Second = int(ss)
t.NanoSecond = int(nss)
return nil
}
func (t Time) FormatStr() string {
return "15:04:05.999999999"
}
func (t Time) GoString() string {
return fmt.Sprintf("rfctime.NewTime(%d, %d, %d, %d)", t.Hour, t.Minute, t.Second, t.NanoSecond)
}
func (t Time) String() string {
return fmt.Sprintf("%04d:%02d:%02d.%09d", t.Hour, t.Minute, t.Second, t.NanoSecond)
}
func NewTime(h int, m int, s int, ns int) Time {
return Time{
Hour: h,
Minute: m,
Second: s,
NanoSecond: ns,
}
}
func NowTime(loc *time.Location) Time {
now := time.Now().In(loc)
return NewTime(now.Hour(), now.Minute(), now.Second(), now.Nanosecond())
}
func NowTimeLoc() Time {
return NowTime(time.UTC)
}
func NowTimeUTC() Time {
return NowTime(time.Local)
}

View File

@ -8,6 +8,8 @@ import (
"gogs.mikescher.com/BlackForestBytes/goext/langext"
"gogs.mikescher.com/BlackForestBytes/goext/rfctime"
"reflect"
"strconv"
"strings"
"time"
)
@ -78,6 +80,40 @@ var ConverterRFC339NanoTimeToString = NewDBTypeConverter[rfctime.RFC3339NanoTime
return rfctime.NewRFC3339Nano(t), nil
})
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)
}
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) {
return v.SerializeShort(), nil
}, func(v string) (rfctime.Time, error) {
res := rfctime.Time{}
err := res.Deserialize(v)
if err != nil {
return rfctime.Time{}, err
}
return res, nil
})
var ConverterJsonObjToString = NewDBTypeConverter[JsonObj, string](func(v JsonObj) (string, error) {
mrsh, err := json.Marshal(v)
if err != nil {

View File

@ -154,4 +154,6 @@ func (db *database) RegisterDefaultConverter() {
db.RegisterConverter(ConverterExErrCategoryToString)
db.RegisterConverter(ConverterExErrSeverityToString)
db.RegisterConverter(ConverterExErrTypeToString)
db.RegisterConverter(ConverterRFCDateToString)
db.RegisterConverter(ConverterRFCTimeToString)
}