1
0
dwelling-radio/internal/radio/song.go

41 lines
1.1 KiB
Go
Raw Permalink Normal View History

package radio
import (
"encoding/json"
"time"
)
type Song struct {
Artist string
Title string
StartAt time.Time
Duration time.Duration
Listeners int64
PeakListeners int64
}
// DurationString returns song's duration as a string formatted as [H:]M:SS.
func (s *Song) DurationString() string {
if s.Duration.Hours() >= 1 {
return time.UnixMilli(s.Duration.Milliseconds()).Format("3:4:05")
}
return time.UnixMilli(s.Duration.Milliseconds()).Format("4:05")
}
func (s *Song) MarshalJSON() ([]byte, error) {
return json.Marshal(&struct {
Artist string `json:"artist"`
Title string `json:"title"`
DurationMill int64 `json:"duration_msec,omitempty"`
Listeners int64 `json:"listeners,omitempty"`
PeakListeners int64 `json:"peak_listeners,omitempty"`
StartAt string `json:"start_at"`
}{
Artist: s.Artist,
Title: s.Title,
DurationMill: s.Duration.Milliseconds(),
Listeners: s.Listeners,
PeakListeners: s.PeakListeners,
StartAt: s.StartAt.UTC().Format(time.RFC3339)})
}