summaryrefslogtreecommitdiff
path: root/since.go
diff options
context:
space:
mode:
Diffstat (limited to 'since.go')
-rw-r--r--since.go83
1 files changed, 83 insertions, 0 deletions
diff --git a/since.go b/since.go
new file mode 100644
index 0000000..22c150e
--- /dev/null
+++ b/since.go
@@ -0,0 +1,83 @@
+package cobol
+
+import (
+ "errors"
+ "time"
+
+ "google.golang.org/protobuf/types/known/timestamppb"
+)
+
+/*
+ etimef := func(e *forgepb.Set) string {
+ etime := e.Etime.AsTime()
+ s := etime.Format("2006/01/02 15:04")
+ if strings.HasPrefix(s, "1970/") {
+ // just show a blank if it's not set
+ return ""
+ }
+ return s
+ }
+ t.AddStringFunc("etime", etimef)
+*/
+
+/*
+ ctimef := func(p *forgepb.Set) string {
+ ctime := p.Ctime.AsTime()
+ return ctime.Format("2006/01/02 15:04")
+ }
+}
+*/
+
+// returns a human readable duration
+func Since(aLongTimeAgo any) string {
+ s, err := SinceCheck(aLongTimeAgo)
+ if errors.Is(err, Broken) {
+ return "bad"
+ }
+ if errors.Is(err, NoTime) {
+ return "nope"
+ }
+ return s
+}
+
+func GetSince(aLongTimeAgo any) (time.Duration, error) {
+ return time.Second, NoTime
+}
+
+// returns a human readable duration
+// also returns errors
+func SinceCheck(maybeTime any) (string, 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 *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)
+ }
+ if err != nil {
+ return "", err
+ }
+
+ dur := time.Since(t)
+ s := FormatDuration(dur)
+
+ return s, nil
+}