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