fs: Implement fs.Time

Similar to fs.Duration but parses into a timestamp instead

Supports parsing from:

* Any of the date formats in parseTimeDates
* A time.Duration offset from now
* parseDurationSuffixes offset from now
This commit is contained in:
SwazRGB 2022-03-26 19:49:04 +01:00 committed by Nick Craig-Wood
parent e34c543660
commit a8cd18faf3
3 changed files with 252 additions and 4 deletions

View file

@ -76,18 +76,28 @@ var timeFormats = []string{
"2006-01-02",
}
// parse the age as time before the epoch in various date formats
func parseDurationDates(age string, epoch time.Time) (t time.Duration, err error) {
// parse the date as time in various date formats
func parseTimeDates(date string) (t time.Time, err error) {
var instant time.Time
for _, timeFormat := range timeFormats {
instant, err = time.ParseInLocation(timeFormat, age, time.Local)
instant, err = time.ParseInLocation(timeFormat, date, time.Local)
if err == nil {
return epoch.Sub(instant), nil
return instant, nil
}
}
return t, err
}
// parse the age as time before the epoch in various date formats
func parseDurationDates(age string, epoch time.Time) (d time.Duration, err error) {
instant, err := parseTimeDates(age)
if err != nil {
return d, err
}
return epoch.Sub(instant), nil
}
// parseDurationFromNow parses a duration string. Allows ParseDuration to match the time
// package and easier testing within the fs package.
func parseDurationFromNow(age string, getNow func() time.Time) (d time.Duration, err error) {