1
0

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.

This commit is contained in:
Alexander Andreev 2023-10-02 03:52:26 +04:00
parent 010774d775
commit f1e2eca876
Signed by: Arav
GPG Key ID: D22A817D95815393

View File

@ -20,6 +20,13 @@ func (s *Song) ArtistTitle() string {
return s.Artist + " - " + s.Title 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) { func (s *Song) MarshalJSON() ([]byte, error) {
return json.Marshal(&struct { return json.Marshal(&struct {
Artist string `json:"artist"` Artist string `json:"artist"`
@ -32,7 +39,7 @@ func (s *Song) MarshalJSON() ([]byte, error) {
Artist: s.Artist, Artist: s.Artist,
Title: s.Title, Title: s.Title,
DurationMill: s.Duration.Milliseconds(), DurationMill: s.Duration.Milliseconds(),
Duration: time.UnixMilli(s.Duration.Milliseconds()).Format("04:05"), Duration: s.DurationString(),
MaxListeners: s.MaxListeners, MaxListeners: s.MaxListeners,
StartAt: s.StartAt.UTC().Format(time.RFC3339)}) StartAt: s.StartAt.UTC().Format(time.RFC3339)})
} }