diff options
| author | Jeff Carr <[email protected]> | 2025-10-12 03:32:26 -0500 |
|---|---|---|
| committer | Jeff Carr <[email protected]> | 2025-10-12 03:32:26 -0500 |
| commit | aff745937b2c936f87cc48b82d0725f662bb1e30 (patch) | |
| tree | 374d37a352bb7e90f515db79bf7b41ea316c582b | |
| parent | a32b88646c02fd785687cd2a498b56410e62b2cd (diff) | |
Standard Time(). Perfect place for this. I am pleased.
| -rw-r--r-- | formatTime.go | 15 | ||||
| -rw-r--r-- | since.go | 18 | ||||
| -rw-r--r-- | time.go | 99 | ||||
| -rw-r--r-- | time.notes.go | 55 | ||||
| -rw-r--r-- | time.string.go | 27 |
5 files changed, 150 insertions, 64 deletions
diff --git a/formatTime.go b/formatTime.go new file mode 100644 index 0000000..2dcc3c2 --- /dev/null +++ b/formatTime.go @@ -0,0 +1,15 @@ +package cobol + +import ( + "time" +) + +// there is only one way to display the time as text +// this is that way. Don't like it? Don't use COBOL +func FormatTime(t time.Time) string { + localTime := t.Local() + s := localTime.Format("2006/01/02 15:04:03") + dur := time.Since(t) + //always time, two spaces, (duration) + return s + " (" + FormatDuration(dur) + ")" +} @@ -19,13 +19,14 @@ func Since(aLongTimeAgo any) string { 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) { +func SinceCheck(mightBeRecently any) (string, error) { + dur, err := GetSince(mightBeRecently) + return FormatDuration(dur), err +} + +func GetSince(maybeTime any) (time.Duration, error) { var t time.Time var err error @@ -52,11 +53,8 @@ func SinceCheck(maybeTime any) (string, error) { err = errors.Join(err, NoTime) } if err != nil { - return "", err + return time.Since(t), err } - dur := time.Since(t) - s := FormatDuration(dur) - - return s, nil + return time.Since(t), nil } @@ -1,65 +1,56 @@ package cobol -import "time" +import ( + "errors" + "time" -/* - 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) -*/ - -/* from godoc -const ( - Layout = "01/02 03:04:05PM '06 -0700" // The reference time, in numerical order. - ANSIC = "Mon Jan _2 15:04:05 2006" - UnixDate = "Mon Jan _2 15:04:05 MST 2006" - RubyDate = "Mon Jan 02 15:04:05 -0700 2006" - RFC822 = "02 Jan 06 15:04 MST" - RFC822Z = "02 Jan 06 15:04 -0700" // RFC822 with numeric zone - RFC850 = "Monday, 02-Jan-06 15:04:05 MST" - RFC1123 = "Mon, 02 Jan 2006 15:04:05 MST" - RFC1123Z = "Mon, 02 Jan 2006 15:04:05 -0700" // RFC1123 with numeric zone - RFC3339 = "2006-01-02T15:04:05Z07:00" - RFC3339Nano = "2006-01-02T15:04:05.999999999Z07:00" - Kitchen = "3:04PM" - // Handy time stamps. - Stamp = "Jan _2 15:04:05" - StampMilli = "Jan _2 15:04:05.000" - StampMicro = "Jan _2 15:04:05.000000" - StampNano = "Jan _2 15:04:05.000000000" - DateTime = "2006-01-02 15:04:05" - DateOnly = "2006-01-02" - TimeOnly = "15:04:05" + "google.golang.org/protobuf/types/known/timestamppb" ) -*/ -/* - // sample dates for reference - // whois: 1994-07-28T04:00:00Z - // git am: Date: Sat, 11 Oct 2025 08:54:40 -0500 - // linux date: Sat Oct 11 08:26:27 CDT 2025 - // /bin/ls: drwxr-xr-x 2 root root 4096 Aug 15 05:31 riscv - // ping: 64 bytes from 2604:bbc0::1: icmp_seq=1 ttl=50 time=30.4 ms - // syslog: [Fri Oct 3 05:13:40 2025] systemd[1]: Detected architecture x86-64. - - ctimef := func(p *forgepb.Set) string { - ctime := p.Ctime.AsTime() - return ctime.Format("2006/01/02 15:04") +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 Time(someTime any) string { - return "todo" +func GetTime(mightBeTimeMightNotBeTime any) (time.Time, error) { + t, err := TimeCheck(mightBeTimeMightNotBeTime) + return t, err } -func GetTime(someTime any) (time.Time, error) { - return time.Now(), NoTime +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 } diff --git a/time.notes.go b/time.notes.go new file mode 100644 index 0000000..9ead496 --- /dev/null +++ b/time.notes.go @@ -0,0 +1,55 @@ +package cobol + +/* + 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) +*/ + +/* from godoc +const ( + Layout = "01/02 03:04:05PM '06 -0700" // The reference time, in numerical order. + ANSIC = "Mon Jan _2 15:04:05 2006" + UnixDate = "Mon Jan _2 15:04:05 MST 2006" + RubyDate = "Mon Jan 02 15:04:05 -0700 2006" + RFC822 = "02 Jan 06 15:04 MST" + RFC822Z = "02 Jan 06 15:04 -0700" // RFC822 with numeric zone + RFC850 = "Monday, 02-Jan-06 15:04:05 MST" + RFC1123 = "Mon, 02 Jan 2006 15:04:05 MST" + RFC1123Z = "Mon, 02 Jan 2006 15:04:05 -0700" // RFC1123 with numeric zone + RFC3339 = "2006-01-02T15:04:05Z07:00" + RFC3339Nano = "2006-01-02T15:04:05.999999999Z07:00" + Kitchen = "3:04PM" + // Handy time stamps. + Stamp = "Jan _2 15:04:05" + StampMilli = "Jan _2 15:04:05.000" + StampMicro = "Jan _2 15:04:05.000000" + StampNano = "Jan _2 15:04:05.000000000" + DateTime = "2006-01-02 15:04:05" + DateOnly = "2006-01-02" + TimeOnly = "15:04:05" +) +*/ + +/* + // sample dates for reference + // whois: 1994-07-28T04:00:00Z + // git am: Date: Sat, 11 Oct 2025 08:54:40 -0500 + // linux date: Sat Oct 11 08:26:27 CDT 2025 + // /bin/ls: drwxr-xr-x 2 root root 4096 Aug 15 05:31 riscv + // ping: 64 bytes from 2604:bbc0::1: icmp_seq=1 ttl=50 time=30.4 ms + // syslog: [Fri Oct 3 05:13:40 2025] systemd[1]: Detected architecture x86-64. + + ctimef := func(p *forgepb.Set) string { + ctime := p.Ctime.AsTime() + return ctime.Format("2006/01/02 15:04") + } +} +*/ diff --git a/time.string.go b/time.string.go new file mode 100644 index 0000000..5dae2c0 --- /dev/null +++ b/time.string.go @@ -0,0 +1,27 @@ +package cobol + +import ( + "errors" + "strconv" + "strings" + "time" +) + +func doTimeString(BUILDTIME string) (time.Time, error) { + var t time.Time + 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 + } + 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") + } + return t, NewFeature +} |
