Skip to content
This repository has been archived by the owner on Apr 11, 2018. It is now read-only.

Commit

Permalink
Added ElasticSearch DateTime format
Browse files Browse the repository at this point in the history
  • Loading branch information
eddiejaoude committed Sep 18, 2014
1 parent c6a87df commit 7fb0274
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
19 changes: 19 additions & 0 deletions src/Opg/Common/Model/Entity/DateFormat.php
Expand Up @@ -17,6 +17,8 @@ final class DateFormat

const REGEXP_MYSQL_DATE_TIME = '/[0-9]{4}-[0-9]{1,2}-[0-9]{1,2} [0-9]{1,2}:[0-9]{1,2}:[0-9]{1,2}$/';

const REGEXP_ElASTICSEARCH_DATE_TIME = '/[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}T[0-9]{1,2}:[0-9]{1,2}:[0-9]{1,2}$/';

This comment has been minimized.

Copy link
@brettminnie

brettminnie Sep 18, 2014

Contributor

Could we not rather make the T optional and then just reuse the REGEXP_MYSQL_DATE_TIME , something like
'/[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}(\s|T)[0-9]{1,2}:[0-9]{1,2}:[0-9]{1,2}$/

Haven't tested if this works but it should :)

This comment has been minimized.

Copy link
@eddiejaoude

eddiejaoude Sep 19, 2014

Author Contributor

Unfortunately that didn't work. I tried a few other combinations with no luck, including..

  • (\ |T)
  • ?T?

const REGEXP_BANKTEC_FORMAT = '/^\d{8}$/';
/**
* @var string
Expand All @@ -33,6 +35,11 @@ final class DateFormat
*/
protected static $DateTimeSqlExport = 'Y-m-d H:i:s';

/**
* @var string
*/
protected static $DateTimeElasticSearch = 'Y-m-d\TH:i:s';

/**
* @var string
*/
Expand Down Expand Up @@ -62,6 +69,14 @@ public static function getSqlDateTimeFormat()
return self::$DateTimeSqlExport;
}

/**
* @return string
*/
public static function getElasticSearchDateTimeFormat()
{
return self::$DateTimeElasticSearch;
}

/**
* @return string
*/
Expand Down Expand Up @@ -95,6 +110,10 @@ public static function createDateTime($strDateTime)
return \DateTime::createFromFormat(self::getBanktecFormat(), $strDateTime);
}

if(preg_match(self::REGEXP_ElASTICSEARCH_DATE_TIME, trim($strDateTime))) {
return \DateTime::createFromFormat(self::getElasticSearchDateTimeFormat(), $strDateTime);
}

throw new InvalidDateFormatException(
"'{$strDateTime}' was not in the expected format " . self::getDateTimeFormat()
);
Expand Down
2 changes: 1 addition & 1 deletion src/Opg/Common/Model/Entity/Traits/Time.php
Expand Up @@ -65,7 +65,7 @@ public function getCreatedTimeString()
public function setCreatedTimeString($createdTime)
{
if (empty($createdTime)) {
$createdTime = null;
return $this->setCreatedTime();

This comment has been minimized.

Copy link
@brettminnie

brettminnie Sep 18, 2014

Contributor

👍 good spot

}

return $this->setCreatedTime(OPGDateFormat::createDateTime($createdTime));
Expand Down

1 comment on commit 7fb0274

@eddiejaoude
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@brettminnie Added this to fix build for the changes src/Opg/Common/Model/Entity/Traits/Time.php:71

return $this->setCreatedTime(OPGDateFormat::createDateTime($createdTime));

Please sign in to comment.