forked from dmgk/faker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdate.go
56 lines (45 loc) · 1.44 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
package faker
import "time"
type FakeDate interface {
// Between returns random time in [from, to] interval, with second resolution.
Between(from, to time.Time) time.Time
// Forward returns random time in [time.Now(), time.Now() + duration] interval, with second resolution.
Forward(duration time.Duration) time.Time
// Backward returns random time in [time.Now() - duration, time.Now()] interval, with second resolution.
Backward(duration time.Duration) time.Time
// Birthday returns random time so that age of the person born at that moment would be between minAge and maxAge years.
Birthday(minAge, maxAge int) time.Time
}
type fakeDate struct{}
func Date() FakeDate {
return fakeDate{}
}
func (d fakeDate) Between(from, to time.Time) time.Time {
if to.Sub(from) < 0 {
panic("invalid time range")
}
return time.Unix(RandomInt64(from.Unix(), to.Unix()), 0)
}
func (d fakeDate) Forward(duration time.Duration) time.Time {
if duration < 0 {
panic("invalid duration")
}
now := time.Now()
return d.Between(now, now.Add(duration))
}
func (d fakeDate) Backward(duration time.Duration) time.Time {
if duration < 0 {
panic("invalid duration")
}
now := time.Now()
return d.Between(now.Add(-duration), now)
}
func (d fakeDate) Birthday(minAge, maxAge int) time.Time {
if minAge > maxAge {
panic("invalid age range")
}
now := time.Now()
from := now.AddDate(-maxAge, 0, 0)
to := now.AddDate(-minAge, 0, 0)
return d.Between(from, to)
}