@@ -338,8 +338,11 @@ impl Track {
338338 }
339339 }
340340
341- /// tries to convert from a `rspotify::model::FullTrack` into `Track`
342- pub fn try_from_full_track ( track : rspotify:: model:: FullTrack ) -> Option < Self > {
341+ /// tries to convert from a `rspotify::model::FullTrack` into `Track` with a optional added_at date
342+ fn try_from_full_track_with_date (
343+ track : rspotify:: model:: FullTrack ,
344+ added_at : Option < chrono:: DateTime < chrono:: Utc > > ,
345+ ) -> Option < Self > {
343346 if track. is_playable . unwrap_or ( true ) {
344347 let id = match track. linked_from {
345348 Some ( d) => d. id ?,
@@ -352,12 +355,26 @@ impl Track {
352355 album : Album :: try_from_simplified_album ( track. album ) ,
353356 duration : track. duration . to_std ( ) . expect ( "valid chrono duration" ) ,
354357 explicit : track. explicit ,
355- added_at : 0 ,
358+ added_at : added_at . map ( |t| t . timestamp ( ) as u64 ) . unwrap_or_default ( ) ,
356359 } )
357360 } else {
358361 None
359362 }
360363 }
364+
365+ /// tries to convert from a `rspotify::model::FullTrack` into `Track`
366+ pub fn try_from_full_track ( track : rspotify:: model:: FullTrack ) -> Option < Self > {
367+ Track :: try_from_full_track_with_date ( track, None )
368+ }
369+
370+ /// tries to convert from a `rspotify::model::PlaylistItem` into `Track`
371+ pub fn try_from_playlist_item ( item : rspotify:: model:: PlaylistItem ) -> Option < Self > {
372+ let rspotify:: model:: PlayableItem :: Track ( track) = item. track ? else {
373+ return None ;
374+ } ;
375+
376+ Track :: try_from_full_track_with_date ( track, item. added_at )
377+ }
361378}
362379
363380impl std:: fmt:: Display for Track {
0 commit comments