diff options
| -rw-r--r-- | time.go | 26 | ||||
| -rw-r--r-- | time.string.go | 11 |
2 files changed, 23 insertions, 14 deletions
@@ -26,7 +26,7 @@ func Time(someTimeAgoOrLaterNotsure any) string { if len(guess) > 0 { return fmt.Sprintf("%-15s", guess) } - return FormatTime(t) + return FormatTime(*t) } func TimeLocal(someTimeAgoOrLaterNotsure any) string { @@ -43,26 +43,34 @@ func TimeLocal(someTimeAgoOrLaterNotsure any) string { } return "nope" } - return FormatTimeLocal(t) + return FormatTimeLocal(*t) } func GetTime(mightBeTimeMightNotBeTime any) (time.Time, error) { _, t, err := TimeCheck(mightBeTimeMightNotBeTime) - return t, err + if t == nil { + var emptyTime time.Time + return emptyTime, err + } + return *t, err } -func TimeCheck(maybeTime any) (string, time.Time, error) { +func TimeCheck(maybeTime any) (string, *time.Time, error) { var guess string - var t time.Time + var t *time.Time 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 + // t = v + return FormatTime(v), &v, err case string: // The type is string, so 'v' is a string variable. t, err = doTimeString(v) + if t != nil { + return "", t, err + } if err != nil { guess = v } @@ -71,7 +79,8 @@ func TimeCheck(maybeTime any) (string, time.Time, error) { // '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() + newt := v.AsTime() + return FormatTime(newt), &newt, nil } else { err = errors.New("pb time invalid") } @@ -79,7 +88,8 @@ func TimeCheck(maybeTime any) (string, time.Time, error) { case timestamppb.Timestamp: // Handle the less common case of a value type instead of a pointer. if v.IsValid() { - t = v.AsTime() + newt := v.AsTime() + return FormatTime(newt), &newt, nil } default: err = errors.Join(err, NoTime) diff --git a/time.string.go b/time.string.go index 5dae2c0..595fb4f 100644 --- a/time.string.go +++ b/time.string.go @@ -7,21 +7,20 @@ import ( "time" ) -func doTimeString(BUILDTIME string) (time.Time, error) { - var t time.Time +func doTimeString(BUILDTIME string) (*time.Time, error) { parts := strings.Split(BUILDTIME, ".") if len(parts) == 1 { // The input epoch seconds // epochSeconds := int64(1758646486) num, err := strconv.Atoi(BUILDTIME) if err != nil { - return t, err + return nil, err } epochSeconds := int64(num) // Convert the epoch seconds to a time.Time object. // time.Unix() creates the time in the UTC timezone by default. - t = time.Unix(epochSeconds, 0) - return t, errors.New("treated string as seconds") + t := time.Unix(epochSeconds, 0) + return &t, errors.New("treated string as seconds") } - return t, NewFeature + return nil, NewFeature } |
