NumberLib.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <?php
  2. //TODO: rename to TimeLib or move the time stuff to a time lib???!!!
  3. /**
  4. * 2011-03-07 ms
  5. */
  6. class NumberLib {
  7. /**
  8. * @access public
  9. * @param float $number
  10. * @param float $increment
  11. * @return float $result
  12. * 2011-04-14 lb
  13. */
  14. public static function roundTo($number, $increments = 1.0) {
  15. return round($number, self::getDecimalPlaces($increments));
  16. }
  17. /**
  18. * @access public
  19. * @param float $number
  20. * @param int $increment
  21. * @return float $result
  22. * 2011-04-14 lb
  23. */
  24. public static function roundUpTo($number, $increments = 1) {
  25. return (ceil($number / $increments) * $increments);
  26. }
  27. /**
  28. * @access public
  29. * @param float $number
  30. * @param int $increment
  31. * @return float $result
  32. * 2011-04-14 lb
  33. */
  34. public static function roundDownTo($number, $increments = 1) {
  35. return (floor($number / $increments) * $increments);
  36. }
  37. /**
  38. * @access public
  39. * @param float $number
  40. * @return int $decimalPlaces
  41. * 2011-04-15 lb
  42. */
  43. public static function getDecimalPlaces($number) {
  44. $decimalPlaces = 0;
  45. while ($number > 1 && $number != 0) {
  46. $number /= 10;
  47. $decimalPlaces -= 1;
  48. }
  49. while ($number < 1 && $number != 0) {
  50. $number *= 10;
  51. $decimalPlaces += 1;
  52. }
  53. return $decimalPlaces;
  54. }
  55. /**
  56. * hours, minutes
  57. * e.g. 9.3 => 9.5
  58. * 2010-11-03 ms
  59. */
  60. public static function standardToDecimalTime($value) {
  61. $base = (int)$value;
  62. $tmp = $value-$base;
  63. $tmp *= 100;
  64. $tmp *= 1/60;
  65. $value = $base+$tmp;
  66. return $value;
  67. }
  68. /**
  69. * hours, minutes
  70. * e.g. 9.5 => 9.3
  71. * with pad=2: 9.30
  72. * 2010-11-03 ms
  73. */
  74. public static function decimalToStandardTime($value, $pad = null, $decPoint = '.') {
  75. $base = (int)$value;
  76. $tmp = $value-$base;
  77. $tmp /= 1/60;
  78. $tmp /= 100;
  79. $value = $base+$tmp;
  80. if ($pad === null) {
  81. return $value;
  82. }
  83. return number_format($value, $pad, $decPoint, '');
  84. }
  85. /**
  86. * parse 2,5 - 2.5 2:30 2:31:58 or even 2011-11-12 10:10:10
  87. * now supports negative values like -2,5 -2,5 -2:30 -:30 or -4
  88. * @param string
  89. * @return int: seconds
  90. * 2011-03-06 ms
  91. */
  92. public static function parseTime($duration, $allowed = array(':', '.', ',')) {
  93. if (empty($duration)) {
  94. return 0;
  95. }
  96. $parts = explode(' ', $duration);
  97. $duration = array_pop($parts);
  98. if (strpos($duration, '.') !== false && in_array('.', $allowed)) {
  99. App::uses('NumberLib', 'Tools.Lib');
  100. //$numberLib = new NumberLib();
  101. $duration = NumberLib::decimalToStandardTime($duration, 2, ':');
  102. } elseif (strpos($duration, ',') !== false && in_array(',', $allowed)) {
  103. App::uses('NumberLib', 'Tools.Lib');
  104. $duration = str_replace(',', '.', $duration);
  105. $duration = NumberLib::decimalToStandardTime($duration, 2, ':');
  106. }
  107. # now there is only the time schema left...
  108. $pieces = explode(':', $duration, 3);
  109. $res = 0;
  110. $hours = abs((int)$pieces[0])*HOUR;
  111. //echo pre($hours);
  112. $isNegative = (strpos((string)$pieces[0], '-') !== false ? true : false);
  113. if (count($pieces) === 3) {
  114. $res += $hours + ((int)$pieces[1])*MINUTE + ((int)$pieces[2])*SECOND;
  115. } elseif (count($pieces) === 2) {
  116. $res += $hours + ((int)$pieces[1])*MINUTE;
  117. } else {
  118. $res += $hours;
  119. }
  120. if ($isNegative) {
  121. return -$res;
  122. }
  123. return $res;
  124. }
  125. /**
  126. * parse 2022-11-12 or 12.11.2022 or even 12.11.22
  127. * @param string $date
  128. * @return int: seconds
  129. * 2011-03-09 ms
  130. */
  131. public static function parseDate($date, $allowed = array('.', '-')) {
  132. $datePieces = explode(' ', $date, 2);
  133. $date = array_shift($datePieces);
  134. if (strpos($date, '.') !== false) {
  135. $pieces = explode('.', $date);
  136. $year = $pieces[2];
  137. if (strlen($year) === 2) {
  138. if ($year < 50) {
  139. $year = '20'.$year;
  140. } else {
  141. $year = '19'.$year;
  142. }
  143. }
  144. $date = mktime(0, 0, 0, $pieces[1], $pieces[0], $year);
  145. } elseif(strpos($date, '-') !== false) {
  146. //$pieces = explode('-', $date);
  147. $date = strtotime($date);
  148. } else {
  149. return 0;
  150. }
  151. return $date;
  152. }
  153. /**
  154. * return strings like 2:30 (later //TODO: or 2:33:99) from seconds etc
  155. * @param int: seconds
  156. * @return string
  157. * 2011-03-06 ms
  158. */
  159. public static function buildTime($duration, $mode = 'H:MM') {
  160. if ($duration < 0) {
  161. $duration = abs($duration);
  162. $isNegative = true;
  163. }
  164. $minutes = $duration%HOUR;
  165. $hours = ($duration - $minutes)/HOUR;
  166. $res = (int)$hours.':'.str_pad(intval($minutes/MINUTE), 2, '0', STR_PAD_LEFT);
  167. if (strpos($mode, 'SS') !== false) {
  168. //TODO
  169. }
  170. if (!empty($isNegative)) {
  171. $res = '-'.$res;
  172. }
  173. return $res;
  174. }
  175. /**
  176. * return strings like 2:33:99 from seconds etc
  177. * @param int: seconds
  178. * @return string
  179. * 2011-03-09 ms
  180. */
  181. public static function buildDefaultTime($duration) {
  182. $minutes = $duration%HOUR;
  183. $duration = $duration - $minutes;
  184. $hours = ($duration)/HOUR;
  185. //$duration = $minutes*MINUTE;
  186. $seconds = $minutes%MINUTE;
  187. return self::pad($hours).':'.self::pad($minutes/MINUTE).':'.self::pad($seconds/SECOND);
  188. }
  189. public static function pad($value, $length = 2) {
  190. return str_pad(intval($value), $length, '0', STR_PAD_LEFT);
  191. }
  192. }