Page MenuHomePhabricator

{{#time}} parser function should not accept dates with zero in day or month positions
Open, Needs TriagePublic

Description

Using numbers 0 in the day or month field of the {{#time}} parser function can lead to unexpected results. For example
{{#time: Y | 2017-00-00 }} → 2016
{{#time: M | 2017-01-00 }} → Dec
{{#time: r | 2017-02-30 }} → Thu, 02 Mar 2017 00:00:00 +0000
{{#time: r | 2017-02-00 }} → Tue, 31 Jan 2017 00:00:00 +0000

The proper behavior would be to return "Error: Invalid time" as other out of bound numbers do:
{{#time: r | 2017-02-32 }} → Error: Invalid time.

Event Timeline

This is the documented behaviour, so is correct. It can be useful, eg subtracting 1 from the day and converting can move you back a month and possibly year.

Help:Extension:ParserFunctions states date is "any format accepted by PHP's strtotime() function".

PHP strtotime doc "Supported Date and Time Formats"/[https://www.php.net/manual/en/datetime.formats.date.php "Date Formats"] section says in the 4th Note:

" It is possible to over- and underflow the dd and DD format. Day 0 means the last day of previous month, whereas overflows count into the next month. This makes "2008-08-00" equivalent to "2008-07-31" and "2008-06-31" equivalent to "2008-07-01" (June only has 30 days).

... It is also possible to underflow the mm and MM formats with the value 0. A month value of 0 means December of the previous year. As example "2008-00-22" is equivalent to "2007-12-22".

If you combine the previous two facts and underflow both the day and the month, the following happens: "2008-00-00" first gets converted to "2007-12-00" which then gets converted to "2007-11-30". This also happens with the string "0000-00-00", which gets transformed into "-0001-11-30" (the year -1 in the ISO 8601 calendar, which is 2 BC in the proleptic Gregorian calendar)."

Pppery subscribed.

There's no reason whatsoever to deliberately make previously valid inputs invalid.

This would be fine to do in a new {{#timeiso}} parser function, if you really want to do this. As @Pppery says, it's not worth breaking existing uses on wiki for which this is a feature, not a bug.

This is the documented behaviour, so is correct. …

Very disputable claim.

If you mean

The function performs a certain amount of date mathematics:

  • {{#time: d F Y | January 0 2008 }} → 31 December 2007
  • […]

(https://www.mediawiki.org/wiki/Help:Extension:ParserFunctions, permalink)

one could complain that January 0 is not exactly a ~ date math expression, unlike now -1 months mentioned in the same list there.