-
Notifications
You must be signed in to change notification settings - Fork 1
/
short.go
67 lines (57 loc) · 2.01 KB
/
short.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
© 2022–present Harald Rudell <harald.rudell@gmail.com> (https://haraldrudell.github.io/haraldrudell/)
ISC License
*/
package parl
import "time"
const (
shortHour = "060102_15:04:05Z07"
shortMinute = "060102_15:04:05Z0700"
shortHourSpace = "060102 15:04:05Z07"
shortMinuteSpace = "060102 15:04:05Z0700"
offsetHourDivisor = int(time.Hour / time.Second)
msHour = "060102_15:04:05.000Z07"
msMinute = "060102_15:04:05.000Z0700"
)
// Short provides a brief time-stamp in compact second-precision including time-zone.
// - sample: 060102_15:04:05-08
// - The timestamp does not contain space.
// - time zone is what is included in tim, typically time.Local
// - if tim is not specified, time.Now() in local time zone
func Short(tim ...time.Time) (s string) {
return timeAndFormat(tim, shortHour, shortMinute)
}
// Short provides a brief time-stamp in compact second-precision including time-zone.
// - sample: 060102 15:04:05-08
// - date is 6-digit separated from time by a space
// - time zone is what is included in tim, typically time.Local
// - if tim is not specified, time.Now() in local time zone
func ShortSpace(tim ...time.Time) (s string) {
return timeAndFormat(tim, shortHourSpace, shortMinuteSpace)
}
// ShortMs provides a brief time-stamp in compact milli-second-precision including time-zone.
// - sample: 060102_15:04:05.123-08
// - The timestamp does not contain space.
// - time zone is what is included in tim, typically time.Local
// - if tim is not specified, time.Now() in local time zone
func ShortMs(tim ...time.Time) (s string) {
return timeAndFormat(tim, msHour, msMinute)
}
func timeAndFormat(tim []time.Time, hour, minute string) (s string) {
// ensure t holds a time
var t time.Time
if len(tim) > 0 {
t = tim[0]
}
if t.IsZero() {
t = time.Now()
}
// pick layout using Zone.offset
var format string
if _, offsetS := t.Zone(); offsetS%offsetHourDivisor != 0 {
format = minute
} else {
format = hour
}
return t.Format(format)
}