summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--errors.go1
-rw-r--r--since.go23
-rw-r--r--time.go31
3 files changed, 26 insertions, 29 deletions
diff --git a/errors.go b/errors.go
index 49126f7..0e07ec5 100644
--- a/errors.go
+++ b/errors.go
@@ -5,6 +5,7 @@ import "errors"
// try out standard generic errors
var NoTime error = errors.New("could not find a time")
+var NoDuration error = errors.New("could not find a duration")
var NoBytes error = errors.New("could not find bytes")
var IsNil error = errors.New("was sent nil")
var IsBlank error = errors.New("var was blank")
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
}
diff --git a/time.go b/time.go
index a1e7828..fb2eeb7 100644
--- a/time.go
+++ b/time.go
@@ -9,24 +9,23 @@ import (
)
// you will be happier if you just use this everywhere
+// This is always 22 chars
func Time(someTimeAgoOrLaterNotsure any) string {
guess, t, err := TimeCheck(someTimeAgoOrLaterNotsure)
+ // this should probably be done first
+ if t != nil {
+ return FormatTime(*t)
+ }
+ if len(guess) > 0 {
+ return fmt.Sprintf("%-22.22s", guess)
+ }
if errors.Is(err, Broken) {
- if len(guess) > 0 {
- return fmt.Sprintf("%-15s", guess)
- }
- return "bad"
+ return fmt.Sprintf("%-22.22s", "cobol.Time() Broken")
}
if errors.Is(err, NoTime) {
- if len(guess) > 0 {
- return fmt.Sprintf("%-15s", guess)
- }
- return "nope"
+ return fmt.Sprintf("%-22.22s", "cobol.Time() NoTime")
}
- if len(guess) > 0 {
- return fmt.Sprintf("%-15s", guess)
- }
- return FormatTime(*t)
+ return fmt.Sprintf("%-22.22s", " / / : : (notsure)")
}
func isUTC(t time.Time) bool {
@@ -50,13 +49,9 @@ func TimeLocal(someTimeAgoOrLaterNotsure any) string {
return FormatTimeLocal(*t)
}
-func GetTime(mightBeTimeMightNotBeTime any) (time.Time, error) {
+func GetTime(mightBeTimeMightNotBeTime any) (*time.Time, error) {
_, t, err := TimeCheck(mightBeTimeMightNotBeTime)
- if t == nil {
- var emptyTime time.Time
- return emptyTime, err
- }
- return *t, err
+ return t, err
}
func TimeCheck(maybeTime any) (string, *time.Time, error) {