|
| 1 | +--TEST-- |
| 2 | +ICU - Intl.DateTimeFormat() |
| 3 | +--SKIPIF-- |
| 4 | +<?php if (!extension_loaded("v8")) console.log "skip"; ?> |
| 5 | +--ENV-- |
| 6 | +TZ=UTC |
| 7 | +--INI-- |
| 8 | +date.timezone = "UTC" |
| 9 | +--FILE-- |
| 10 | +<?php |
| 11 | +/** @var \Phpv8Testsuite $helper */ |
| 12 | +$helper = require '.testsuite.php'; |
| 13 | + |
| 14 | +require '.v8-helpers.php'; |
| 15 | +$v8_helper = new PhpV8Helpers($helper); |
| 16 | + |
| 17 | +// Tests: |
| 18 | + |
| 19 | +$isolate = new \V8\Isolate(); |
| 20 | +$context = new \V8\Context($isolate); |
| 21 | +$v8_helper->injectConsoleLog($context); |
| 22 | + |
| 23 | +// @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DateTimeFormat#Using_locales |
| 24 | +$source = /** @lang JavaScript 1.8 */ |
| 25 | + <<<HEREDOC |
| 26 | +var date = new Date(Date.UTC(2012, 11, 20, 3, 0, 0)); |
| 27 | +
|
| 28 | +// formats below assume the local time zone of the locale; |
| 29 | +// America/Los_Angeles for the US |
| 30 | +
|
| 31 | +// US English uses month-day-year order |
| 32 | +console.log(new Intl.DateTimeFormat('en-US').format(date)); |
| 33 | +// → "12/19/2012" |
| 34 | +
|
| 35 | +// British English uses day-month-year order |
| 36 | +console.log(new Intl.DateTimeFormat('en-GB').format(date)); |
| 37 | +// → "20/12/2012" |
| 38 | +
|
| 39 | +// Korean uses year-month-day order |
| 40 | +console.log(new Intl.DateTimeFormat('ko-KR').format(date)); |
| 41 | +// → "2012. 12. 20." |
| 42 | +
|
| 43 | +// Arabic in most Arabic speaking countries uses real Arabic digits |
| 44 | +console.log(new Intl.DateTimeFormat('ar-EG').format(date)); |
| 45 | +// → "٢٠/١٢/٢٠١٢" |
| 46 | +
|
| 47 | +// for Japanese, applications may want to use the Japanese calendar, |
| 48 | +// where 2012 was the year 24 of the Heisei era |
| 49 | +console.log(new Intl.DateTimeFormat('ja-JP-u-ca-japanese').format(date)); |
| 50 | +// → "24/12/20" |
| 51 | +
|
| 52 | +// when requesting a language that may not be supported, such as |
| 53 | +// Balinese, include a fallback language, in this case Indonesian |
| 54 | +console.log(new Intl.DateTimeFormat(['ban', 'id']).format(date)); |
| 55 | +// → "20/12/2012" |
| 56 | +HEREDOC; |
| 57 | + |
| 58 | +(new \V8\Script($context, new \V8\StringValue($isolate, $source)))->Run($context); |
| 59 | + |
| 60 | +$helper->line(); |
| 61 | + |
| 62 | + |
| 63 | +// @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DateTimeFormat#Using_options |
| 64 | +// with en-AU am/AM hack as it causes troubles in CI and inconsistent across different ICU versions |
| 65 | +$source = /** @lang JavaScript 1.8 */ |
| 66 | + <<<HEREDOC |
| 67 | +var date = new Date(Date.UTC(2012, 11, 20, 3, 0, 0)); |
| 68 | +
|
| 69 | +// request a weekday along with a long date |
| 70 | +var options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' }; |
| 71 | +console.log(new Intl.DateTimeFormat('de-DE', options).format(date)); |
| 72 | +// → "Donnerstag, 20. Dezember 2012" |
| 73 | +
|
| 74 | +// an application may want to use UTC and make that visible |
| 75 | +options.timeZone = 'UTC'; |
| 76 | +options.timeZoneName = 'short'; |
| 77 | +console.log(new Intl.DateTimeFormat('en-US', options).format(date)); |
| 78 | +// → "Thursday, December 20, 2012, GMT" |
| 79 | +
|
| 80 | +// sometimes you want to be more precise |
| 81 | +var options = { |
| 82 | + hour: 'numeric', minute: 'numeric', second: 'numeric', |
| 83 | + timeZoneName: 'short' |
| 84 | +}; |
| 85 | +console.log(new Intl.DateTimeFormat('en-AU', options).format(date).replace('AM', 'am')); |
| 86 | +// → "2:00:00 pm AEDT" |
| 87 | +
|
| 88 | +// sometimes even the US needs 24-hour time |
| 89 | +options = { |
| 90 | + year: 'numeric', month: 'numeric', day: 'numeric', |
| 91 | + hour: 'numeric', minute: 'numeric', second: 'numeric', |
| 92 | + hour12: false |
| 93 | +}; |
| 94 | +console.log(date.toLocaleString('en-US', options)); |
| 95 | +// → "12/19/2012, 19:00:00" |
| 96 | +HEREDOC; |
| 97 | + |
| 98 | +(new \V8\Script($context, new \V8\StringValue($isolate, $source)))->Run($context); |
| 99 | + |
| 100 | + |
| 101 | +$helper->line(); |
| 102 | + |
| 103 | +$source = /** @lang JavaScript 1.8 */ |
| 104 | + <<<HEREDOC |
| 105 | +var date = new Date(Date.UTC(2012, 11, 20, 3, 0, 0)); |
| 106 | +
|
| 107 | +// request a weekday along with a long date |
| 108 | +var options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' }; |
| 109 | +
|
| 110 | +options.timeZone = 'America/New_York'; |
| 111 | +console.log(new Intl.DateTimeFormat('en-US', options).format(date)); |
| 112 | +options.timeZone = 'Europe/Paris'; |
| 113 | +console.log(new Intl.DateTimeFormat('fr-FR', options).format(date)); |
| 114 | +options.timeZone = 'Europe/Berlin'; |
| 115 | +console.log(new Intl.DateTimeFormat('de-DE', options).format(date)); |
| 116 | +
|
| 117 | +HEREDOC; |
| 118 | + |
| 119 | +(new \V8\Script($context, new \V8\StringValue($isolate, $source)))->Run($context); |
| 120 | + |
| 121 | + |
| 122 | +?> |
| 123 | +--EXPECT-- |
| 124 | +12/20/2012 |
| 125 | +20/12/2012 |
| 126 | +2012. 12. 20. |
| 127 | +٢٠/١٢/٢٠١٢ |
| 128 | +平成24/12/20 |
| 129 | +20/12/2012 |
| 130 | + |
| 131 | +Donnerstag, 20. Dezember 2012 |
| 132 | +Thursday, December 20, 2012, GMT |
| 133 | +3:00:00 am GMT |
| 134 | +12/20/2012, 03:00:00 |
| 135 | + |
| 136 | +Wednesday, December 19, 2012 |
| 137 | +jeudi 20 décembre 2012 |
| 138 | +Donnerstag, 20. Dezember 2012 |
0 commit comments