PageRenderTime 60ms CodeModel.GetById 36ms RepoModel.GetById 0ms app.codeStats 0ms

/xandra.org/www/modules/codebench/classes/Bench/DateSpan.php

https://bitbucket.org/ekkl/tanora
PHP | 186 lines | 118 code | 39 blank | 29 comment | 22 complexity | 34fbaf1a855ef992a8f624d9b54d471f MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause
  1. <?php defined('SYSPATH') or die('No direct access allowed.');
  2. /**
  3. * @package Kohana/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 = array();
  12. public function __construct()
  13. {
  14. parent::__construct();
  15. $this->subjects = array(
  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(array('deny', 'key', 'difference', 'output'));
  72. // Return the difference
  73. $difference = array();
  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. }