Skip to content

Commit 5f66897

Browse files
committed
add first version of interval class + test
1 parent 1eb24e3 commit 5f66897

File tree

2 files changed

+106
-0
lines changed

2 files changed

+106
-0
lines changed

src/SapDateInterval.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
/**
3+
* File src/SapDateInterval.php
4+
*
5+
* SAP time to interval conversions for times > 24h.
6+
*
7+
* @package datetime
8+
* @author Gregor J.
9+
* @license MIT
10+
*/
11+
12+
namespace phpsap\DateTime;
13+
14+
use DateInterval;
15+
16+
/**
17+
* Class SapDateInterval
18+
*
19+
* Helps converting SAP time values to intervals for times greater than 24 hours.
20+
*
21+
* @package phpsap\DateTime
22+
* @author Gregor J.
23+
* @license MIT
24+
*/
25+
class SapDateInterval extends DateInterval
26+
{
27+
/**
28+
* @var string SAP time format for DateInterval
29+
*/
30+
const SAP_TIME = '%H%I%S';
31+
32+
/**
33+
* Sets up a DateInterval from the relative parts of the string
34+
* @param string $time
35+
* @return \DateInterval
36+
* @throws \Exception
37+
* @link https://php.net/manual/en/dateinterval.createfromdatestring.php
38+
*/
39+
public static function createFromDateString($time)
40+
{
41+
$matches = [];
42+
if (preg_match('~^([\d]{2})([0-5][\d])([0-5][\d])$~', $time, $matches)) {
43+
return new parent(sprintf(
44+
'PT%sH%sM%sS',
45+
$matches[1],
46+
$matches[2],
47+
$matches[3]
48+
));
49+
}
50+
return parent::createFromDateString($time);
51+
}
52+
}

tests/SapDateIntervalTest.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
/**
3+
* File tests/SapDateIntervalTest.php
4+
*
5+
* Test SapDateInterval class.
6+
*
7+
* @package datetime
8+
* @author Gregor J.
9+
* @license MIT
10+
*/
11+
12+
namespace tests\phpsap\DateTime;
13+
14+
use phpsap\DateTime\SapDateInterval;
15+
16+
/**
17+
* Class SapDateIntervalTest
18+
*
19+
* Unit tests for the SapDateTime class.
20+
*
21+
* @package tests\phpsap\DateTime
22+
* @author Gregor J.
23+
* @license MIT
24+
*/
25+
class SapDateIntervalTest extends \PHPUnit_Framework_TestCase
26+
{
27+
/**
28+
* Data provider for valid time return values from SAP.
29+
* @return array
30+
*/
31+
public static function provideValidTimes()
32+
{
33+
return [
34+
['010203', '2020-01-31 00:00:00', '2020-01-31 01:02:03'],
35+
['263052', '2020-01-31 00:00:00', '2020-02-01 02:30:52'],
36+
];
37+
}
38+
39+
/**
40+
* Test converting valid times from SAP to an ISO date and time after 2020-01-31.
41+
* @param string $sapTime
42+
* @param string $startDate
43+
* @param string $expected
44+
* @throws \Exception
45+
* @dataProvider provideValidTimes
46+
*/
47+
public function testValidTimes($sapTime, $startDate, $expected)
48+
{
49+
$dateTime = new \DateTime($startDate);
50+
$time = SapDateInterval::createFromDateString($sapTime);
51+
$dateTime->add($time);
52+
static::assertSame($expected, $dateTime->format('Y-m-d H:i:s'));
53+
}
54+
}

0 commit comments

Comments
 (0)