Skip to content
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

Fix to_datetime() for datetimes before the epoch #26

Merged
merged 2 commits into from
Mar 15, 2018

Conversation

aferreira
Copy link

I found the issue when representing birth dates with Mango::Time objects - calling to_datetime on a date before the epoch like 1965-12-21 would emit uninit warnings and end up with Jan 1, 1970 as output.

The bug comes from

https://github.com/oliwer/mango/blob/master/lib/Mango/BSON/Time.pm#L12 which in turn points

to Mojo::Date code

https://github.com/kraih/mojo/blob/master/lib/Mojo/Date.pm#L25

I tried to fix the issue downstream in Mojo::Date

but those patches have been rejected: the first due to failing extra tests added for negative fractional epoch, and the second because it was not elegant enough.

This time, this patch is meant to make Mango::BSON::Time to replace Mojo::Date->new($epoch) (which does not work for dates before Jan 1, 1970) with Mojo::Date->new->epoch($epoch).

They currently fail because Mojo::Date->new() can't cope with
negative epoch.
Work around Mojo::Date->new($epoch) choking on negative epoch.
@aferreira
Copy link
Author

aferreira commented Jun 8, 2017

This is the output of t/bson.t with the extra tests on 5dd139e and before the fix on dee3371:


$ prove --blib t/bson.t
t/bson.t .. 1/? Use of uninitialized value $epoch in gmtime at /Users/ferreira/.perlbrew/libs/perl-5.22.2@tmp3/lib/perl5/Mojo/Date.pm line 57.
Use of uninitialized value $epoch in pattern match (m//) at /Users/ferreira/.perlbrew/libs/perl-5.22.2@tmp3/lib/perl5/Mojo/Date.pm line 60.

#   Failed test 'Before epoch: Boris Karloff death'
#   at t/bson.t line 61.
#          got: '1970-01-01T00:00:00Z'
#     expected: '1969-02-02T11:00:00Z'
Use of uninitialized value $epoch in gmtime at /Users/ferreira/.perlbrew/libs/perl-5.22.2@tmp3/lib/perl5/Mojo/Date.pm line 57.
Use of uninitialized value $epoch in pattern match (m//) at /Users/ferreira/.perlbrew/libs/perl-5.22.2@tmp3/lib/perl5/Mojo/Date.pm line 60.

#   Failed test 'Well before epoch: Battle of New Orleans'
#   at t/bson.t line 63.
#          got: '1970-01-01T00:00:00Z'
#     expected: '1815-01-08T17:44:38Z'
# Looks like you failed 2 tests of 182.
t/bson.t .. Dubious, test returned 2 (wstat 512, 0x200)
Failed 2/182 subtests 

Test Summary Report
-------------------
t/bson.t (Wstat: 512 Tests: 182 Failed: 2)
  Failed tests:  25-26
  Non-zero exit status: 2
Files=1, Tests=182,  0 wallclock secs ( 0.02 usr  0.00 sys +  0.11 cusr  0.01 csys =  0.14 CPU)
Result: FAIL

@aferreira
Copy link
Author

Side note about dates used for the tests (not important for the test itself):

Where these dates came from?

I used the epoch computed with DateTime 1.43 and DateTime::Format::Strptime 1.73

  1. Boris Karloff death

    DateTime::Format::Strptime->new(pattern => '%Y-%m-%d %H:%M', time_zone => 'Europe/London')
    ->parse_datetime('1969-02-02 12:00')
    ->epoch

    -28731600
    1969-02-02T11:00:00Z

  2. Battle of New Orleans

    DateTime::Format::Strptime->new(pattern => '%Y-%m-%d %H:%M', time_zone => 'America/Indianapolis')
    ->parse_datetime('1815-01-08 12:00')
    ->epoch

    -4890694522
    1815-01-08T17:44:38Z

@aferreira
Copy link
Author

@oliwer Let me know what you think about this patch.

@oliwer
Copy link
Owner

oliwer commented Mar 15, 2018

Thanks @aferreira. This looks good to me. And it's nice to add some cultural references. I'll try to make a release this weekend.

@oliwer oliwer merged commit 813c8c1 into oliwer:master Mar 15, 2018
@aferreira aferreira deleted the devel/to-datetime branch March 23, 2018 20:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants