From f1e2eca8767f7f47c8c75a57b0273a6a1abcbdff Mon Sep 17 00:00:00 2001 From: "Alexander \"Arav\" Andreev" Date: Mon, 2 Oct 2023 03:52:26 +0400 Subject: [PATCH] Added a DurationString() method for a Song that returns time in format 04:05 or 15:04:05 if a song is one hour or more long. --- internal/radio/song.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/internal/radio/song.go b/internal/radio/song.go index f9d90e3..ccc70f4 100644 --- a/internal/radio/song.go +++ b/internal/radio/song.go @@ -20,6 +20,13 @@ func (s *Song) ArtistTitle() string { return s.Artist + " - " + s.Title } +func (s *Song) DurationString() string { + if s.Duration.Hours() > 0 { + return time.UnixMilli(s.Duration.Milliseconds()).Format("15:04:05") + } + return time.UnixMilli(s.Duration.Milliseconds()).Format("04:05") +} + func (s *Song) MarshalJSON() ([]byte, error) { return json.Marshal(&struct { Artist string `json:"artist"` @@ -32,7 +39,7 @@ func (s *Song) MarshalJSON() ([]byte, error) { Artist: s.Artist, Title: s.Title, DurationMill: s.Duration.Milliseconds(), - Duration: time.UnixMilli(s.Duration.Milliseconds()).Format("04:05"), + Duration: s.DurationString(), MaxListeners: s.MaxListeners, StartAt: s.StartAt.UTC().Format(time.RFC3339)}) }