Index: openacs-4/packages/acs-tcl/tcl/utilities-procs.tcl =================================================================== RCS file: /usr/local/cvsroot/openacs-4/packages/acs-tcl/tcl/utilities-procs.tcl,v diff -u -N -r1.189.2.16 -r1.189.2.17 --- openacs-4/packages/acs-tcl/tcl/utilities-procs.tcl 16 Jun 2019 12:22:26 -0000 1.189.2.16 +++ openacs-4/packages/acs-tcl/tcl/utilities-procs.tcl 25 Jun 2019 12:33:32 -0000 1.189.2.17 @@ -3416,6 +3416,47 @@ return [lindex $list $idx] } +ad_proc -public util::content_size_pretty { + {-size "0"} + {-precision "1"} + {-decimal:boolean} +} { + Transforms data size, provided in non-negative bytes, to KB, MB... up to YB/Yib. + + @param size Size in bytes + @param precision Numbers in the fractional part + @param decimal Use powers of 10 instead of 2 + + @return Pretty size (e.g. '5.2 MB') + + @author Héctor Romojaro + @creation-date 2019-06-25 + +} { + if {$decimal_p} { + set div 1000 + set units [list B KB MB GB TB PB EB ZB YB] + } else { + set div 1024 + set units [list B KiB MiB GiB TiB PiB EiB ZiB YiB] + } + + if {$size eq ""} { + set size 0 + } + + set len [string length $size] + + if {$size < $div} { + set size_pretty [format "%s B" [lc_numeric $size]] + } else { + set unit [expr {($len - 1) / 3}] + set size_pretty [format "%.${precision}f %s" [lc_numeric [expr {$size / pow($div,$unit)}]] [lindex $units $unit]] + } + + return $size_pretty +} + ad_proc -public util::age_pretty { -timestamp_ansi:required -sysdate_ansi:required Index: openacs-4/packages/acs-tcl/tcl/test/acs-tcl-test-procs.tcl =================================================================== RCS file: /usr/local/cvsroot/openacs-4/packages/acs-tcl/tcl/test/acs-tcl-test-procs.tcl,v diff -u -N -r1.71.2.7 -r1.71.2.8 --- openacs-4/packages/acs-tcl/tcl/test/acs-tcl-test-procs.tcl 16 Apr 2019 18:18:20 -0000 1.71.2.7 +++ openacs-4/packages/acs-tcl/tcl/test/acs-tcl-test-procs.tcl 25 Jun 2019 12:33:32 -0000 1.71.2.8 @@ -1362,6 +1362,48 @@ {host openacs.org port 80 path www tail t.html} } +aa_register_case \ + -cats {api smoke production_safe} \ + -procs util::content_size_pretty \ + util__content_size_pretty { + + Test util::content_size_pretty proc + + @author Héctor Romojaro + @creation-date 2019-06-25 +} { + aa_equals "No arguments" [util::content_size_pretty] "[lc_numeric 0] B" + aa_equals "No arguments decimal" [util::content_size_pretty -decimal] "[lc_numeric 0] B" + aa_equals "Empty value" [util::content_size_pretty -size ""] "[lc_numeric 0] B" + aa_equals "Empty value decimal" [util::content_size_pretty -size "" -decimal] "[lc_numeric 0] B" + aa_equals "1.0 KiB" [util::content_size_pretty -size 1024] "[lc_numeric 1.0] KiB" + aa_equals "1.0 KB" [util::content_size_pretty -size 1000 -decimal] "[lc_numeric 1.0] KB" + aa_equals "1.0 MiB" [util::content_size_pretty -size 1048576] "[lc_numeric 1.0] MiB" + aa_equals "1.0 MB" [util::content_size_pretty -size 1000000 -decimal] "[lc_numeric 1.0] MB" + aa_equals "1.0 GiB" [util::content_size_pretty -size 1073741824] "[lc_numeric 1.0] GiB" + aa_equals "1.0 GB" [util::content_size_pretty -size 1000000000 -decimal] "[lc_numeric 1.0] GB" + aa_equals "1.0 TiB" [util::content_size_pretty -size 1099511627800] "[lc_numeric 1.0] TiB" + aa_equals "1.0 TB" [util::content_size_pretty -size 1000000000000 -decimal] "[lc_numeric 1.0] TB" + aa_equals "1.0 PiB" [util::content_size_pretty -size 1125899906842620] "[lc_numeric 1.0] PiB" + aa_equals "1.0 PB" [util::content_size_pretty -size 1000000000000000 -decimal] "[lc_numeric 1.0] PB" + aa_equals "1.0 EiB" [util::content_size_pretty -size 1152921504606850000] "[lc_numeric 1.0] EiB" + aa_equals "1.0 EB" [util::content_size_pretty -size 1000000000000000000 -decimal] "[lc_numeric 1.0] EB" + aa_equals "1.0 ZiB" [util::content_size_pretty -size 1180591620717410000000] "[lc_numeric 1.0] ZiB" + aa_equals "1.0 ZB" [util::content_size_pretty -size 1000000000000000000000 -decimal] "[lc_numeric 1.0] ZB" + aa_equals "1.0 YiB" [util::content_size_pretty -size 1208925819614630000000000] "[lc_numeric 1.0] YiB" + aa_equals "1.0 YB" [util::content_size_pretty -size 1000000000000000000000000 -decimal] "[lc_numeric 1.0] YB" + aa_equals "1.3 YiB" [util::content_size_pretty -size 1571603565499020000000000] "[lc_numeric 1.3] YiB" + aa_equals "1.3 YB" [util::content_size_pretty -size 1300000000000000000000000 -decimal] "[lc_numeric 1.3] YB" + aa_equals "1.000 KiB" [util::content_size_pretty -size 1024 -precision 3] "[lc_numeric 1.000] KiB" + aa_equals "1.000 KB" [util::content_size_pretty -size 1000 -precision 3 -decimal] "[lc_numeric 1.000] KB" + aa_equals "1 KiB" [util::content_size_pretty -size 1024 -precision 0] "[lc_numeric 1] KiB" + aa_equals "1 KB" [util::content_size_pretty -size 1000 -precision 0 -decimal] "[lc_numeric 1] KB" + aa_equals "1 KiB" [util::content_size_pretty -size 1044 -precision 0] "[lc_numeric 1] KiB" + aa_equals "1 KB" [util::content_size_pretty -size 1080 -precision 0 -decimal] "[lc_numeric 1] KB" + aa_equals "1.01953 -> 1.020 KiB" [util::content_size_pretty -size 1044 -precision 3] "[lc_numeric 1.020] KiB" + aa_equals "1.080 KB" [util::content_size_pretty -size 1080 -precision 3 -decimal] "[lc_numeric 1.080] KB" +} + # Local variables: # mode: tcl # tcl-indent-level: 4