Skip to content

Wishlist: natural sorting for Mojo::Date #1573

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
augensalat opened this issue Oct 15, 2020 · 0 comments
Open

Wishlist: natural sorting for Mojo::Date #1573

augensalat opened this issue Oct 15, 2020 · 0 comments

Comments

@augensalat
Copy link
Contributor

  • Mojolicious version: any
  • Perl version: any
  • Operating system: any

Mojo::Date objects stringify to RFC 7231 HTTP-date format. As a consequence sorting is kind of broken:

With string comparison operators their first sort attribute is the English week day name, and you get wrong results and warnings with numeric comparison.

Steps to reproduce the behavior

$> perl -MMojo::Date -wE 'say Mojo::Date->new("2020-10-15T00:00:00Z") cmp Mojo::Date->new("2020-10-16T00:00:00Z");'
1
$> perl -MMojo::Date -wE 'say Mojo::Date->new("2020-10-15T00:00:00Z") <=> Mojo::Date->new("2020-10-16T00:00:00Z");'
Argument "Fri, 16 Oct 2020 00:00:00 GMT" isn't numeric in numeric comparison (<=>) at -e line 1.
Argument "Thu, 15 Oct 2020 00:00:00 GMT" isn't numeric in numeric comparison (<=>) at -e line 1.
0

Expected behavior

$> perl -MMojo::Date -wE 'say Mojo::Date->new("2020-10-15T00:00:00Z") cmp Mojo::Date->new("2020-10-16T00:00:00Z");'
-1
$> perl -MMojo::Date -wE 'say Mojo::Date->new("2020-10-15T00:00:00Z") <=> Mojo::Date->new("2020-10-16T00:00:00Z");'
-1

Actual behavior

See Steps to reproduce the behavior

Suggested code change

Overload cmp and <=> with something like sub ($l, $r) { $l->epoch <=> $r->epoch } (type check for $r omitted for brevity).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants