Skip to content

Commit

Permalink
added added date to rfc3339 tests and implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Rico Kaltofen committed Feb 1, 2018
1 parent d643c52 commit 711b2d4
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 12 deletions.
39 changes: 35 additions & 4 deletions src/Date/DateUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,21 +50,52 @@ public function getTimePeriodInSeconds($timePeriod)
*
* @param string $format The php date format (see: http://php.net/manual/de/function.date.php#refsect1-function.date-parameters)
*
* @return string The RFC3339 compliant format (see: http://userguide.icu-project.org/formatparse/datetime)
* @return string The RFC3339 compliant format (see: http://www.unicode.org/reports/tr35/tr35-dates.html#Date_Field_Symbol_Table)
*/
public function formatPhpDateToRFC3339(string $format): string
public function transformPhpDateFormatToRFC3339(string $format): string
{
$mapping = [
'd' => 'dd', //Day of the month, 2 digits with leading zeros (01 to 31)
'D' => 'E', // A textual representation of a day, three letters (Mon through Sun)
'j' => 'd', // Day of the month without leading zeros (1 to 31)
'l' => 'EEEE', // A full textual representation of the day of the week (Sunday through Saturday)
'N' => 'd', // ISO-8601 numeric representation of the day of the week (added in PHP 5.1.0) (1 (for Monday) through 7 (for Sunday))
'S' => '', // English ordinal suffix for the day of the month, 2 characters (st, nd, rd or th. Works well with j)
'S' => '', // Not supported yet: English ordinal suffix for the day of the month, 2 characters (st, nd, rd or th. Works well with j)
'w' => 'e', // Numeric representation of the day of the week (0 (for Sunday) through 6 (for Saturday))
'z' => 'D', // The day of the year (starting from 0) (0 through 365)
'W' => 'w', // ISO-8601 week number of year, weeks starting on Monday (Example: 42 (the 42nd week in the year))
'F' => 'MMMM', // A full textual representation of a month, such as January or March (January through December)
'm' => 'MM', // Numeric representation of a month, with leading zeros (01 through 12)
'M' => 'MMM', // A short textual representation of a month, three letters (Jan through Dec)
'n' => 'M', // Numeric representation of a month, without leading zeros (1 through 12)
't' => '', // Not supported yet: Number of days in the given month (28 through 31)
'L' => '', // Not supported yet: Whether it's a leap year (1 if it is a leap year, 0 otherwise.)
'o' => 'Y', // ISO-8601 week-numbering year. This has the same value as Y, except that if the ISO week number (W) belongs to the previous or next year, that year is used instead. (added in PHP 5.1.0) (Examples: 1999 or 2003)
'Y' => 'yyyy', // A full numeric representation of a year, 4 digits (Examples: 1999 or 2003)
'y' => 'yy', // A two digit representation of a year (Examples: 99 or 03)
'a' => '', // Not supported yet: Lowercase Ante meridiem and Post meridiem (am or pm)
'A' => 'a', // Uppercase Ante meridiem and Post meridiem (AM or PM)
'B' => '', // Not supported yet: Swatch Internet time (000 through 999)
'g' => 'h', // 12-hour format of an hour without leading zeros (1 through 12)
'G' => 'H', // 24-hour format of an hour without leading zeros (0 through 23)
'h' => 'hh', // 12-hour format of an hour with leading zeros (01 through 12)
'H' => 'HH', // 24-hour format of an hour with leading zeros (00 through 23)
'i' => 'mm', // Minutes with leading zeros (00 to 59)
's' => 'ss', // Seconds, with leading zeros (00 to 59)
'u' => '', // Not supported yet: Microseconds (added in PHP 5.2.2). Note that date() will always generate 000000 since it takes an integer parameter, whereas DateTime::format() does support microseconds if DateTime was created with microseconds. (Example: 654321)
];

return str_replace(array_keys($mapping), array_values($mapping), $format);
$chunks = str_split($format);

foreach ($chunks as $k => $v) {
if (!isset($mapping[$v])) {
continue;
}

$chunks[$k] = $mapping[$v];
}

return preg_replace('/([a-zA-Z])/', '$1', implode('', $chunks));
}

/**
Expand Down
61 changes: 53 additions & 8 deletions tests/Date/DateUtilTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,26 +26,26 @@ public function testCanBeInstantiated()
/**
* @dataProvider phpDateRFC3339Provider
*/
public function testFormatPhpDateToRFC3339($format, $expected)
public function testTransformPhpDateFormatToRFC3339($format, $expected)
{
$instance = new DateUtil($this->mockContaoFramework());

$this->assertEquals($expected, $instance->formatPhpDateToRFC3339($format));
$this->assertEquals($expected, $instance->transformPhpDateFormatToRFC3339($format));
}

/**
* @dataProvider transformPhpDateRFC3339Provider
* @dataProvider transformPhpDateFormatToRFC3339Provider
*/
public function testTransformPhpDateToRFC3339($format, $locale, $date, $expected)
public function testTransformPhpDateFormatToRFC3339WithDate($format, $locale, \DateTime $date, $expected)
{
$timezone = 'UTC';
$timezone = $date->getTimezone()->getName();
$calendar = \IntlDateFormatter::GREGORIAN;
$pattern = null;
$dateFormat = \IntlDateFormatter::MEDIUM; // default from Symfony\Component\Form\Extension\Core\DataTransformer\DateTimeToLocalizedStringTransformer
$timeFormat = \IntlDateFormatter::SHORT; // default from Symfony\Component\Form\Extension\Core\DataTransformer\DateTimeToLocalizedStringTransformer

$utils = new DateUtil($this->mockContaoFramework());
$rfc3339Format = $utils->formatPhpDateToRFC3339($format);
$rfc3339Format = $utils->transformPhpDateFormatToRFC3339($format);

$intlDateFormatter = new \IntlDateFormatter($locale, $dateFormat, $timeFormat, $timezone, $calendar, $pattern);
$intlDateFormatter->setPattern($rfc3339Format);
Expand All @@ -66,17 +66,39 @@ public function phpDateRFC3339Provider()
['N', 'd'],
['S', ''],
['w', 'e'],
['z', 'D'],
['W', 'w'],
['F', 'MMMM'],
['m', 'MM'],
['M', 'MMM'],
['n', 'M'],
['t', ''],
['L', ''],
['o', 'Y'],
['Y', 'yyyy'],
['y', 'yy'],
['a', ''],
['A', 'a'],
['B', ''],
['g', 'h'],
['G', 'H'],
['h', 'hh'],
['H', 'HH'],
['i', 'mm'],
['s', 'ss'],
['u', ''],
];
}

/**
* The php to rfc3339 test data provider
*/
public function transformPhpDateRFC3339Provider()
public function transformPhpDateFormatToRFC3339Provider()
{
$date = new \DateTime();
$date->setTimezone(new \DateTimeZone('UTC'));
$date->setDate('2018', '02', '04');
$date->setTime('16', '33', '12', '0');
$date->setTime('16', '09', '02', '1234');

return [
['d', 'en-EN', $date, '04'],
Expand All @@ -85,6 +107,29 @@ public function transformPhpDateRFC3339Provider()
['N', 'en-EN', $date, '4'],
['S', 'en-EN', $date, ''],
['w', 'en-EN', $date, '7'],
['z', 'en-EN', $date, '35'],
['W', 'en-EN', $date, '5'],
['F', 'en-EN', $date, 'February'],
['m', 'en-EN', $date, '02'],
['M', 'en-EN', $date, 'Feb'],
['M', 'en-EN', $date, 'Feb'],
['n', 'en-EN', $date, '2'],
['t', 'en-EN', $date, ''],
['L', 'en-EN', $date, ''],
['o', 'en-EN', $date, '2018'],
['o', 'en-EN', $date, '2018'],
['Y', 'en-EN', $date, '2018'],
['y', 'en-EN', $date, '18'],
['a', 'en-EN', $date, ''],
['A', 'en-EN', $date, 'PM'],
['B', 'en-EN', $date, ''],
['g', 'en-EN', $date, '4'],
['G', 'en-EN', $date, '16'],
['h', 'en-EN', $date, '04'],
['H', 'en-EN', $date, '16'],
['i', 'en-EN', $date, '09'],
['s', 'en-EN', $date, '02'],
['u', 'en-EN', $date, ''],
];
}

Expand Down

0 comments on commit 711b2d4

Please sign in to comment.