Skip to content

Commit

Permalink
style: time/timeutil: simplify DurationInfoString.Duration()
Browse files Browse the repository at this point in the history
  • Loading branch information
grokify committed Sep 17, 2023
1 parent 1b2bed1 commit 445cedb
Showing 1 changed file with 17 additions and 78 deletions.
95 changes: 17 additions & 78 deletions time/timeutil/duration_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,24 +282,29 @@ func (dis DurationInfoString) Duration() (time.Duration, error) {
} else {
d += dx
}
timeUnitFuncs := []struct {

timeUnitData := []struct {
v string
f func(string) (time.Duration, error)
n int64
}{
{dis.Hours, parseDurationHours},
{dis.Minutes, parseDurationMinutes},
{dis.Seconds, parseDurationSeconds},
{dis.Milliseconds, parseDurationMilliseconds},
{dis.Microseconds, parseDurationMicroseconds},
{dis.Nanoseconds, parseDurationMicroseconds},
{dis.Hours, NanosPerHour},
{dis.Minutes, NanosPerMinute},
{dis.Seconds, NanosPerSecond},
{dis.Milliseconds, NanosPerMillisecond},
{dis.Microseconds, NanosPerMicrosecond},
{dis.Nanoseconds, 1},
}

for _, tu := range timeUnitFuncs {
if dx, err := tu.f(tu.v); err != nil {
for _, tu := range timeUnitData {
v := strings.TrimSpace(tu.v)
if v == "" || v == "0" {
continue
}
f, err := strconv.ParseFloat(v, 64)
if err != nil {
return d, err
} else {
d += dx
}
d += time.Duration(int64(f * float64(tu.n)))
}

return d, nil
Expand Down Expand Up @@ -329,69 +334,3 @@ func parseDurationDays(d string, hoursPerDay float32) (time.Duration, error) {
return time.Duration(int64(f * float64(NanosPerHour) * float64(hoursPerDay))), nil
}
}

func parseDurationHours(d string) (time.Duration, error) {
d = strings.TrimSpace(d)
if d == "" || d == "0" {
return 0, nil
} else if f, err := strconv.ParseFloat(d, 64); err != nil {
return 0, err
} else {
return time.Duration(int64(f * float64(NanosPerHour))), nil
}
}

func parseDurationMinutes(d string) (time.Duration, error) {
d = strings.TrimSpace(d)
if d == "" || d == "0" {
return 0, nil
} else if f, err := strconv.ParseFloat(d, 64); err != nil {
return 0, err
} else {
return time.Duration(int64(f * float64(NanosPerMinute))), nil
}
}

func parseDurationSeconds(d string) (time.Duration, error) {
d = strings.TrimSpace(d)
if d == "" || d == "0" {
return 0, nil
} else if f, err := strconv.ParseFloat(d, 64); err != nil {
return 0, err
} else {
return time.Duration(int64(f * float64(NanosPerSecond))), nil
}
}

func parseDurationMilliseconds(d string) (time.Duration, error) {
d = strings.TrimSpace(d)
if d == "" || d == "0" {
return 0, nil
} else if f, err := strconv.ParseFloat(d, 64); err != nil {
return 0, err
} else {
return time.Duration(int64(f * float64(NanosPerMillisecond))), nil
}
}

func parseDurationMicroseconds(d string) (time.Duration, error) {
d = strings.TrimSpace(d)
if d == "" || d == "0" {
return 0, nil
} else if f, err := strconv.ParseFloat(d, 64); err != nil {
return 0, err
} else {
return time.Duration(int64(f * float64(NanosPerMicrosecond))), nil
}
}

func parseDurationNanoseconds(d string) (time.Duration, error) {
d = strings.TrimSpace(d)
if d == "" || d == "0" {
return 0, nil
} else if f, err := strconv.ParseFloat(d, 64); err != nil {
return 0, err
} else {
return time.Duration(int64(f)), nil
}
}

0 comments on commit 445cedb

Please sign in to comment.