@@ -118,6 +118,15 @@ impl DownloadTracker {
118118 }
119119 self . prepare_for_new_download ( ) ;
120120 }
121+ // we're doing modular arithmetic, treat as integer
122+ pub fn from_seconds ( sec : u32 ) -> ( u32 , u32 , u32 , u32 ) {
123+ let d = sec / ( 24 * 3600 ) ;
124+ let h = sec % ( 24 * 3600 ) / 3600 ;
125+ let min = sec % 3600 / 60 ;
126+ let sec = sec % 60 ;
127+
128+ ( d, h, min, sec)
129+ }
121130 /// Resets the state to be ready for a new download.
122131 fn prepare_for_new_download ( & mut self ) {
123132 self . content_len = None ;
@@ -186,27 +195,16 @@ impl fmt::Display for Duration {
186195 let sec = self . 0 ;
187196
188197 if sec. is_infinite ( ) {
189- write ! ( f, "Unknown" ) ;
198+ write ! ( f, "Unknown" )
190199 } else {
191- match from_seconds ( sec as u32 ) {
200+ match DownloadTracker :: from_seconds ( sec as u32 ) {
192201 ( d, h, m, s) if d > 0 => write ! ( f, "{:3.0}d {:2.0}h {:2.0}m {:2.0}s" , d, h, m, s) ,
193202 ( 0 , h, m, s) if h > 0 => write ! ( f, "{:2.0}h {:2.0}m {:2.0}s" , h, m, s) ,
194203 ( 0 , 0 , m, s) if m > 0 => write ! ( f, "{:2.0}m {:2.0}s" , m, s) ,
195204 ( _, _, _, s) => write ! ( f, "{:2.0}s" , s) ,
196205 }
197206 }
198207 }
199-
200- fn from_seconds ( sec : u32 ) -> ( u32 , u32 , u32 , u32 ) {
201- // we're doing modular arithmetic, treat as integer
202- let d = sec / ( 24 * 3600 ) ;
203- let h = sec % ( 24 * 3600 ) / 3600 ;
204- let min = sec % 3600 / 60 ;
205- let sec = sec % 60 ;
206-
207- ( d, h, min, sec)
208- }
209-
210208}
211209
212210/// Human readable size (bytes)
@@ -227,3 +225,42 @@ impl fmt::Display for Size {
227225 }
228226 }
229227}
228+
229+ #[ cfg( test) ]
230+ mod tests {
231+
232+ #[ test]
233+ fn download_tracker_from_seconds_test ( ) {
234+ use crate :: download_tracker:: DownloadTracker ;
235+ assert_eq ! (
236+ DownloadTracker :: from_seconds( 2 ) ,
237+ ( 0 , 0 , 0 , 2 )
238+ ) ;
239+
240+ assert_eq ! (
241+ DownloadTracker :: from_seconds( 60 ) ,
242+ ( 0 , 0 , 1 , 0 )
243+ ) ;
244+
245+ assert_eq ! (
246+ DownloadTracker :: from_seconds( 3600 ) ,
247+ ( 0 , 1 , 0 , 0 )
248+ ) ;
249+
250+ assert_eq ! (
251+ DownloadTracker :: from_seconds( 3600 * 24 ) ,
252+ ( 1 , 0 , 0 , 0 )
253+ ) ;
254+
255+ assert_eq ! (
256+ DownloadTracker :: from_seconds( 52292 ) ,
257+ ( 0 , 14 , 31 , 32 )
258+ ) ;
259+
260+ assert_eq ! (
261+ DownloadTracker :: from_seconds( 222292 ) ,
262+ ( 2 , 13 , 44 , 52 )
263+ ) ;
264+ }
265+
266+ }
0 commit comments