3 * @copyright Copyright (C) 2010-2022, the Friendica project
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Test\src\Util;
24 use Friendica\Test\MockedTest;
25 use Friendica\Util\DateTimeFormat;
27 class DateTimeFormatTest extends MockedTest
29 public function dataYearMonth()
36 'validOneCharMonth' => [
40 'validTwoCharMonth' => [
72 * @dataProvider dataYearMonth
74 public function testIsYearMonth(string $input, bool $assert)
76 $dtFormat = new DateTimeFormat();
78 self::assertEquals($assert, $dtFormat->isYearMonth($input));
82 * Test the DateTimeFormat::API output.
86 public function testApiDate()
88 self::assertEquals('Wed Oct 10 00:00:00 +0000 1990', DateTimeFormat::utc('1990-10-10', DateTimeFormat::API));
91 public function dataFix(): array
94 'Mo, 19 Sep 2022 14:51:00 +0200' => [
95 'expectedDate' => '2022-09-19T14:51:00+02:00',
96 'dateString' => 'Mo, 19 Sep 2022 14:51:00 +0200',
98 '2020-11-21T12:00:13.745339ZZ' => [
99 'expectedDate' => '2020-11-21T12:00:13+00:00',
100 'dateString' => '2020-11-21T12:00:13.745339ZZ',
102 '2016-09-09T13:32:00ZZ' => [
103 'expectedDate' => '2016-09-09T13:32:00+00:00',
104 'dateString' => '2016-09-09T13:32:00ZZ',
106 'Sun, 10/03/2021 - 12:41' => [
107 'expectedDate' => '2021-10-03T12:41:00+00:00',
108 'dateString' => 'Sun, 10/03/2021 - 12:41',
110 '4:30 PM, Sep 13, 2022' => [
111 'expectedDate' => '2022-09-13T16:30:00+00:00',
112 'dateString' => '4:30 PM, Sep 13, 2022',
114 'August 27, 2022 - 21:00' => [
115 'expectedDate' => '2022-08-27T21:00:00+00:00',
116 'dateString' => 'August 27, 2022 - 21:00',
118 '2021-09-19T14:06:03+00:00' => [
119 'expectedDate' => '2021-09-19T14:06:03+00:00',
120 'dateString' => '2021-09-19T14:06:03+00:00',
122 'Eastern Time timezone' => [
123 'expectedDate' => '2022-09-30T00:00:00-05:00',
124 'dateString' => 'September 30, 2022, 12:00 a.m. ET',
126 'German date time string' => [
127 'expectedDate' => '2022-10-05T16:34:00+02:00',
128 'dateString' => '05 Okt 2022 16:34:00 +0200',
134 * @dataProvider dataFix
136 * @param $expectedDate
141 public function testFix($expectedDate, $dateString)
143 $fixed = DateTimeFormat::fix($dateString);
145 $this->assertEquals($expectedDate, (new \DateTime($fixed))->format('c'));
149 * This test is meant to ensure DateTimeFormat::fix() isn't called on relative date/time strings
154 public function testConvertRelative()
156 $now = DateTimeFormat::utcNow('U');
157 $date = DateTimeFormat::utc('now - 3 days', 'U');
159 $this->assertEquals(259200, $now - $date);