Skip to content

Commit

Permalink
Add support for unavailable durations in Duration type
Browse files Browse the repository at this point in the history
Introduce `DurationUnavailable` constant to represent an indefinite duration. Implement `Duration()` method to return this constant when duration data is unavailable, enhancing the robustness of duration handling.
  • Loading branch information
wneessen committed Aug 20, 2024
1 parent 4ae8885 commit bfe9172
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions duration.go
Original file line number Diff line number Diff line change
@@ -10,6 +10,10 @@ import (
"time"
)

// DurationUnavailable represents an indefinite duration that is used to indicate that a specific
// duration is unavailable.
const DurationUnavailable = time.Duration(-1)

// Duration is a type wrapper of an WeatherData for holding height values in WeatherData
// (based on meters a default unit)
type Duration WeatherData
@@ -45,3 +49,17 @@ func (d Duration) Value() float64 {
}
return d.floatVal
}

// Duration returns the Duration value as time.Duration type
//
// If the Duration is not available in the WeatherData, Duration will return DurationUnavailable instead.
func (d Duration) Duration() time.Duration {
if d.notAvailable {
return DurationUnavailable
}
duration, err := time.ParseDuration(d.String())
if err != nil {
return DurationUnavailable
}
return duration
}

0 comments on commit bfe9172

Please sign in to comment.