-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathit.go
182 lines (162 loc) · 3.94 KB
/
it.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
it = internet time in beats, there are one thousand beats between midnights
usage: it [options]
options:
y = print year since last millenium
d = print date or day number since last january first
s = print seconds or subbeats (there are one hundred subbeats in one beat)
n = not to print time
f = yd = print year, date and time
ff = yds = print year, date, time and seconds/subbeats
v = print string of year and day suitable for version tagging like 20.289
vv = print string of year, day and beats suitable for version tagging like 20.289.499
vvv = print string of year, day and beats and subbeats suitable for version tagging like 20.289.499.38
history:
2020/8/31 copy from tik
20/289 (2020/10/15) add options for version printing and not printing beats but date only
20/209 add options f and ff
21/1215 -m option to show date with months and time in military 24 hour
GoFmt GoBuildNull GoBuild GoRelease GoRun
*/
package main
import (
"fmt"
"os"
"strings"
"time"
)
const (
Beat time.Duration = time.Duration(24) * time.Hour / 1000
)
var (
tzBiel *time.Location
tnow time.Time
td0, ty0 time.Time
)
func militarytimefmt(td time.Duration) string {
return fmt.Sprintf("%02d%02d", int(td/time.Hour), int((td%time.Hour)/time.Minute))
}
func secondsfmt(td time.Duration) string {
return fmt.Sprintf("%02d", int((td%time.Minute)/time.Second))
}
func beatsfmt(td time.Duration) string {
return fmt.Sprintf("%03d", int(td/Beat))
}
func subbeatsfmt(td time.Duration) string {
return fmt.Sprintf("%02d", int((td/(Beat/100))%100))
}
func dayfmt(td time.Duration) string {
day := int(td/(time.Duration(24)*time.Hour)) + 1
return fmt.Sprintf("%03d", day)
}
func monthdayfmt(td time.Duration) string {
return ty0.Add(td).Format("0102")
}
func yearfmt(t time.Time, zeropad bool) (y string) {
if zeropad {
y = fmt.Sprintf("%03d", t.Year()%1000)
} else {
y = fmt.Sprintf("%d", t.Year()%1000)
}
return y
}
func init() {
tzBiel = time.FixedZone("Biel", 60*60)
tnow = time.Now().In(tzBiel)
td0 = time.Date(tnow.Year(), tnow.Month(), tnow.Day(), 0, 0, 0, 0, tzBiel)
ty0 = time.Date(tnow.Year(), 1, 1, 0, 0, 0, 0, tzBiel)
}
func main() {
var ptime, pseconds, pmilitarytime bool
var pdate, pmonthday bool
var pyear bool
var pversion, plongversion, plonglongversion bool
ptime = true
for _, arg := range os.Args[1:] {
if strings.Contains(arg, "y") {
pyear = true
}
if strings.Contains(arg, "d") {
pdate = true
}
if strings.Contains(arg, "m") {
pmonthday = true
pmilitarytime = true
}
if strings.Contains(arg, "s") {
pseconds = true
}
if strings.Contains(arg, "n") {
ptime = false
pseconds = false
}
if strings.Contains(arg, "f") {
pyear = true
pdate = true
ptime = true
}
if strings.Count(arg, "f") > 1 {
pseconds = true
}
if strings.Contains(arg, "v") {
pversion = true
}
if strings.Count(arg, "v") > 1 {
plongversion = true
}
if strings.Count(arg, "v") > 2 {
plonglongversion = true
}
}
td := time.Since(td0)
if pversion || plongversion {
var version string
if pmonthday {
version = fmt.Sprintf("%s.%s", yearfmt(tnow, true), monthdayfmt(time.Since(ty0)))
} else {
version = fmt.Sprintf("%s.%s", yearfmt(tnow, true), dayfmt(time.Since(ty0)))
}
if plongversion {
if pmilitarytime {
version += "." + militarytimefmt(td)
} else {
version += "." + beatsfmt(td)
}
}
if plonglongversion {
if pmilitarytime {
version += "." + secondsfmt(td)
} else {
version += "." + subbeatsfmt(td)
}
}
fmt.Println(version)
return
}
var s string
if ptime {
if pmilitarytime {
s += "@" + militarytimefmt(td)
} else {
s += "@" + beatsfmt(td)
}
}
if pseconds {
if pmilitarytime {
s += "." + secondsfmt(td)
} else {
s += "." + subbeatsfmt(td)
}
}
if pdate {
if pmonthday {
s = monthdayfmt(time.Since(ty0)) + s
} else {
s = dayfmt(time.Since(ty0)) + s
}
}
if pyear {
s = yearfmt(tnow, false) + "/" + s
}
fmt.Println(s)
}