-
Notifications
You must be signed in to change notification settings - Fork 6
/
kair.go
171 lines (147 loc) · 3.85 KB
/
kair.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
// Copyright 2018 Guilherme Caruso. All rights reserved.
// License that can be found in the LICENSE file.
/*
Package Kair Used to facilitate the process of formatting dates and times using human language in your process.
Examples:
> If you need to get the current time:
Kair.Now().Time
> If you need to get a formatted date:
Kair.Date().Time
> If you need to get a formatted datetime:
Kair.DateTime().Time
> Using standart formatters:
Kair.DateTime("20,05,2018,10,20,00").Format("LT") // "10:20 AM"
Kair.DateTime("20,05,2018,10,20,00").Format("LL") // "29/5/2018"
Kair.DateTime("20,05,2018,10,20,00").Format("llll") // "Mon, May 29, 2018 10:20 AM"
-> Using custom formatter:
Kair.DateTime("20,05,2018,10,20,00").PersonalFormat(""MMM/dd/YY h:m:s"") // "May/20/18 10:20:0"
*/
package kair
import (
"regexp"
"strings"
"time"
)
var (
units = []string{"LT", "LTS", "L", "l", "LL", "ll", "LLL", "lll", "LLLL", "llll"}
months = map[int]time.Month{
1: time.January,
2: time.February,
3: time.March,
4: time.April,
5: time.May,
6: time.June,
7: time.July,
8: time.August,
9: time.September,
10: time.October,
11: time.November,
12: time.December,
}
formats = map[string]string{
"MMMM": "January",
"MMM": "Jan",
"MM": "01",
"M": "1",
"YYYY": "2006",
"YY": "06",
"DD": "Monday",
"D": "Mon",
"dd": "02",
"d": "2",
"hh": "03",
"h": "3",
"mm": "04",
"m": "4",
"ss": "05",
"s": "5",
}
)
//SKair Used to standardize the use of functions
type SKair struct {
Time time.Time
}
/*
Format Uses standard sequence for the time format.
Returns a string standart format if var is invalid
Standard formats :
["LT", "LTS", "L", "l", "LL", "ll", "LLL", "lll", "LLLL", "llll"]
*/
func (k *SKair) Format(format string) string {
timeLint := k.Time
var timeriz string
switch format {
case units[0]:
timeriz = timeLint.Format("3:04 PM")
case units[1]:
timeriz = timeLint.Format("3:04:05 PM")
case units[2]:
timeriz = timeLint.Format("02/01/2006")
case units[3]:
timeriz = timeLint.Format("02/1/2006")
case units[4]:
timeriz = timeLint.Format("January 2, 2006")
case units[5]:
timeriz = timeLint.Format("Jan 2, 2006")
case units[6]:
timeriz = timeLint.Format("January 2, 2006 3:04 PM")
case units[7]:
timeriz = timeLint.Format("Jan 2, 2006 3:04 PM")
case units[8]:
timeriz = timeLint.Format("Monday, January 2, 2006 3:04 PM")
case units[9]:
timeriz = timeLint.Format("Mon, Jan 2, 2006 3:04 PM")
default:
timeriz = timeLint.String()
}
return timeriz
}
/*
CustomFormat Uses custom sequence for the time format.
Returns a string custom datetime format
Custom formatters :
"MMMM": Long Month,
"MMM": Month,
"MM": Zero Number Month,
"M": Number Month,
"YYYY": Long Year,
"YY": Year,
"DD": Long Day,
"D": Day,
"dd": Long Number Day,
"d": Number Day,
"hh": Long Hour,
"h": Hour,
"mm": Long Minute,
"m": Minute,
"ss": Long Second,
"s": Second
*/
func (k *SKair) CustomFormat(pformat string) string {
re := regexp.MustCompile(`(?m)(M{4})|(M{3})|(M{2})|(M{1})|(Y{4})|(Y{2})|(D{2})|(D{1})|(d{2})|(d{1})|(h{2})|(h{1})|(m{2})|(m{1})|(s{2})|(s{1})`)
for _, match := range re.FindAllString(pformat, -1) {
if val, ok := formats[match]; ok {
pformat = strings.Replace(pformat, match, val, -1)
}
}
timeriz := k.Time.Format(pformat)
return timeriz
}
//Now - Retrieve current datetime
func Now() *SKair {
var k SKair
k.Time = time.Now()
return &k
}
//Date - Retrieves a custom date
func Date(day int, month int, year int) *SKair {
var k SKair
k.Time = time.Date(year, months[month], day, 0, 0, 0, 0, time.UTC)
return &k
}
// DateTime - Retrieves a custom datetime
func DateTime(day int, month int, year int, hour int, min int, sec int) *SKair {
var k SKair
k.Time = time.Date(year, months[month], day, hour, min, sec, 0, time.UTC)
return &k
}