package cobol import ( "errors" "time" "google.golang.org/protobuf/types/known/timestamppb" ) // you will be happier if you just use this everywhere func Time(someTimeAgoOrLaterNotsure any) string { t, err := TimeCheck(someTimeAgoOrLaterNotsure) if errors.Is(err, Broken) { return "bad" } if errors.Is(err, NoTime) { return "nope" } return FormatTime(t) } func TimeLocal(someTimeAgoOrLaterNotsure any) string { t, err := TimeCheck(someTimeAgoOrLaterNotsure) if errors.Is(err, Broken) { return "bad" } if errors.Is(err, NoTime) { return "nope" } return FormatTimeLocal(t) } func GetTime(mightBeTimeMightNotBeTime any) (time.Time, error) { t, err := TimeCheck(mightBeTimeMightNotBeTime) return t, err } func TimeCheck(maybeTime any) (time.Time, error) { 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 case string: // The type is string, so 'v' is a string variable. t, err = doTimeString(v) 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() } else { err = errors.New("pb time invalid") } case timestamppb.Timestamp: // Handle the less common case of a value type instead of a pointer. if v.IsValid() { t = v.AsTime() } default: err = errors.Join(err, NoTime) } return t, err }