-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDateTimeMate_test.go
79 lines (65 loc) · 2.01 KB
/
DateTimeMate_test.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
package DateTimeMate
import "testing"
func TestShrinkPeriod(t *testing.T) {
// explicitly define these here as a sanity check vs just iterating through abbrevMap
var allLongPeriods = []string{"nanoseconds", "microseconds", "milliseconds", "seconds", "minutes", "hours", "days", "weeks", "months", "years"}
var allShortPeriods = []string{"ns", "us", "ms", "s", "m", "h", "D", "W", "M", "Y"}
for i, period := range allLongPeriods {
shrunk := shrinkPeriod(period)
if shrunk != allShortPeriods[i] {
t.Errorf("[computed: %v] != [correct: %v]", shrunk, allShortPeriods[i])
}
}
for i, period := range allLongPeriods {
period = removeTrailingS(period)
shrunk := shrinkPeriod(period)
if shrunk != allShortPeriods[i] {
t.Errorf("[computed: %v] != [correct: %v]", shrunk, allShortPeriods[i])
}
}
}
func testFormat(t *testing.T, source, outputFormat, correct string) {
t.Helper()
computed, err := Reformat(source, outputFormat)
if err != nil {
t.Error(err)
}
if computed != correct {
t.Errorf("[computed: %v] != [correct: %v]", computed, correct)
}
}
func TestFormatCommand(t *testing.T) {
source := "2024-07-22 08:21:44"
fmt := "%T %D"
correct := "08:21:44 07/22/24"
testFormat(t, source, fmt, correct)
fmt = "%v %r"
correct = "22-Jul-2024 08:21:44 AM"
testFormat(t, source, fmt, correct)
fmt = "%Y%m%d.%H%M%S"
correct = "20240722.082144"
testFormat(t, source, fmt, correct)
source = "2024-02-29T23:59:59Z"
fmt = "%Y%m%d.%H%M%S"
correct = "20240229.235959"
testFormat(t, source, fmt, correct)
fmt = "%Z"
correct = "UTC"
testFormat(t, source, fmt, correct)
source = "Mon Jul 22 08:40:33 EDT 2024"
fmt = "%Z %z"
correct = "EDT -0400"
testFormat(t, source, fmt, correct)
source = "2024-11-16 14:01:02"
fmt = "%s"
correct = "1731783662"
testFormat(t, source, fmt, correct)
source = "1704085262"
fmt = "%F %T"
correct = "2024-01-01 00:01:02"
testFormat(t, source, fmt, correct)
source = "1704085262999"
fmt = "%F %T"
correct = "2024-01-01 00:01:02"
testFormat(t, source, fmt, correct)
}