DateSpan.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. /**
  3. * @package koseven/Codebench
  4. * @category Tests
  5. * @author Woody Gilk <woody.gilk@kohanaphp.com>
  6. */
  7. class Bench_DateSpan extends Codebench {
  8. public $description =
  9. 'Optimization for <code>Date::span()</code>.';
  10. public $loops = 1000;
  11. public $subjects = [];
  12. public function __construct()
  13. {
  14. parent::__construct();
  15. $this->subjects = [
  16. time(),
  17. time() - Date::MONTH,
  18. time() - Date::YEAR,
  19. time() - Date::YEAR * 10,
  20. ];
  21. }
  22. // Original method
  23. public static function bench_span_original($remote, $local = NULL, $output = 'years,months,weeks,days,hours,minutes,seconds')
  24. {
  25. // Array with the output formats
  26. $output = preg_split('/[^a-z]+/', strtolower( (string) $output));
  27. // Invalid output
  28. if (empty($output))
  29. return FALSE;
  30. // Make the output values into keys
  31. extract(array_flip($output), EXTR_SKIP);
  32. if ($local === NULL)
  33. {
  34. // Calculate the span from the current time
  35. $local = time();
  36. }
  37. // Calculate timespan (seconds)
  38. $timespan = abs($remote - $local);
  39. if (isset($years))
  40. {
  41. $timespan -= Date::YEAR * ($years = (int) floor($timespan / Date::YEAR));
  42. }
  43. if (isset($months))
  44. {
  45. $timespan -= Date::MONTH * ($months = (int) floor($timespan / Date::MONTH));
  46. }
  47. if (isset($weeks))
  48. {
  49. $timespan -= Date::WEEK * ($weeks = (int) floor($timespan / Date::WEEK));
  50. }
  51. if (isset($days))
  52. {
  53. $timespan -= Date::DAY * ($days = (int) floor($timespan / Date::DAY));
  54. }
  55. if (isset($hours))
  56. {
  57. $timespan -= Date::HOUR * ($hours = (int) floor($timespan / Date::HOUR));
  58. }
  59. if (isset($minutes))
  60. {
  61. $timespan -= Date::MINUTE * ($minutes = (int) floor($timespan / Date::MINUTE));
  62. }
  63. // Seconds ago, 1
  64. if (isset($seconds))
  65. {
  66. $seconds = $timespan;
  67. }
  68. // Remove the variables that cannot be accessed
  69. unset($timespan, $remote, $local);
  70. // Deny access to these variables
  71. $deny = array_flip(['deny', 'key', 'difference', 'output']);
  72. // Return the difference
  73. $difference = [];
  74. foreach ($output as $key)
  75. {
  76. if (isset($$key) AND ! isset($deny[$key]))
  77. {
  78. // Add requested key to the output
  79. $difference[$key] = $$key;
  80. }
  81. }
  82. // Invalid output formats string
  83. if (empty($difference))
  84. return FALSE;
  85. // If only one output format was asked, don't put it in an array
  86. if (count($difference) === 1)
  87. return current($difference);
  88. // Return array
  89. return $difference;
  90. }
  91. // Using an array for the output
  92. public static function bench_span_use_array($remote, $local = NULL, $output = 'years,months,weeks,days,hours,minutes,seconds')
  93. {
  94. // Array with the output formats
  95. $output = preg_split('/[^a-z]+/', strtolower( (string) $output));
  96. // Invalid output
  97. if (empty($output))
  98. return FALSE;
  99. // Convert the list of outputs to an associative array
  100. $output = array_combine($output, array_fill(0, count($output), 0));
  101. // Make the output values into keys
  102. extract(array_flip($output), EXTR_SKIP);
  103. if ($local === NULL)
  104. {
  105. // Calculate the span from the current time
  106. $local = time();
  107. }
  108. // Calculate timespan (seconds)
  109. $timespan = abs($remote - $local);
  110. if (isset($output['years']))
  111. {
  112. $timespan -= Date::YEAR * ($output['years'] = (int) floor($timespan / Date::YEAR));
  113. }
  114. if (isset($output['months']))
  115. {
  116. $timespan -= Date::MONTH * ($output['months'] = (int) floor($timespan / Date::MONTH));
  117. }
  118. if (isset($output['weeks']))
  119. {
  120. $timespan -= Date::WEEK * ($output['weeks'] = (int) floor($timespan / Date::WEEK));
  121. }
  122. if (isset($output['days']))
  123. {
  124. $timespan -= Date::DAY * ($output['days'] = (int) floor($timespan / Date::DAY));
  125. }
  126. if (isset($output['hours']))
  127. {
  128. $timespan -= Date::HOUR * ($output['hours'] = (int) floor($timespan / Date::HOUR));
  129. }
  130. if (isset($output['minutes']))
  131. {
  132. $timespan -= Date::MINUTE * ($output['minutes'] = (int) floor($timespan / Date::MINUTE));
  133. }
  134. // Seconds ago, 1
  135. if (isset($output['seconds']))
  136. {
  137. $output['seconds'] = $timespan;
  138. }
  139. if (count($output) === 1)
  140. {
  141. // Only a single output was requested, return it
  142. return array_pop($output);
  143. }
  144. // Return array
  145. return $output;
  146. }
  147. }