Browse Source

Start adding the Date wrapper.

Add the I18n\Date class which adds the same i18n features to Dates that
we offer on Time instances. The timeAgoInWords() method has not been
implemented yet.
Mark Story 10 years ago
parent
commit
77294ba789
2 changed files with 141 additions and 0 deletions
  1. 62 0
      src/I18n/Date.php
  2. 79 0
      tests/TestCase/I18n/DateTest.php

+ 62 - 0
src/I18n/Date.php

@@ -0,0 +1,62 @@
+<?php
+/**
+ * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
+ * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
+ *
+ * Licensed under The MIT License
+ * For full copyright and license information, please see the LICENSE.txt
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
+ * @link          http://cakephp.org CakePHP(tm) Project
+ * @since         3.2.0
+ * @license       http://www.opensource.org/licenses/mit-license.php MIT License
+ */
+namespace Cake\I18n;
+
+use Cake\Chronos\Date as BaseDate;
+use IntlDateFormatter;
+use JsonSerializable;
+
+/**
+ * Extends the Date class provided by Chronos.
+ *
+ * Adds handy methods and locale-aware formatting helpers
+ */
+class Date extends BaseDate implements JsonSerializable
+{
+    use DateFormatTrait;
+
+    /**
+     * The format to use when formatting a time using `Cake\I18n\Date::i18nFormat()`
+     * and `__toString`
+     *
+     * The format should be either the formatting constants from IntlDateFormatter as
+     * described in (http://www.php.net/manual/en/class.intldateformatter.php) or a pattern
+     * as specified in (http://www.icu-project.org/apiref/icu4c/classSimpleDateFormat.html#details)
+     *
+     * It is possible to provide an array of 2 constants. In this case, the first position
+     * will be used for formatting the date part of the object and the second position
+     * will be used to format the time part.
+     *
+     * @var string|array|int
+     * @see \Cake\I18n\Time::i18nFormat()
+     */
+    protected static $_toStringFormat = [IntlDateFormatter::SHORT, -1];
+
+    /**
+     * The format to use when formatting a time using `Cake\I18n\Time::nice()`
+     *
+     * The format should be either the formatting constants from IntlDateFormatter as
+     * described in (http://www.php.net/manual/en/class.intldateformatter.php) or a pattern
+     * as specified in (http://www.icu-project.org/apiref/icu4c/classSimpleDateFormat.html#details)
+     *
+     * It is possible to provide an array of 2 constants. In this case, the first position
+     * will be used for formatting the date part of the object and the second position
+     * will be used to format the time part.
+     *
+     * @var string|array|int
+     * @see \Cake\I18n\Time::nice()
+     */
+    public static $niceFormat = [IntlDateFormatter::MEDIUM, -1];
+}

+ 79 - 0
tests/TestCase/I18n/DateTest.php

@@ -0,0 +1,79 @@
+<?php
+/**
+ * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
+ * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
+ *
+ * Licensed under The MIT License
+ * For full copyright and license information, please see the LICENSE.txt
+ * Redistributions of files must retain the above copyright notice
+ *
+ * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
+ * @link          http://cakephp.org CakePHP(tm) Project
+ * @since         1.2.0
+ * @license       http://www.opensource.org/licenses/mit-license.php MIT License
+ */
+namespace Cake\Test\TestCase\I18n;
+
+use Cake\I18n\Date;
+use Cake\TestSuite\TestCase;
+
+/**
+ * DateTest class
+ */
+class DateTest extends TestCase
+{
+    /**
+     * test formatting dates taking in account preferred i18n locale file
+     *
+     * @return void
+     */
+    public function testI18nFormat()
+    {
+        $time = new Date('Thu Jan 14 13:59:28 2010');
+        $result = $time->i18nFormat();
+        $expected = '1/14/10';
+        $this->assertEquals($expected, $result);
+
+        $result = $time->i18nFormat(\IntlDateFormatter::FULL, null, 'es-ES');
+        $expected = 'jueves, 14 de enero de 2010, 0:00:00 (GMT)';
+        $this->assertEquals($expected, $result);
+
+        $format = [\IntlDateFormatter::NONE, \IntlDateFormatter::SHORT];
+        $result = $time->i18nFormat($format);
+        $expected = '12:00 AM';
+        $this->assertEquals($expected, $result);
+
+        $result = $time->i18nFormat('HH:mm:ss', 'Australia/Sydney');
+        $expected = '00:00:00';
+        $this->assertEquals($expected, $result);
+
+        Date::$defaultLocale = 'fr-FR';
+        $result = $time->i18nFormat(\IntlDateFormatter::FULL);
+        $expected = 'jeudi 14 janvier 2010 00:00:00 UTC';
+        $this->assertEquals($expected, $result);
+
+        $result = $time->i18nFormat(\IntlDateFormatter::FULL, null, 'es-ES');
+        $expected = 'jueves, 14 de enero de 2010, 0:00:00 (GMT)';
+        $this->assertEquals($expected, $result, 'Default locale should not be used');
+    }
+
+    public function testToString()
+    {
+        $this->markTestIncomplete();
+    }
+
+    public function testJsonSerialize()
+    {
+        $this->markTestIncomplete();
+    }
+
+    public function testParseDate()
+    {
+        $this->markTestIncomplete();
+    }
+
+    public function testParseDateTime()
+    {
+        $this->markTestIncomplete();
+    }
+}