Repository: mopidy/mopidy
Issue: MPD: No Last-Modified timestamp is output for any file

MPD outputs a Last-Modified line. Mopidy's track model has a last_modified attribute, so it should be used in the MPD output.

I'll submit a PR forthwith.


Changed files:
mopidy/mpd/translator.py, tests/mpd/test_translator.py

diff --git a/mopidy/mpd/translator.py b/mopidy/mpd/translator.py
index 025ccdc9..92e5a3ae 100644
--- a/mopidy/mpd/translator.py
+++ b/mopidy/mpd/translator.py
@@ -1,5 +1,6 @@
 from __future__ import absolute_import, unicode_literals
 
+import datetime
 import re
 
 from mopidy.models import TlTrack
@@ -86,6 +87,11 @@ def track_to_mpd_format(track, position=None, stream_title=None):
     if track.disc_no:
         result.append(('Disc', track.disc_no))
 
+    if track.last_modified:
+        datestring = datetime.datetime.utcfromtimestamp(
+            track.last_modified // 1000).isoformat()
+        result.append(('Last-Modified', datestring + 'Z'))
+
     if track.musicbrainz_id is not None:
         result.append(('MUSICBRAINZ_TRACKID', track.musicbrainz_id))
     return result
diff --git a/tests/mpd/test_translator.py b/tests/mpd/test_translator.py
index 99c87dad..066bacad 100644
--- a/tests/mpd/test_translator.py
+++ b/tests/mpd/test_translator.py
@@ -79,6 +79,43 @@ class TrackMpdFormatTest(unittest.TestCase):
         self.assertNotIn(('Comment', 'a comment'), result)
         self.assertEqual(len(result), 14)
 
+    def test_track_to_mpd_format_with_last_modified(self):
+        track = self.track.replace(last_modified=995303899000)
+        result = translator.track_to_mpd_format(track)
+        self.assertIn(('file', 'a uri'), result)
+        self.assertIn(('Time', 137), result)
+        self.assertIn(('Artist', 'an artist'), result)
+        self.assertIn(('Title', 'a name'), result)
+        self.assertIn(('Album', 'an album'), result)
+        self.assertIn(('AlbumArtist', 'an other artist'), result)
+        self.assertIn(('Composer', 'a composer'), result)
+        self.assertIn(('Performer', 'a performer'), result)
+        self.assertIn(('Genre', 'a genre'), result)
+        self.assertIn(('Track', '7/13'), result)
+        self.assertIn(('Date', '1977-01-01'), result)
+        self.assertIn(('Disc', 1), result)
+        self.assertIn(('Last-Modified', '2001-07-16T17:18:19Z'), result)
+        self.assertNotIn(('Comment', 'a comment'), result)
+        self.assertEqual(len(result), 13)
+
+    def test_track_to_mpd_format_with_last_modified_of_zero(self):
+        track = self.track.replace(last_modified=0)
+        result = translator.track_to_mpd_format(track)
+        self.assertIn(('file', 'a uri'), result)
+        self.assertIn(('Time', 137), result)
+        self.assertIn(('Artist', 'an artist'), result)
+        self.assertIn(('Title', 'a name'), result)
+        self.assertIn(('Album', 'an album'), result)
+        self.assertIn(('AlbumArtist', 'an other artist'), result)
+        self.assertIn(('Composer', 'a composer'), result)
+        self.assertIn(('Performer', 'a performer'), result)
+        self.assertIn(('Genre', 'a genre'), result)
+        self.assertIn(('Track', '7/13'), result)
+        self.assertIn(('Date', '1977-01-01'), result)
+        self.assertIn(('Disc', 1), result)
+        self.assertNotIn(('Comment', 'a comment'), result)
+        self.assertEqual(len(result), 12)
+
     def test_track_to_mpd_format_musicbrainz_trackid(self):
         track = self.track.replace(musicbrainz_id='foo')
         result = translator.track_to_mpd_format(track)