41 lines
1.0 KiB
Go
41 lines
1.0 KiB
Go
package radio
|
|
|
|
import (
|
|
"encoding/json"
|
|
"time"
|
|
)
|
|
|
|
type Song struct {
|
|
Artist string
|
|
Title string
|
|
StartAt time.Time
|
|
Duration time.Duration
|
|
Listeners int64
|
|
MaxListeners 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"`
|
|
MaxListeners int64 `json:"max_listeners"`
|
|
StartAt string `json:"start_at"`
|
|
}{
|
|
Artist: s.Artist,
|
|
Title: s.Title,
|
|
DurationMill: s.Duration.Milliseconds(),
|
|
Listeners: s.Listeners,
|
|
MaxListeners: s.MaxListeners,
|
|
StartAt: s.StartAt.UTC().Format(time.RFC3339)})
|
|
}
|