storj/private/compensation/period.go
Yaroslav Vorobiov 157d3d980e multinode/console: storage usage and total storage usage
Change-Id: I4970275daf4a4a9c5d02aea6a205891869dd4eff
2021-06-10 16:01:41 +00:00

64 lines
1.5 KiB
Go

// 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,
}
}