examine_diff.inc 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. /**
  3. * Helper for the DateTime diff/days/math tests
  4. *
  5. * @author Daniel Convissor <danielc@analysisandsolutions.com>
  6. */
  7. /**#@+
  8. * Which test segments should be displayed?
  9. */
  10. define('PHPT_DATETIME_SHOW_DIFF', 1);
  11. define('PHPT_DATETIME_SHOW_DAYS', 2);
  12. define('PHPT_DATETIME_SHOW_ADD', 3);
  13. define('PHPT_DATETIME_SHOW_SUB', 4);
  14. /**#@-*/
  15. /**
  16. * Provides a consistent interface for executing date diff/add/sub tests
  17. *
  18. * @param string|DateTime $end_date the end date in YYYY-MM-DD format
  19. * (can include time HH:MM:SS) or a DateTime object
  20. * @param string|DateTime $start_date the start date in YYYY-MM-DD format
  21. * (can include time HH:MM:SS) or a DateTime object
  22. * @param string $expect_spec the expected result of the tests, in the
  23. * special interval specification used for this test suite.
  24. * This spec includes a "+" or "-" after the "P" in order to
  25. * indicate which direction to go.
  26. * @param int $expect_days the number of days to compare with the
  27. * interval's "days" property
  28. * @param bool $absolute should the result always be a positive number?
  29. *
  30. * @return void
  31. */
  32. function examine_diff($end_date, $start_date, $expect_spec, $expect_days, $absolute = false) {
  33. if (is_string($start_date)) {
  34. $start = new DateTime($start_date);
  35. } else {
  36. $start = $start_date;
  37. }
  38. $start_date = $start->format('Y-m-d H:i:s T');
  39. if (is_string($end_date)) {
  40. $end = new DateTime($end_date);
  41. } else {
  42. $end = $end_date;
  43. }
  44. $end_date = $end->format('Y-m-d H:i:s T');
  45. $expect_interval = new DateInterval('P' . substr($expect_spec, 2));
  46. if (substr($expect_spec, 1, 1) == '-') {
  47. $expect_interval->invert = true;
  48. }
  49. if (PHPT_DATETIME_SHOW == PHPT_DATETIME_SHOW_DIFF) {
  50. $result_interval = $start->diff($end, $absolute);
  51. $result_spec = $result_interval->format('P%R%yY%mM%dDT%hH%iM%sS');
  52. echo "DIFF: $end_date - $start_date = **$result_spec**\n";
  53. // echo "DIFF: $end_date - $start_date = **$expect_spec**\n";
  54. }
  55. if (PHPT_DATETIME_SHOW == PHPT_DATETIME_SHOW_DAYS) {
  56. $result_interval = $start->diff($end, $absolute);
  57. $result_days = $result_interval->format('%a');
  58. echo "DAYS: **$result_days**\n";
  59. // echo "DAYS: **$expect_days**\n";
  60. }
  61. if (PHPT_DATETIME_SHOW == PHPT_DATETIME_SHOW_ADD) {
  62. $start->add($expect_interval);
  63. $result_end_date = $start->format('Y-m-d H:i:s T');
  64. echo "ADD: $start_date + $expect_spec = **$result_end_date**\n";
  65. // echo "ADD: $start_date + $expect_spec = **$end_date**\n";
  66. }
  67. if (PHPT_DATETIME_SHOW == PHPT_DATETIME_SHOW_SUB) {
  68. $end->sub($expect_interval);
  69. $result_start_date = $end->format('Y-m-d H:i:s T');
  70. echo "SUB: $end_date - $expect_spec = **$result_start_date**\n";
  71. // echo "SUB: $end_date - $expect_spec = **$start_date**\n";
  72. }
  73. }