This repository has been archived by the owner on Apr 26, 2021. It is now read-only.
/
formatDate.go
146 lines (140 loc) · 6.11 KB
/
formatDate.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
// formatDate.go
package genLib
import (
"regexp"
"strconv"
"strings"
"time"
)
type Entry struct {
InPos int
OutPos int
Val int
Str string
}
func NewDateFormat() []string {
tmpString := []string{}
tmpString = append(tmpString, "%d-%m-%y") // Day-Month-Year
tmpString = append(tmpString, "%y-%m-%d") // Year-Month-Day
tmpString = append(tmpString, "%m-%d-%y") // Month-Day-Year
return tmpString
}
// Find the date in relation to the given format. (French format like %d, %m, %y or Y%). input fmtDate: "%d-%m-%y %H:%M:%S"
func FindDate(inputString, fmtDate string) []string {
// Remove %H:%M:%S
splitSpace := strings.Split(fmtDate, " ")
// Split d, m, y
splitAlNum := regexp.MustCompile("[[:punct:]]")
outstr := splitAlNum.Split(strings.ToLower(splitSpace[0]), -1)
var outShort, outLong, outSingleShort, outSingleLong, out2Single1Short, out2Single1Long, out1Single2Short, out1Single2Long []string
// Replace d with 21, m with 12 and y or Y with 73 or 1973, there will be converted into character class equivalent
for _, val := range outstr {
switch val {
case "d":
out1Single2Short = append(out1Single2Short, "1")
out1Single2Long = append(out1Single2Long, "1")
out2Single1Short = append(out2Single1Short, "21")
out2Single1Long = append(out2Single1Long, "21")
outSingleShort = append(outSingleShort, "1")
outSingleLong = append(outSingleLong, "1")
outShort = append(outShort, "21")
outLong = append(outLong, "21")
case "m":
out1Single2Short = append(out1Single2Short, "21")
out1Single2Long = append(out1Single2Long, "21")
out2Single1Short = append(out2Single1Short, "1")
out2Single1Long = append(out2Single1Long, "1")
outSingleShort = append(outSingleShort, "2")
outSingleLong = append(outSingleLong, "2")
outShort = append(outShort, "12")
outLong = append(outLong, "12")
case "y":
out1Single2Short = append(out1Single2Short, "73")
out1Single2Long = append(out1Single2Long, "1973")
out2Single1Short = append(out2Single1Short, "73")
out2Single1Long = append(out2Single1Long, "1973")
outSingleShort = append(outSingleShort, "73")
outSingleLong = append(outSingleLong, "1973")
outShort = append(outShort, "73")
outLong = append(outLong, "1973")
}
}
dateLongFmt := regexp.MustCompile(StringToCharacterClasses(strings.Join(outLong, "/"), false, false))
dateShrtFmt := regexp.MustCompile(StringToCharacterClasses(strings.Join(outShort, "/"), false, false))
dateSingleLongFmt := regexp.MustCompile(StringToCharacterClasses(strings.Join(outSingleLong, "/"), false, false))
dateSingleShrtFmt := regexp.MustCompile(StringToCharacterClasses(strings.Join(outSingleShort, "/"), false, false))
date1Single2LongFmt := regexp.MustCompile(StringToCharacterClasses(strings.Join(out1Single2Long, "/"), false, false))
date1Single2ShrtFmt := regexp.MustCompile(StringToCharacterClasses(strings.Join(out1Single2Short, "/"), false, false))
date2Single1LongFmt := regexp.MustCompile(StringToCharacterClasses(strings.Join(out2Single1Long, "/"), false, false))
date2Single1ShrtFmt := regexp.MustCompile(StringToCharacterClasses(strings.Join(out2Single1Short, "/"), false, false))
// Search for character class replacement of integer.
result := dateLongFmt.FindAllString(inputString, 1)
if result == nil {
result = append(result, dateShrtFmt.FindAllString(inputString, 1)...)
if result == nil {
result = append(result, date1Single2LongFmt.FindAllString(inputString, 1)...)
if result == nil {
result = append(result, date1Single2ShrtFmt.FindAllString(inputString, 1)...)
if result == nil {
result = append(result, date2Single1LongFmt.FindAllString(inputString, 1)...)
if result == nil {
result = append(result, date2Single1ShrtFmt.FindAllString(inputString, 1)...)
if result == nil {
result = append(result, dateSingleLongFmt.FindAllString(inputString, 1)...)
if result == nil {
result = append(result, dateSingleShrtFmt.FindAllString(inputString, 1)...)
}
}
}
}
}
}
}
return result
}
// Change date format to unix model with pattern like: fmtDate:='%d-%m-%y %H:%M:%S' --> '$y-%m-%d %H:%M:%S'
// Note: year format must be in lowercase ...
func FormatDate(fmtDate string, inpDate string) time.Time {
// Make variable with defaults values (Unix date)
lnxlistDate := []Entry{}
fmtListDate := []Entry{}
tmpListDate := [6]Entry{}
tmpInt := 0
timeUnix := []string{`y`, `m`, `d`, `H`, `M`, `S`}
for idx, str := range timeUnix {
lnxlistDate = append(lnxlistDate, Entry{0, idx, 0, str})
} // Split fmtDate elements
regExp := `[[:alpha:]]`
re := regexp.MustCompile(regExp)
submatchall := re.FindAllString(fmtDate, -1)
for idx, str := range submatchall {
fmtListDate = append(fmtListDate, Entry{idx, 0, 0, str})
} // Split inpDate elements
regExp = `[^[:alnum:]]`
re = regexp.MustCompile(regExp)
submatchall = re.Split(inpDate, -1)
for idx, str := range submatchall {
tmpInt, _ = strconv.Atoi(str)
fmtListDate[idx] = Entry{fmtListDate[idx].InPos, 0, tmpInt, fmtListDate[idx].Str}
} // Mixing data
for lnxIdx, lnx := range lnxlistDate {
for fmtIdx, frmt := range fmtListDate {
if frmt.Str == timeUnix[0] && len(strconv.Itoa(frmt.Val)) == 2 { // Deal with short year format to convert to 4 digits
if fmtListDate[fmtIdx].Val <= 70 { // Using EPOCH year date (1970) as reference
fmtListDate[fmtIdx].Val = fmtListDate[fmtIdx].Val + 2000
} else {
fmtListDate[fmtIdx].Val = fmtListDate[fmtIdx].Val + 1900
}
} // Create output datas: `[{0 2 9 d} {1 1 6 m} {2 0 2018 y} {3 3 6 H} {4 4 54 M} {5 5 5 S}]`
if lnx.Str == frmt.Str { // from this one: `9/06/18 6-54-05` for exemple
fmtListDate[fmtIdx].OutPos = lnxlistDate[lnxIdx].OutPos
}
}
}
// Sorting result array to conforming unix std: [{2 0 2018 y} {1 1 6 m} {0 2 9 d} {3 3 6 H} {4 4 54 M} {5 5 5 S}]
for idx := 0; idx < len(fmtListDate); idx++ {
tmpListDate[fmtListDate[idx].OutPos] = fmtListDate[idx]
}
// fmt.Println(lnxlistDate) // fmt.Println(fmtListDate) // fmt.Println(tmpListDate)
return time.Date(tmpListDate[0].Val, time.Month(tmpListDate[1].Val), tmpListDate[2].Val, tmpListDate[3].Val, tmpListDate[4].Val, tmpListDate[5].Val, 0, time.Local)
}