]> git.sur5r.net Git - bacula/bacula/blob - gui/bacula-web/includes/utils/utils.class.php
bacula-web: New class TimeUtils in utils.class.php module
[bacula/bacula] / gui / bacula-web / includes / utils / utils.class.php
1 <?php
2 /* 
3 +-------------------------------------------------------------------------+
4 | Copyright 2010-2011, Davide Franco                                              |
5 |                                                                         |
6 | This program is free software; you can redistribute it and/or           |
7 | modify it under the terms of the GNU General Public License             |
8 | as published by the Free Software Foundation; either version 2          |
9 | of the License, or (at your option) any later version.                  |
10 |                                                                         |
11 | This program is distributed in the hope that it will be useful,         |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of          |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           |
14 | GNU General Public License for more details.                            |
15 +-------------------------------------------------------------------------+ 
16 */
17 class Utils {
18         static public function Get_Human_Size( $size, $decimal = 2, $unit = 'auto' )
19         {
20                 $unit_id = 0;
21                 $lisible = false;
22                 $units = array('B','KB','MB','GB','TB');
23                 $hsize = $size;
24
25                 switch( $unit )
26                 {
27                         case 'auto';
28                                 while( !$lisible ) {
29                                         if ( $hsize >= 1024 ) {
30                                                 $hsize    = $hsize / 1024;
31                                                 $unit_id += 1;
32                                         }        
33                                         else
34                                                 $lisible = true;
35                                 } // end while
36                         break;
37                         
38                         default:
39                                 $p = array_search( $unit, $units);
40                                 $hsize = $hsize / pow(1024,$p);
41                         break;
42                 } // end switch
43                 
44                 $hsize = sprintf("%." . $decimal . "f", $hsize);
45                 $hsize = $hsize . ' ' . $units[$unit_id];
46                 return $hsize;
47         }
48 }
49
50 class TimeUtils {
51         static public function Get_Elapsed_Time( $start, $end)
52         {
53                 $diff = $end_time - $start_time;
54
55         $daysDiff = sprintf("%02d", floor($diff/60/60/24) );
56         $diff -= $daysDiff*60*60*24;
57
58         $hrsDiff = sprintf("%02d", floor($diff/60/60) );
59         $diff -= $hrsDiff*60*60;
60
61         $minsDiff = sprintf("%02d", floor($diff/60) );
62         $diff -= $minsDiff*60;
63         $secsDiff = sprintf("%02d", $diff );
64
65         if( $daysDiff > 0 )
66                         return $daysDiff . 'day(s) ' . $hrsDiff.':' . $minsDiff . ':' . $secsDiff;
67         else
68                         return $hrsDiff . ':' . $minsDiff . ':' . $secsDiff;
69         }
70 }
71
72 ?>