Skip to content

Commit 7402e40

Browse files
committed
feat: text/markdown: add GeneratedAt prefix functions for markdown times
1 parent ab2869f commit 7402e40

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

text/markdown/times.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package markdown
2+
3+
import (
4+
"time"
5+
6+
"github.com/grokify/mogo/time/timeutil"
7+
)
8+
9+
func GeneratedAtLocationStringsHuman(dt time.Time, locNames []string) (string, error) {
10+
return GeneratedAtLocationStrings(timeutil.HumanDateTime, dt, locNames)
11+
}
12+
13+
func GeneratedAtLocationStrings(layout string, dt time.Time, locNames []string) (string, error) {
14+
locs := []*time.Location{}
15+
for _, locStr := range locNames {
16+
if loc, err := time.LoadLocation(locStr); err != nil {
17+
return "", err
18+
} else if loc != nil {
19+
locs = append(locs, loc)
20+
}
21+
}
22+
return GeneratedAt(layout, dt, locs), nil
23+
}
24+
25+
func GeneratedAt(layout string, dt time.Time, locs []*time.Location) string {
26+
prefix := "Generated at"
27+
if len(locs) == 0 {
28+
return prefix + " " + dt.Format(layout)
29+
} else if len(locs) == 1 {
30+
if locs[0] != nil {
31+
dt = dt.In(locs[0])
32+
}
33+
return prefix + " " + dt.Format(layout)
34+
}
35+
str := prefix + "\n"
36+
for _, loc := range locs {
37+
if loc != nil {
38+
dt = dt.In(loc)
39+
}
40+
str += "* " + dt.Format(layout) + "\n"
41+
}
42+
return str
43+
}

0 commit comments

Comments
 (0)