Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
43.75% |
7 / 16 |
|
50.00% |
2 / 4 |
CRAP | |
0.00% |
0 / 1 |
| AbstractDateFormatter | |
43.75% |
7 / 16 |
|
50.00% |
2 / 4 |
19.39 | |
0.00% |
0 / 1 |
| format | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| getFormat | |
33.33% |
2 / 6 |
|
0.00% |
0 / 1 |
5.67 | |||
| getJavascriptFormat | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| convertPhpToJavascriptFormat | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2015-2019 (original work) Open Assessment Technologies SA; |
| 19 | */ |
| 20 | |
| 21 | namespace oat\tao\helpers\dateFormatter; |
| 22 | |
| 23 | use common_Logger as Logger; |
| 24 | use common_session_SessionManager as SessionManager; |
| 25 | use DateTime; |
| 26 | use DateTimeZone; |
| 27 | use oat\oatbox\Configurable; |
| 28 | use tao_helpers_Date as DateHelper; |
| 29 | |
| 30 | /** |
| 31 | * Utility to display dates. |
| 32 | */ |
| 33 | class AbstractDateFormatter extends Configurable implements DateFormatterInterface |
| 34 | { |
| 35 | public const REPLACEMENTS = [ |
| 36 | 'A' => 'A', // for the sake of escaping below |
| 37 | 'a' => 'a', // for the sake of escaping below |
| 38 | 'B' => '', // Swatch internet time (.beats), no equivalent |
| 39 | 'c' => 'YYYY-MM-DD[T]HH:mm:ssZ', // ISO 8601 |
| 40 | 'D' => 'ddd', |
| 41 | 'd' => 'DD', |
| 42 | 'e' => 'zz', // deprecated since version 1.6.0 of moment.js |
| 43 | 'F' => 'MMMM', |
| 44 | 'G' => 'H', |
| 45 | 'g' => 'h', |
| 46 | 'H' => 'HH', |
| 47 | 'h' => 'hh', |
| 48 | 'I' => '', // Daylight Saving Time? => moment().isDST(); |
| 49 | 'i' => 'mm', |
| 50 | 'j' => 'D', |
| 51 | 'L' => '', // Leap year? => moment().isLeapYear(); |
| 52 | 'l' => 'dddd', |
| 53 | 'M' => 'MMM', |
| 54 | 'm' => 'MM', |
| 55 | 'N' => 'E', |
| 56 | 'n' => 'M', |
| 57 | 'O' => 'ZZ', |
| 58 | 'o' => 'YYYY', |
| 59 | 'P' => 'Z', |
| 60 | 'r' => 'ddd, DD MMM YYYY HH:mm:ss ZZ', // RFC 2822 |
| 61 | 'S' => 'o', |
| 62 | 's' => 'ss', |
| 63 | 'T' => 'z', // deprecated since version 1.6.0 of moment.js |
| 64 | 't' => '', // days in the month => moment().daysInMonth(); |
| 65 | 'U' => 'X', |
| 66 | 'u' => 'SSSSSS', // microseconds |
| 67 | 'v' => 'SSS', // milliseconds (from PHP 7.0) |
| 68 | 'W' => 'W', // for the sake of escaping below |
| 69 | 'w' => 'e', |
| 70 | 'Y' => 'YYYY', |
| 71 | 'y' => 'YY', |
| 72 | 'Z' => '', // time zone offset in minutes => moment().zone(); |
| 73 | 'z' => 'DDD', |
| 74 | ]; |
| 75 | |
| 76 | protected $datetimeFormats = []; |
| 77 | |
| 78 | /** |
| 79 | * {@inheritdoc} |
| 80 | */ |
| 81 | public function format($timestamp, $format, DateTimeZone $timeZone = null) |
| 82 | { |
| 83 | // Creates DateTime with microseconds. |
| 84 | $dateTime = DateTime::createFromFormat('U.u', sprintf('%.f', $timestamp)); |
| 85 | if ($timeZone === null) { |
| 86 | $timeZone = new DateTimeZone(SessionManager::getSession()->getTimeZone()); |
| 87 | } |
| 88 | $dateTime->setTimezone($timeZone); |
| 89 | |
| 90 | return $dateTime->format($this->getFormat($format)); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * {@inheritdoc} |
| 95 | */ |
| 96 | public function getFormat($format) |
| 97 | { |
| 98 | if (!isset($this->datetimeFormats[$format])) { |
| 99 | if (!isset($this->datetimeFormats[DateHelper::FORMAT_FALLBACK])) { |
| 100 | Logger::w('Unknown date format ' . $format . ' for ' . __FUNCTION__, 'TAO'); |
| 101 | return ''; |
| 102 | } |
| 103 | |
| 104 | $format = DateHelper::FORMAT_FALLBACK; |
| 105 | } |
| 106 | |
| 107 | return $this->datetimeFormats[$format]; |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * {@inheritdoc} |
| 112 | */ |
| 113 | public function getJavascriptFormat($format) |
| 114 | { |
| 115 | return $this->convertPhpToJavascriptFormat($this->getFormat($format)); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Converts php DateTime format to Javascript Moment format. |
| 120 | * |
| 121 | * @param string $phpFormat |
| 122 | * |
| 123 | * @return string |
| 124 | */ |
| 125 | public function convertPhpToJavascriptFormat($phpFormat) |
| 126 | { |
| 127 | // Converts all the meaningful characters. |
| 128 | $replacements = self::REPLACEMENTS; |
| 129 | |
| 130 | // Converts all the escaped meaningful characters. |
| 131 | foreach (self::REPLACEMENTS as $from => $to) { |
| 132 | $replacements['\\' . $from] = '[' . $from . ']'; |
| 133 | } |
| 134 | |
| 135 | return strtr($phpFormat, $replacements); |
| 136 | } |
| 137 | } |