2 *** This file Copyright (C) Mnemosyne LLC
4 *** This code is licensed under the GPL version 2.
5 *** For more details, see http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
8 Transmission.fmt = (function()
11 var speed_B_str = 'B';
12 var speed_K_str = 'KB/s';
13 var speed_M_str = 'MB/s';
14 var speed_G_str = 'GB/s';
15 var speed_T_str = 'TB/s';
19 var size_K_str = 'KB';
20 var size_M_str = 'MB';
21 var size_G_str = 'GB';
22 var size_T_str = 'TB';
33 updateUnits: function( u )
35 speed_K = u['speed-bytes'];
36 speed_K_str = u['speed-units'][0];
37 speed_M_str = u['speed-units'][1];
38 speed_G_str = u['speed-units'][2];
39 speed_T_str = u['speed-units'][3];
41 size_K = u['size-bytes'];
42 size_K_str = u['size-units'][0];
43 size_M_str = u['size-units'][1];
44 size_G_str = u['size-units'][2];
45 size_T_str = u['size-units'][3];
47 mem_K = u['memory-bytes'];
48 mem_K_str = u['memory-units'][0];
49 mem_M_str = u['memory-units'][1];
50 mem_G_str = u['memory-units'][2];
51 mem_T_str = u['memory-units'][3];
55 * Format a percentage to a string
57 percentString: function( x ) {
59 return x.toTruncFixed( 2 );
61 return x.toTruncFixed( 1 );
63 return x.toTruncFixed( 0 );
67 * Format a ratio to a string
69 ratioString: function( x ) {
75 return this.percentString( x );
79 * Formats the a memory size into a human-readable string
80 * @param {Number} bytes the filesize in bytes
81 * @return {String} human-readable string
83 mem: function( bytes )
86 return [ bytes, mem_B_str ].join(' ');
91 if( bytes < Math.pow( mem_K, 2 ) )
93 convertedSize = bytes / mem_K;
96 else if( bytes < Math.pow( mem_K, 3 ) )
98 convertedSize = bytes / Math.pow( mem_K, 2 );
101 else if( bytes < Math.pow( mem_K, 4 ) )
103 convertedSize = bytes / Math.pow( mem_K, 3 );
108 convertedSize = bytes / Math.pow( mem_K, 4 );
112 // try to have at least 3 digits and at least 1 decimal
113 return convertedSize <= 9.995 ? [ convertedSize.toTruncFixed(2), unit ].join(' ')
114 : [ convertedSize.toTruncFixed(1), unit ].join(' ');
118 * Formats the a disk capacity or file size into a human-readable string
119 * @param {Number} bytes the filesize in bytes
120 * @return {String} human-readable string
122 size: function( bytes )
125 return [ bytes, size_B_str ].join(' ');
130 if( bytes < Math.pow( size_K, 2 ) )
132 convertedSize = bytes / size_K;
135 else if( bytes < Math.pow( size_K, 3 ) )
137 convertedSize = bytes / Math.pow( size_K, 2 );
140 else if( bytes < Math.pow( size_K, 4 ) )
142 convertedSize = bytes / Math.pow( size_K, 3 );
147 convertedSize = bytes / Math.pow( size_K, 4 );
151 // try to have at least 3 digits and at least 1 decimal
152 return convertedSize <= 9.995 ? [ convertedSize.toTruncFixed(2), unit ].join(' ')
153 : [ convertedSize.toTruncFixed(1), unit ].join(' ');
156 speedBps: function( Bps )
158 return this.speed( this.toKBps( Bps ) );
161 toKBps: function( Bps )
163 return Math.floor( Bps / speed_K );
166 speed: function( KBps )
170 if (speed <= 999.95) // 0 KBps to 999.9 K
171 return [ speed.toTruncFixed(1), speed_K_str ].join(' ');
175 if (speed <= 99.995) // 1 M to 99.99 M
176 return [ speed.toTruncFixed(2), speed_M_str ].join(' ');
177 if (speed <= 999.95) // 100 M to 999.9 M
178 return [ speed.toTruncFixed(1), speed_M_str ].join(' ');
182 return [ speed.toTruncFixed(2), speed_G_str ].join(' ');
185 timeInterval: function( seconds )
188 var days = Math.floor(seconds / 86400);
189 var hours = Math.floor((seconds % 86400) / 3600);
190 var minutes = Math.floor((seconds % 3600) / 60);
191 var seconds = Math.floor((seconds % 3600) % 60);
193 if (days > 0 && hours == 0)
194 result = [ days, 'days' ];
195 else if (days > 0 && hours > 0)
196 result = [ days, 'days', hours, 'hr' ];
197 else if (hours > 0 && minutes == 0)
198 result = [ hours, 'hr' ];
199 else if (hours > 0 && minutes > 0)
200 result = [ hours,'hr', minutes, 'min' ];
201 else if (minutes > 0 && seconds == 0)
202 result = [ minutes, 'min' ];
203 else if (minutes > 0 && seconds > 0)
204 result = [ minutes, 'min', seconds, 'seconds' ];
206 result = [ seconds, 'seconds' ];
208 return result.join(' ');
211 timestamp: function( seconds )
216 var myDate = new Date(seconds*1000);
217 var now = new Date();
222 var sameYear = now.getFullYear() == myDate.getFullYear();
223 var sameMonth = now.getMonth() == myDate.getMonth();
225 var dateDiff = now.getDate() - myDate.getDate();
226 if(sameYear && sameMonth && Math.abs(dateDiff) <= 1){
230 else if(dateDiff == 1){
238 date = myDate.toDateString();
241 var hours = myDate.getHours();
253 var minutes = myDate.getMinutes();
255 minutes = "0" + minutes;
257 var seconds = myDate.getSeconds();
259 seconds = "0" + seconds;
262 time = [hours, minutes, seconds].join(':');
264 return [date, time, period].join(' ');
267 plural: function( i, word )
269 return [ i, ' ', word, (word==1?'':'s') ].join('');