-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtime.go
More file actions
267 lines (235 loc) · 6.7 KB
/
time.go
File metadata and controls
267 lines (235 loc) · 6.7 KB
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
package gofaker
import (
"errors"
"strconv"
"time"
)
// Date will generate a random time.Time struct
func Date() time.Time {
return time.Date(Year(), time.Month(Number(0, 12)), Day(), Hour(), Minute(), Second(), NanoSecond(), time.UTC)
}
// DateRange will generate a random time.Time struct between a start and end date
func DateRange(start, end time.Time) time.Time {
return time.Unix(0, int64(Number(int(start.UnixNano()), int(end.UnixNano())))).UTC()
}
// NanoSecond will generate a random nano second
func NanoSecond() int {
return Number(0, 999999999)
}
// Second will generate a random second
func Second() int {
return Number(0, 59)
}
// Minute will generate a random minute
func Minute() int {
return Number(0, 59)
}
// Hour will generate a random hour - in military time
func Hour() int {
return Number(0, 23)
}
// Day will generate a random day between 1 - 31
func Day() int {
return Number(1, 31)
}
// WeekDay will generate a random weekday string (Monday-Sunday)
func WeekDay() string {
return time.Weekday(Number(0, 6)).String()
}
// Month will generate a random month string
func Month() string {
return time.Month(Number(1, 12)).String()
}
// Year will generate a random year between 1900 - current year
func Year() int {
return Number(1900, time.Now().Year())
}
// TimeZone will select a random timezone string
func TimeZone() string {
return getRandValue([]string{"timezone", "text"})
}
// TimeZoneFull will select a random full timezone string
func TimeZoneFull() string {
return getRandValue([]string{"timezone", "full"})
}
// TimeZoneRegion will select a random region style timezone string, e.g. "America/Chicago"
func TimeZoneRegion() string {
return getRandValue([]string{"timezone", "region"})
}
// TimeZoneAbv will select a random timezone abbreviation string
func TimeZoneAbv() string {
return getRandValue([]string{"timezone", "abr"})
}
// TimeZoneOffset will select a random timezone offset
func TimeZoneOffset() float32 {
value, _ := strconv.ParseFloat(getRandValue([]string{"timezone", "offset"}), 32)
return float32(value)
}
func addDateTimeLookup() {
AddFuncLookup("date", Info{
Display: "Date",
Category: "time",
Description: "Random date",
Example: "2006-01-02T15:04:05Z07:00",
Output: "string",
Params: []Param{
{
Field: "format",
Display: "Format",
Type: "string",
Default: "RFC3339",
Options: []string{"ANSIC", "UnixDate", "RubyDate", "RFC822", "RFC822Z", "RFC850", "RFC1123", "RFC1123Z", "RFC3339", "RFC3339Nano"},
Description: "Date time string format output",
},
},
Call: func(m *map[string][]string, info *Info) (interface{}, error) {
format, err := info.GetString(m, "format")
if err != nil {
return nil, err
}
switch format {
case "ANSIC":
return Date().Format(time.ANSIC), nil
case "UnixDate":
return Date().Format(time.UnixDate), nil
case "RubyDate":
return Date().Format(time.RubyDate), nil
case "RFC822":
return Date().Format(time.RFC822), nil
case "RFC822Z":
return Date().Format(time.RFC822Z), nil
case "RFC850":
return Date().Format(time.RFC850), nil
case "RFC1123":
return Date().Format(time.RFC1123), nil
case "RFC1123Z":
return Date().Format(time.RFC1123Z), nil
case "RFC3339":
return Date().Format(time.RFC3339), nil
case "RFC3339Nano":
return Date().Format(time.RFC3339Nano), nil
}
return "", errors.New("Invalid format")
},
})
AddFuncLookup("nanosecond", Info{
Display: "Nanosecond",
Category: "time",
Description: "Random nanosecond",
Example: "196446360",
Output: "int",
Call: func(m *map[string][]string, info *Info) (interface{}, error) {
return NanoSecond(), nil
},
})
AddFuncLookup("second", Info{
Display: "Second",
Category: "time",
Description: "Random second",
Example: "43",
Output: "int",
Call: func(m *map[string][]string, info *Info) (interface{}, error) {
return Second(), nil
},
})
AddFuncLookup("minute", Info{
Display: "Minute",
Category: "time",
Description: "Random minute",
Example: "34",
Output: "int",
Call: func(m *map[string][]string, info *Info) (interface{}, error) {
return Second(), nil
},
})
AddFuncLookup("hour", Info{
Display: "Hour",
Category: "time",
Description: "Random hour",
Example: "8",
Output: "int",
Call: func(m *map[string][]string, info *Info) (interface{}, error) {
return Second(), nil
},
})
AddFuncLookup("day", Info{
Display: "Day",
Category: "time",
Description: "Random day",
Example: "12",
Output: "int",
Call: func(m *map[string][]string, info *Info) (interface{}, error) {
return Day(), nil
},
})
AddFuncLookup("weekday", Info{
Display: "Weekday",
Category: "time",
Description: "Random week day",
Example: "Friday",
Output: "string",
Call: func(m *map[string][]string, info *Info) (interface{}, error) {
return WeekDay(), nil
},
})
AddFuncLookup("year", Info{
Display: "Year",
Category: "time",
Description: "Random year",
Example: "1900",
Output: "int",
Call: func(m *map[string][]string, info *Info) (interface{}, error) {
return Year(), nil
},
})
AddFuncLookup("timezone", Info{
Display: "Timezone",
Category: "time",
Description: "Random timezone",
Example: "Kaliningrad Standard Time",
Output: "string",
Call: func(m *map[string][]string, info *Info) (interface{}, error) {
return TimeZone(), nil
},
})
AddFuncLookup("timezoneabv", Info{
Display: "Timezone Abbreviation",
Category: "time",
Description: "Random abbreviated timezone",
Example: "KST",
Output: "string",
Call: func(m *map[string][]string, info *Info) (interface{}, error) {
return TimeZoneAbv(), nil
},
})
AddFuncLookup("timezonefull", Info{
Display: "Timezone Full",
Category: "time",
Description: "Random full timezone",
Example: "(UTC+03:00) Kaliningrad, Minsk",
Output: "string",
Call: func(m *map[string][]string, info *Info) (interface{}, error) {
return TimeZoneFull(), nil
},
})
AddFuncLookup("timezoneoffset", Info{
Display: "Timezone Offset",
Category: "time",
Description: "Random timezone offset",
Example: "3",
Output: "float32",
Call: func(m *map[string][]string, info *Info) (interface{}, error) {
return TimeZoneOffset(), nil
},
})
AddFuncLookup("timezoneregion", Info{
Display: "Timezone Region",
Category: "time",
Description: "Random region timezone",
Example: "America/Alaska",
Output: "string",
Call: func(m *map[string][]string, info *Info) (interface{}, error) {
return TimeZoneRegion(), nil
},
})
}