Skip to content

Commit

Permalink
issue #2 fix SAP_DATE parsing with separate method
Browse files Browse the repository at this point in the history
  • Loading branch information
gregor-j committed Feb 28, 2020
1 parent cd15786 commit ccf4bbf
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions src/SapDateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,27 @@ public static function createFromSapWeek($sapWeek, $timezone = null)
return new parent($week, $timezone);
}

/**
* Parse an SAP date string into a new DateTime object.
*
* @param string $sapDate String representing the SAP date.
* @param \DateTimeZone $timezone A DateTimeZone object representing the desired
* time zone.
* @return \DateTime|false
*/
public static function createFromSapDate($sapDate, $timezone = null)
{
if ($timezone === null) {
$result = parent::createFromFormat(static::SAP_DATE, $sapDate);
} else {
$result = parent::createFromFormat(static::SAP_DATE, $sapDate, $timezone);
}
if ($result !== false) {
$result->setTime(0, 0, 0);
}
return $result;
}

/**
* Parse a string into a new DateTime object according to the specified format.
*
Expand All @@ -87,13 +108,8 @@ public static function createFromFormat(
if ($format === static::SAP_WEEK) {
return static::createFromSapWeek($time, $timezone);
}
if ($format === static::SAP_DATE && $timezone === null) {
return parent::createFromFormat($format, $time)
->setTime(0, 0, 0);
}
if ($format === static::SAP_DATE) {
return parent::createFromFormat($format, $time, $timezone)
->setTime(0, 0, 0);
return static::createFromSapDate($time, $timezone);
}
if ($timezone === null) {
return parent::createFromFormat($format, $time);
Expand Down

0 comments on commit ccf4bbf

Please # to comment.