From 82440840077f82f0be3043063961fb84881b6126 Mon Sep 17 00:00:00 2001 From: Jonas L Date: Wed, 15 Aug 2018 20:16:51 +0200 Subject: [PATCH] Add parsing year for vorbis comment (#39) --- vorbis.go | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/vorbis.go b/vorbis.go index 54f6a20..07f8903 100644 --- a/vorbis.go +++ b/vorbis.go @@ -10,6 +10,7 @@ import ( "io" "strconv" "strings" + "time" ) func newMetadataVorbis() *metadataVorbis { @@ -205,8 +206,23 @@ func (m *metadataVorbis) Genre() string { } func (m *metadataVorbis) Year() int { - // FIXME: try to parse the date in m.c["date"] to extract this - return 0 + var dateFormat string + + // The date need to follow the international standard https://en.wikipedia.org/wiki/ISO_8601 + // and obviously the VorbisComment standard https://wiki.xiph.org/VorbisComment#Date_and_time + switch len(m.c["date"]) { + case 0: + return 0 + case 4: + dateFormat = "2006" + case 7: + dateFormat = "2006-01" + case 10: + dateFormat = "2006-01-02" + } + + t, _ := time.Parse(dateFormat, m.c["date"]) + return t.Year() } func (m *metadataVorbis) Track() (int, int) {