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 /time.go | |
| parent | a32b88646c02fd785687cd2a498b56410e62b2cd (diff) | |
Standard Time(). Perfect place for this. I am pleased.
Diffstat (limited to 'time.go')
| -rw-r--r-- | time.go | 99 |
1 files changed, 45 insertions, 54 deletions
@@ -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 } |
