summaryrefslogtreecommitdiff
path: root/since.go
diff options
context:
space:
mode:
authorJeff Carr <[email protected]>2025-10-14 22:00:23 -0500
committerJeff Carr <[email protected]>2025-10-14 22:00:23 -0500
commit2b2943425d69b2dfdb8c1f7af4a6fd2707328918 (patch)
treeaa099a0fe0daed46a600518b31f2979e6f327923 /since.go
parente6fa4d421e9f253715da425e9953e17a5e47f48b (diff)
only use time pointers. it's much superior here
Diffstat (limited to 'since.go')
-rw-r--r--since.go23
1 files changed, 12 insertions, 11 deletions
diff --git a/since.go b/since.go
index 3be2e5a..22d2a75 100644
--- a/since.go
+++ b/since.go
@@ -23,23 +23,27 @@ func Since(aLongTimeAgo any) string {
// also returns errors
func SinceCheck(mightBeRecently any) (string, error) {
dur, err := GetSince(mightBeRecently)
- return FormatDuration(dur), err
+ if dur == nil {
+ return FormatDuration(time.Second), NoDuration
+ }
+ return FormatDuration(*dur), err
}
-func GetSince(maybeTime any) (time.Duration, error) {
- var t time.Time
+func GetSince(maybeTime any) (*time.Duration, error) {
var err error
switch v := maybeTime.(type) {
case time.Time:
// If the type is time.Time, 'v' is now a time.Time variable.
- t = v
+ dur := time.Since(v)
+ return &dur, nil
case *timestamppb.Timestamp:
// If it's a protobuf Timestamp pointer (most common case),
// 'v' is a *timestamppb.Timestamp. We must convert it.
// It's also good to check if it's a valid timestamp.
if v.IsValid() {
- t = v.AsTime()
+ dur := time.Since(v.AsTime())
+ return &dur, nil
} else {
err = errors.New("pb time invalid")
}
@@ -47,14 +51,11 @@ func GetSince(maybeTime any) (time.Duration, error) {
case timestamppb.Timestamp:
// Handle the less common case of a value type instead of a pointer.
if v.IsValid() {
- t = v.AsTime()
+ dur := time.Since(v.AsTime())
+ return &dur, nil
}
default:
err = errors.Join(err, NoTime)
}
- if err != nil {
- return time.Since(t), err
- }
-
- return time.Since(t), nil
+ return nil, err
}