diff --git a/src/x/time/unit.go b/src/x/time/unit.go index 36f50789c9..bdb5479a8d 100644 --- a/src/x/time/unit.go +++ b/src/x/time/unit.go @@ -49,14 +49,14 @@ var ( ) // Unit represents a time unit. -type Unit byte +type Unit uint16 // Value is the time duration of the time unit. func (tu Unit) Value() (time.Duration, error) { - if d, found := unitsToDuration[tu]; found { - return d, nil + if tu < 1 || int(tu) > len(unitsToDuration) { + return 0, errUnrecognizedTimeUnit } - return 0, errUnrecognizedTimeUnit + return time.Duration(unitsToDuration[tu]), nil } // Count returns the number of units contained within the duration. @@ -65,12 +65,12 @@ func (tu Unit) Count(d time.Duration) (int, error) { return 0, errNegativeDuraton } - if dur, found := unitsToDuration[tu]; found { - return int(d / dur), nil + if tu < 1 || int(tu) > len(unitsToDuration) { + return 0, errUnrecognizedTimeUnit } - // Invalid unit. - return 0, errUnrecognizedTimeUnit + dur := unitsToDuration[tu] + return int(d / dur), nil } // MustCount is like Count but panics if d is negative or if tu is not @@ -86,17 +86,16 @@ func (tu Unit) MustCount(d time.Duration) int { // IsValid returns whether the given time unit is valid / supported. func (tu Unit) IsValid() bool { - _, valid := unitsToDuration[tu] - return valid + return tu > 0 && int(tu) < len(unitsToDuration) } // String returns the string representation for the time unit func (tu Unit) String() string { - if s, found := unitStrings[tu]; found { - return s + if tu < 1 || int(tu) > len(unitsToDuration) { + return "unknown" } - return "unknown" + return unitStrings[tu] } // UnitFromDuration creates a time unit from a time duration. @@ -110,11 +109,11 @@ func UnitFromDuration(d time.Duration) (Unit, error) { // DurationFromUnit creates a time duration from a time unit. func DurationFromUnit(u Unit) (time.Duration, error) { - if duration, found := unitsToDuration[u]; found { - return duration, nil + if u < 1 || int(u) > len(unitsToDuration) { + return 0, errConvertUnitToDuration } - return 0, errConvertUnitToDuration + return unitsToDuration[u], nil } // MaxUnitForDuration determines the maximum unit for which @@ -153,28 +152,33 @@ func MaxUnitForDuration(d time.Duration) (int64, Unit) { } var ( - unitStrings = map[Unit]string{ - Second: "s", - Millisecond: "ms", - Nanosecond: "ns", - Microsecond: "us", - Minute: "m", - Hour: "h", - Day: "d", - Year: "y", + unitStrings = []string{ + "unknown", + "s", + "ms", + "us", + "ns", + "m", + "h", + "d", + "y", } durationsToUnit = make(map[time.Duration]Unit) - unitsToDuration = map[Unit]time.Duration{ - Second: time.Second, - Millisecond: time.Millisecond, - Nanosecond: time.Nanosecond, - Microsecond: time.Microsecond, - Minute: time.Minute, - Hour: time.Hour, - Day: time.Hour * 24, - Year: time.Hour * 24 * 365, + unitsToDuration = []time.Duration{ + time.Duration(0), + time.Second, + time.Millisecond, + time.Microsecond, + time.Nanosecond, + time.Minute, + time.Hour, + time.Hour * 24, + time.Hour * 24 * 365, } + + unitNum = len(unitsToDuration) + unitsByDurationDesc []Unit ) @@ -193,7 +197,12 @@ func (b byDurationDesc) Less(i, j int) bool { func init() { unitsByDurationDesc = make([]Unit, 0, len(unitsToDuration)) - for u, d := range unitsToDuration { + for uu, d := range unitsToDuration { + u := Unit(uu) + if u == None { + continue + } + durationsToUnit[d] = u unitsByDurationDesc = append(unitsByDurationDesc, u) }