summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeff Carr <[email protected]>2025-10-11 09:25:56 -0500
committerJeff Carr <[email protected]>2025-10-11 09:50:17 -0500
commita32b88646c02fd785687cd2a498b56410e62b2cd (patch)
tree528d711fd27831613df1e5e979e653df5ee6ed15
parent63e3a8382583af1d96da91295616df66fcf25dc4 (diff)
pretty good 5 char duration.v0.0.18
-rw-r--r--formatDuration.go38
-rw-r--r--since.go21
-rw-r--r--time.go28
3 files changed, 54 insertions, 33 deletions
diff --git a/formatDuration.go b/formatDuration.go
index f945325..f9cd547 100644
--- a/formatDuration.go
+++ b/formatDuration.go
@@ -5,41 +5,51 @@ import (
"time"
)
+// this math isn't perfect but I don't care right now
+// use time.
func FormatDuration(d time.Duration) string {
result := ""
// check if it's more than a year
years := int(d.Hours()) / (24 * 365)
if years > 0 {
- result += fmt.Sprintf("%dy", years)
+ result += fmt.Sprintf("%3d Y", years)
return result
}
- // check if it's more than a day
days := int(d.Hours()) / 24
+ /* was terrible
+ // check if it's more than a day
+ if days > 45 {
+ months := int(d.Hours()) % 12
+ result += fmt.Sprintf("%2d M", months)
+ return result
+ }
+ */
+
if days > 0 {
- result += fmt.Sprintf("%dd", days)
+ result += fmt.Sprintf("%3d d", days)
return result
}
// check if it's more than an hour
hours := int(d.Hours()) % 24
- if hours > 0 {
- result += fmt.Sprintf("%dh", hours)
+ if hours > 2 {
+ result += fmt.Sprintf("%3d h", hours)
return result
}
// check if it's more than a minute
minutes := int(d.Minutes()) % 60
- if minutes > 0 {
- result += fmt.Sprintf("%dm", minutes)
+ if minutes > 2 {
+ result += fmt.Sprintf("%3d m", minutes)
return result
}
// check if it's more than a second
seconds := int(d.Seconds()) % 60
if seconds > 0 {
- result += fmt.Sprintf("%ds", seconds)
+ result += fmt.Sprintf("%3d s", seconds)
return result
}
@@ -49,18 +59,24 @@ func FormatDuration(d time.Duration) string {
// todo: print .3s, etc ?
}
if ms > 0 {
- result += fmt.Sprintf("%dms", ms)
+ /*
+ if ms > 99 {
+ result += fmt.Sprintf("%-3dm", ms)
+ return result
+ }
+ */
+ result += fmt.Sprintf("%3dms", ms)
return result
}
// report in milliseconds
mc := int(d.Microseconds())
if mc > 0 {
- result += fmt.Sprintf("%dmc", mc)
+ result += fmt.Sprintf("%3dmc", mc)
return result
}
ns := int(d.Nanoseconds())
- result += fmt.Sprintf("%dns", ns)
+ result += fmt.Sprintf("%3dns", ns)
return result
}
diff --git a/since.go b/since.go
index 22c150e..48f8575 100644
--- a/since.go
+++ b/since.go
@@ -7,27 +7,6 @@ import (
"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)
diff --git a/time.go b/time.go
index 68ce878..df8ed63 100644
--- a/time.go
+++ b/time.go
@@ -15,13 +15,39 @@ import "time"
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:
+ // 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()