-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgotime.go
66 lines (62 loc) · 2.05 KB
/
gotime.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
package gotime
import (
"regexp"
"time"
)
var fmtmap = make(map[*regexp.Regexp]string)
func setValues() {
if len(fmtmap) == 0 {
// Full month name
fmtmap[regexp.MustCompile("%B")] = "January"
// Short month name
fmtmap[regexp.MustCompile("%b")] = "Jan"
// Month as zero-padded decimal
fmtmap[regexp.MustCompile("%m")] = "01"
// Full day name
fmtmap[regexp.MustCompile("%A")] = "Monday"
// Short day name
fmtmap[regexp.MustCompile("%a")] = "Mon"
// Day of month as zero-padded decimal
fmtmap[regexp.MustCompile("%d")] = "02"
// Hour (12-hour clock) as zero-padded decimal
fmtmap[regexp.MustCompile("%I")] = "03"
// Hour (24-hour clock) as zero-padded decimal
fmtmap[regexp.MustCompile("%H")] = "15"
// Minute as zero-padded decimal
fmtmap[regexp.MustCompile("%M")] = "04"
// Second as zero-padded decimal
fmtmap[regexp.MustCompile("%S")] = "05"
// Year with century as decimal
fmtmap[regexp.MustCompile("%Y")] = "2006"
// Year without century as zero-padded decimal
fmtmap[regexp.MustCompile("%y")] = "06"
// AM or PM, or locale equivilent
fmtmap[regexp.MustCompile("%p")] = "pm"
// Timezone
fmtmap[regexp.MustCompile("%Z")] = "MST"
}
}
// FormatDateTime uses standard verbs.
// Verbs available are:
// %B : Full month name
// %b : Short month name
// %m : Month as zero-padded decimal
// %A : Full day name
// %a : Short day name
// %d : Day of month as zero-padded decimal
// %I : Hour (12-hour clock) as zero-padded decimal
// %H : Hour (24-hour clock) as zero-padded decimal
// %M : Minute as zero-padded decimal
// %S : Second as zero-padded decimal
// %Y : Year with century as decimal
// %y : Year without century as zero-padded decimal
// %p : AM or PM, or locale equivilent
// %Z : Timezone
// Verbs were taken from the Python3 documentation: https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior
func FormatDateTime(DateTime time.Time, fmt string) string {
setValues()
for re, val := range fmtmap {
fmt = re.ReplaceAllString(fmt, val)
}
return DateTime.Format(fmt)
}