forked from icrowley/fake
-
Notifications
You must be signed in to change notification settings - Fork 2
/
dates.go
66 lines (54 loc) · 1.46 KB
/
dates.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 fake
import (
"fmt"
"math/rand"
"time"
)
// Time generates a time.Time between 'from' and 'to'.
func Time(from, to time.Time) time.Time {
diff := to.Sub(from)
ns := rand.Int63n(diff.Nanoseconds())
d, _ := time.ParseDuration(fmt.Sprintf("%dms", ns))
return from.Add(d)
}
// Day generates day of the month
func Day() int {
return rand.Intn(31) + 1
}
// WeekDay generates name ot the week day
func WeekDay() string {
return lookup(lang, "weekdays", true)
}
// WeekDayShort generates abbreviated name of the week day
func WeekDayShort() string {
return lookup(lang, "weekdays_short", true)
}
// WeekdayNum generates number of the day of the week
func WeekdayNum() int {
return rand.Intn(7) + 1
}
// Month generates month name
func Month() string {
return lookup(lang, "months", true)
}
// MonthShort generates abbreviated month name
func MonthShort() string {
return lookup(lang, "months_short", true)
}
// MonthNum generates month number (from 1 to 12)
func MonthNum() int {
return rand.Intn(12) + 1
}
// Year generates year using the given boundaries
func Year(from, to int) int {
n := rand.Intn(to-from) + 1
return from + n
}
// Birthdate returns a date of birth for someone of the given age
func Birthdate(age int) time.Time {
now := time.Now()
startWindow := now.AddDate(-1*(age+1), 0, 0)
endWindow := startWindow.AddDate(1, 0, 0)
randomUnix := startWindow.Unix() + rand.Int63n(endWindow.Unix()-startWindow.Unix())
return time.Unix(randomUnix, 0)
}