WordPress.org

Make WordPress Core

Opened 2 years ago

Last modified 4 months ago

#41032 new defect (bug)

REST API: Date fields do not support ISO8601

Reported by: jnylen0 Owned by:
Milestone: Future Release Priority: normal
Severity: normal Version:
Component: REST API Keywords: has-unit-tests has-patch
Focuses: docs, rest-api Cc:

Description

The post and comment date and date_gmt fields as well as a couple of others like the post before and after fields claim to support ISO8601 but this is not entirely accurate.

The actual format supported is that handled by the `rest_parse_date` function: YYYY-MM-DDTHH:MM:SS plus optional fractional seconds plus an optional timezone specifier in the form Z, +XX, -XX, +XX:XX, or -XX:XX.

Additionally, WordPress behavior around timezones is complicated and poorly specified. The post_date database field is stored in the site's current timezone, and how to interact with this using before and after in particular is not clear. (The best way to do it is probably to avoid specifying a timezone string at all for these values, unless the site's timezone is known by the API client and specified exactly.)

All of this needs to be documented at https://developer.wordpress.org/rest-api/ and any missing test cases added.

Attachments (3)

41032.patch (1.2 KB) - added by nickylimjj 4 months ago.
41032.diff (13.8 KB) - added by nickylimjj 4 months ago.
41032.2.diff (6.9 KB) - added by nickylimjj 4 months ago.

Download all attachments as: .zip

Change History (7)

@nickylimjj
4 months ago

#1 @nickylimjj
4 months ago

  • Keywords needs-unit-tests added

Attempted a patch for a better regex string, for ISO8601 validation referenced here: https://www.myintervals.com/blog/2009/05/20/iso-8601-date-validation-that-doesnt-suck/.

Will attempt the missing test cases.

@nickylimjj
4 months ago

#2 follow-up: @nickylimjj
4 months ago

  • Keywords has-unit-tests has-patch added; needs-unit-tests removed

Looks like I uploaded extra files because i used the $ grunt upload_patch:41032command. Will need to fix that...

I modified the tests test_rest_parse_date and test_rest_parse_date_force_utcto assertNotFalse instead of assertEquals because I am getting different unix timestamps for strtotime(...) used in the rest_parse_date function in wp-includes/rest-api.php and the gmmktime(...) function for testing in phpunit/tests/rest-api.php For instance

php > echo gmmktime( 0, 0, 0, 1, 16, 2017 );
1484524800
php > echo strtotime( '2017-01-16' );
1484542800
php > echo strtotime( '2017-01-16T' );
1484550000

are all valid strings and it is sufficient to just assertTrue.

This also seems to respond to #38614

Reference:
ISO8601 validation https://www.myintervals.com/blog/2009/05/20/iso-8601-date-validation-that-doesnt-suck/
regex tester for ISO8601 validation https://www.regextester.com/97766

This ticket was mentioned in Slack in #core-restapi by nickylimjj. View the logs.


4 months ago

@nickylimjj
4 months ago

#4 in reply to: ↑ 2 @nickylimjj
4 months ago

Replying to nickylimjj:

Looks like I uploaded extra files because i used the $ grunt upload_patch:41032command. Will need to fix that...

Fixed.

Note: See TracTickets for help on using tickets.