Debian Codegraph @

domain/time.go

raw
1 package domain
2
3 import (
4 "encoding/json"
5 "fmt"
6 "net/url"
7 "strconv"
8 "strings"
9 "time"
10 )
11
12 // ShortDate represents a date without a time, i.e. the time is always zero.
13 type ShortDate struct {
14 time.Time
15 }
16
17 // NewShortDate creates a ShortDate from a given time
18 func NewShortDate(date time.Time) ShortDate {
19 return Date(date.Year(), date.Month(), date.Day(), date.Location())
20 }
21
22 // Date returns a new ShortDate for the given year, month, day and location
23 func Date(year int, month time.Month, day int, location *time.Location) ShortDate {
24 return ShortDate{time.Date(year, month, day, 0, 0, 0, 0, time.UTC)}
25 }
26
27 // MarshalJSON marshals the date into a JSON representation
28 func (s *ShortDate) MarshalJSON() ([]byte, error) {
29 if s.IsZero() {
30 return json.Marshal("")
31 }
32 return json.Marshal(s.Format("2006-01-02"))
33 }
34
35 // UnmarshalJSON unmarshals the JSON representation into a date
36 func (s *ShortDate) UnmarshalJSON(data []byte) error {
37 unquotedData, _ := strconv.Unquote(string(data))
38 time, err := time.Parse("2006-01-02", unquotedData)
39 s.Time = time
40 return err
41 }
42
43 func (s *ShortDate) String() string {
44 return s.Format("2006-01-02")
45 }
46
47 // MarshalText marshals the date into a byte representation
48 func (s *ShortDate) MarshalText() ([]byte, error) {
49 return []byte(s.Format("2006-01-02")), nil
50 }
51
52 // UnmarshalText unmarshals text into a ShortDate
53 func (s *ShortDate) UnmarshalText(text []byte) error {
54 time, err := time.Parse("2006-01-02", string(text))
55 if err != nil {
56 return err
57 }
58 *s = ShortDate{time}
59 return nil
60 }
61
62 // Timeframe represents a date range
63 type Timeframe struct {
64 StartDate ShortDate
65 EndDate ShortDate
66 }
67
68 // TimeframeFromDate returns a Timeframe with the StartDate set to date and the EndDate set to today.
69 // The EndDate will use the same timezone location as provided in StartDate
70 func TimeframeFromDate(date ShortDate) Timeframe {
71 endDate := NewShortDate(time.Now().In(date.Location()))
72 return Timeframe{date, endDate}
73 }
74
75 // TimeframeFromQuery parses a timeframe from a query. The param keys are
76 // expected to be `from` for the StartDate and `to` for the EndDate
77 func TimeframeFromQuery(params url.Values) (Timeframe, error) {
78 from := params.Get("from")
79 to := params.Get("to")
80 if from == "" || to == "" {
81 return Timeframe{}, fmt.Errorf("'from' and/or 'to' must be set")
82 }
83 startTime, err1 := time.Parse("20060102", from)
84 startDate := ShortDate{startTime}
85 endTime, err2 := time.Parse("20060102", to)
86 endDate := ShortDate{endTime}
87 if err1 != nil || err2 != nil {
88 return Timeframe{}, fmt.Errorf("Malformed query params")
89 }
90 return Timeframe{StartDate: startDate, EndDate: endDate}, nil
91 }
92
93 // ToQuery transforms a timeframe to a query. The param keys are
94 // `from` for the StartDate and `to` for the EndDate
95 func (tf *Timeframe) ToQuery() url.Values {
96 params := make(url.Values)
97 params.Set("from", tf.StartDate.Format("20060102"))
98 params.Set("to", tf.EndDate.Format("20060102"))
99 return params
100 }
101
102 // MarshalJSON marhsals the timeframe into a JSON string
103 func (tf *Timeframe) MarshalJSON() ([]byte, error) {
104 if tf.StartDate.IsZero() || tf.EndDate.IsZero() {
105 return json.Marshal("")
106 }
107 return json.Marshal(fmt.Sprintf("%s,%s", tf.StartDate.Format("2006-01-02"), tf.EndDate.Format("2006-01-02")))
108 }
109
110 // UnmarshalJSON unmarshals data into a timeframe
111 func (tf *Timeframe) UnmarshalJSON(data []byte) error {
112 unquotedData, _ := strconv.Unquote(string(data))
113 dates := strings.Split(unquotedData, ",")
114 if len(dates) != 2 {
115 *tf = Timeframe{}
116 return nil
117 }
118 startTime, err1 := time.Parse("2006-01-02", dates[0])
119 startDate := ShortDate{startTime}
120 endTime, err2 := time.Parse("2006-01-02", dates[1])
121 endDate := ShortDate{endTime}
122 if err1 != nil || err2 != nil {
123 *tf = Timeframe{}
124 return nil
125 }
126 *tf = Timeframe{StartDate: startDate, EndDate: endDate}
127 return nil
128 }
129
130 // IsZero returns true when StartDate and EndDate are both zero, i.e. when the
131 // Timeframe is uninitialized.
132 func (tf *Timeframe) IsZero() bool {
133 return tf.StartDate.IsZero() && tf.EndDate.IsZero()
134 }
135
136 func (tf *Timeframe) String() string {
137 return fmt.Sprintf("{%s-%s}", tf.StartDate, tf.EndDate)
138 }