Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add format microtime() to DateTimeInterface::RFC3339_EXTENDED function #3928

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions helpers/class.Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -283,4 +283,29 @@ public static function getTimeKeys(\DateInterval $interval, \DateTimeInterface $
}
return $timeKeys;
}

/**
* Converts from microseconds seconds format of microtime() function to RFC3339_EXTENDED
* @param string|null $microtime
* @return string|null
* @example 0.47950700 1700135696 to 1700135696.47950700
*/
public static function formatMicrotime(?string $microtime): ?string
{
if ($microtime === null) {
return null;
}

// Split the string into microseconds and seconds
list($microseconds, $seconds) = explode(' ', $microtime);

// Show only the numbers after the dot without the integral part
list(, $decimalPart) = explode('.', sprintf('%0.6f', $microseconds));
//To preserve time zone we are not using createFromFormat()
$date = new DateTime();
$date->setTimestamp((int)$seconds);
$date->modify('+ ' . $decimalPart . ' microseconds');

return $date->format(DateTimeInterface::RFC3339_EXTENDED);
}
}
50 changes: 50 additions & 0 deletions test/unit/helpers/DateTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

/**
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; under version 2
* of the License (non-upgradable).
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Copyright (c) 2023 (original work) Open Assessment Technologies SA;
*/

declare(strict_types=1);

namespace oat\tao\test\unit\helpers;

use oat\generis\test\TestCase;
use tao_helpers_Date as DateHelper;

class DateTest extends TestCase
{
/**
* @dataProvider microtimeProvider
*/
public function testMicrotimeFormat(?string $microtime, ?string $expected)
{
$result = DateHelper::formatMicrotime($microtime);
$this->assertEquals($expected, $result);
}

public function microtimeProvider()
{
return [
['microtime' => '0.51487900 1701076034', 'expected' => '2023-11-27T09:07:14.514+00:00'],
['microtime' => '0.81487900 1701078054', 'expected' => '2023-11-27T09:40:54.814+00:00'],
['microtime' => '0.71487900 1701073054', 'expected' => '2023-11-27T08:17:34.714+00:00'],
['microtime' => '0.0 1701073054', 'expected' => '2023-11-27T08:17:34.000+00:00'],
['microtime' => '0 1701073054', 'expected' => '2023-11-27T08:17:34.000+00:00'],
['microtime' => null, 'expected' => null]
];
}
}
Loading