-
Notifications
You must be signed in to change notification settings - Fork 402
/
period.go
63 lines (53 loc) · 1.46 KB
/
period.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
// Copyright (C) 2021 Storj Labs, Inc.
// See LICENSE for copying information.
package compensation
import (
"fmt"
"time"
)
// Period represents a monthly payment period.
type Period struct {
Year int
Month time.Month
}
// String outputs the YYYY-MM form of the payment period.
func (p Period) String() string {
return fmt.Sprintf("%04d-%02d", p.Year, p.Month)
}
// StartDate returns a time.Time that is less than or equal to any time in the period.
func (p Period) StartDate() time.Time {
return time.Date(p.Year, p.Month, 1, 0, 0, 0, 0, time.UTC)
}
// EndDateExclusive returns a time.Time that is greater than any time in the period.
func (p Period) EndDateExclusive() time.Time {
return time.Date(p.Year, p.Month+1, 1, 0, 0, 0, 0, time.UTC)
}
// UnmarshalCSV reads the Period in CSV form.
func (p *Period) UnmarshalCSV(s string) error {
v, err := PeriodFromString(s)
if err != nil {
return err
}
*p = v
return nil
}
// MarshalCSV returns the CSV form of the Period.
func (p Period) MarshalCSV() (string, error) {
return p.String(), nil
}
// PeriodFromString parses the YYYY-MM string into a Period.
func PeriodFromString(s string) (Period, error) {
t, err := time.Parse("2006-01", s)
if err != nil {
return Period{}, err
}
return PeriodFromTime(t), nil
}
// PeriodFromTime takes a time.Time and returns a Period that contains it.
func PeriodFromTime(t time.Time) Period {
year, month, _ := t.UTC().Date()
return Period{
Year: year,
Month: month,
}
}