-
Notifications
You must be signed in to change notification settings - Fork 3
/
date.go
100 lines (78 loc) · 1.98 KB
/
date.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
package faker
import (
"time"
)
// Dater Interface
type Dater interface {
Past(years int) time.Time
Future(years int) time.Time
Between(from, to time.Time) time.Time
Recent(days int) time.Time
Soon(days int) time.Time
Month(abbr, context bool) string
Weekday(abbr, context bool) string
}
// Date struct
type Date struct {
*Fake
}
// Past returns a past time
func (d *Date) Past(years int) time.Time {
t := time.Now()
tNano := t.Unix() * 1000
tNano -= int64(randomIntRange(1000, years*365*24*3600*1000))
return time.Unix(tNano/1000, 0)
}
// Future returns a future time
func (d *Date) Future(years int) time.Time {
t := time.Now()
tNano := t.Unix() * 1000
tNano += int64(randomIntRange(1000, years*365*24*3600*1000))
return time.Unix(tNano/1000, 0)
}
// Between returns a time between two dates
func (d *Date) Between(from, to time.Time) time.Time {
t := time.Now()
offset := random(int(to.Unix() - from.Unix()))
return time.Unix(t.Unix()+int64(offset), 0)
}
// Recent returns a recent time
func (d *Date) Recent(days int) time.Time {
t := time.Now()
tNano := t.Unix() * 1000
tNano -= int64(randomIntRange(1000, days*24*3600*1000))
return time.Unix(tNano/1000, 0)
}
// Soon returns a soon time
func (d *Date) Soon(days int) time.Time {
t := time.Now()
tNano := t.Unix() * 1000
tNano += int64(randomIntRange(1000, days*24*3600*1000))
return time.Unix(tNano/1000, 0)
}
// Month returns a time month
func (d *Date) Month(abbr, context bool) string {
var t string
if abbr {
t = "/abbr"
} else {
t = "/wide"
}
if context && directoryExists(d.DefaultLocale+"/"+datePrefix+"/month"+t+"_context") {
t += "_context"
}
return d.pick(datePrefix + "/month" + t)
}
// Weekday returns a time weekday
func (d *Date) Weekday(abbr, context bool) string {
var t string
if abbr {
t = "/abbr"
} else {
t = "/wide"
}
if context && directoryExists(d.DefaultLocale+"/"+datePrefix+"/weekday"+t+"_context") {
t += "_context"
}
return d.pick(datePrefix + "/weekday" + t)
}