diff --git a/go.mod b/go.mod index 4a1cfc0..8a95f3c 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 3e83146..c6a3e1f 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/goextVersion.go b/goextVersion.go index 50af877..36cdd94 100644 --- a/goextVersion.go +++ b/goextVersion.go @@ -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" diff --git a/rfctime/time.go b/rfctime/time.go new file mode 100644 index 0000000..1b0e1b4 --- /dev/null +++ b/rfctime/time.go @@ -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) +} diff --git a/sq/converter.go b/sq/converter.go index 942948a..1f3a1d3 100644 --- a/sq/converter.go +++ b/sq/converter.go @@ -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 { diff --git a/sq/database.go b/sq/database.go index e562dc8..b9b9213 100644 --- a/sq/database.go +++ b/sq/database.go @@ -154,4 +154,6 @@ func (db *database) RegisterDefaultConverter() { db.RegisterConverter(ConverterExErrCategoryToString) db.RegisterConverter(ConverterExErrSeverityToString) db.RegisterConverter(ConverterExErrTypeToString) + db.RegisterConverter(ConverterRFCDateToString) + db.RegisterConverter(ConverterRFCTimeToString) }