-
Notifications
You must be signed in to change notification settings - Fork 2
/
timeframe.go
55 lines (46 loc) · 1.36 KB
/
timeframe.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
package main
import "time"
const dateFormat = "2006-01-02"
// TimeFrame holds information between which dates data can be crawled
type TimeFrame struct {
since string
until string
}
// IsValid checks that:
// since is mot empty
// until is not empty
// since is in the given time format
// until is in the given time format
// since <= until
func (t TimeFrame) IsValid() bool {
if t.since == "" || t.until == "" {
return false
}
sinceTime, err := time.Parse(dateFormat, t.since)
if err != nil {
return false
}
untilTime, err := time.Parse(dateFormat, t.until)
if err != nil {
return false
}
return sinceTime.Before(untilTime)
}
// TimeFrameFromDays returns a TimeFrame by counting back the days in time beginning from today
func TimeFrameFromDays(days int) TimeFrame {
if days <= 0 {
return TimeFrame{}
}
sinceTime := time.Now().AddDate(0, 0, -(days - 1)).Format(dateFormat)
untilTime := time.Now().AddDate(0, 0, 1).Format(dateFormat) // in order to include tweets from today, we have to crawl from "tomorrow"
return TimeFrame{since: sinceTime, until: untilTime}
}
// TimeFrameFromSince returns a TimeFrame from today until the given since date
func TimeFrameFromSince(since string) TimeFrame {
if since == "" {
return TimeFrame{}
}
sinceTime := since
untilTime := time.Now().Format(dateFormat)
return TimeFrame{since: sinceTime, until: untilTime}
}