parser: Fix formatdate parser function for ISO year 0 = 1 BC

I'm not sure how this ever happened, but I'm sure it's a mistake.
The following test scenario should make it very obvious:

* {{#formatdate:-0002-12-31|mdy}}
* {{#formatdate:-0001-12-31|mdy}}
* {{#formatdate:0000-12-31|mdy}}
* {{#formatdate:0001-12-31|mdy}}
* {{#formatdate:0002-12-31|mdy}}

Expected output: 3 BC, 2 BC, 1 BC, 1, 2, …
Current output: 3 BC, 2 BC, 0 (?), 1, 2, …

Note how "1 BC" is skipped and shown as "0" instead. Everything else
is correct, e.g. the ISO year -1 is already displayed as "2 BC".
It's really only this single outlier.

In case you don't know: There is no year 0 when the BC specifier is
used. There is either year 1 after or year 1 before Christ. This is
different in ISO, mostly to make calculations easier. That's why the
DateFormater already does an extra `- 1` and `+ 1` in the two
makeIsoYear and makeNormalYear methods.

The problematic line of code was originally written in 2003, see
https://phabricator.wikimedia.org/rMW98fc03e6
The core parser function exists since 2009, see
https://phabricator.wikimedia.org/rMWb9ffb5a7

Change-Id: Iaeb7a954579a409fefd87dab4e2a15778ab39fb4
This commit is contained in:
thiemowmde 2024-02-27 17:17:36 +01:00
parent 58fdf9d660
commit a15b6d516f
2 changed files with 16 additions and 1 deletions

View file

@ -308,7 +308,7 @@ class DateFormatter {
* year number and 'BC' at the end otherwise.
*/
private function makeNormalYear( $iso ) {
if ( $iso[0] == '-' ) {
if ( $iso <= 0 ) {
$text = ( intval( substr( $iso, 1 ) ) + 1 ) . ' BC';
} else {
$text = intval( $iso );

View file

@ -12064,6 +12064,21 @@ formatdate uses correct capitalisation in English
<p><span class="mw-formatted-date" title="06-03" about="#mwt1" typeof="mw:Transclusion" data-parsoid='{"stx":"html","pi":[[{"k":"1"}]]}' data-mw='{"parts":[{"template":{"target":{"wt":"#formatdate:june 3","function":"formatdate"},"params":{"1":{"wt":"dmy"}},"i":0}}]}'>3 June</span></p>
!! end
!! test
formatdate formats ISO year 0 as 1 BC
!! wikitext
{{#formatdate:-0001-12-31|mdy}}
{{#formatdate:0000-12-31|mdy}}
{{#formatdate:0001-12-31|mdy}}
!! html/php
<p><span class="mw-formatted-date" title="-0001-12-31">December 31, 2 BC</span>
<span class="mw-formatted-date" title="0000-12-31">December 31, 1 BC</span>
<span class="mw-formatted-date" title="0001-12-31">December 31, 1</span>
</p>
!! html/parsoid+integrated
<p><span class="mw-formatted-date" title="-0001-12-31" typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"#formatdate:-0001-12-31","function":"formatdate"},"params":{"1":{"wt":"mdy"}},"i":0}}]}'>December 31, 2 BC</span> <span class="mw-formatted-date" title="0000-12-31" typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"#formatdate:0000-12-31","function":"formatdate"},"params":{"1":{"wt":"mdy"}},"i":0}}]}'>December 31, 1 BC</span> <span class="mw-formatted-date" title="0001-12-31" typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"#formatdate:0001-12-31","function":"formatdate"},"params":{"1":{"wt":"mdy"}},"i":0}}]}'>December 31, 1</span></p>
!! end
#
#
#