storj/cmd/satellite/reports/range.go
Andrew Harding 62c58f4a9a satellite: consistent report range arguments
This change updates the three satellite report commands that accept date
ranges to parse and treat those dates uniformly.

- End dates are now uniformly exclusive. Exclusive end dates helps
operators avoid one-off errors on month boundaries, as in the operator
does not have to remember how many days are in that month and can just
run the report from the 1st (inclusive) through the 1st (exclusive).
- Fixed the date range validity check which only failed if the start
date came after the end date (it should have failed dates that were
equal since the check happened after adjusting for inclusivity).

Change-Id: Ib2ee1c71ddb916c6e1906834d5ff0dc47d1a5801
2019-12-20 17:17:09 +00:00

31 lines
879 B
Go

// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package reports
import (
"time"
"github.com/zeebo/errs"
)
// ParseRange parses report date range arguments. If the dates are malformed or
// if the start date does not come before the end date, an error is returned.
// The end date is exclusive.
func ParseRange(startArg, endArg string) (time.Time, time.Time, error) {
layout := "2006-01-02"
start, err := time.Parse(layout, startArg)
if err != nil {
return time.Time{}, time.Time{}, errs.New("malformed start date (use YYYY-MM-DD)")
}
end, err := time.Parse(layout, endArg)
if err != nil {
return time.Time{}, time.Time{}, errs.New("malformed end date (use YYYY-MM-DD)")
}
if !start.Before(end) {
return time.Time{}, time.Time{}, errs.New("invalid date range: start date must come before end date")
}
return start, end, nil
}