Bug 32232 - Koha crashes if dateofbirth is 1947-04-27, 1948-04-25, or 1949-04-24
Summary: Koha crashes if dateofbirth is 1947-04-27, 1948-04-25, or 1949-04-24
Status: RESOLVED FIXED
Alias: None
Product: Koha
Classification: Unclassified
Component: Patrons (show other bugs)
Version: unspecified
Hardware: All All
: P5 - low minor (vote)
Assignee: David Cook
QA Contact: Testopia
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2022-11-16 14:25 UTC by Caroline Cyr La Rose
Modified: 2023-07-24 23:50 UTC (History)
8 users (show)

See Also:
Change sponsored?: ---
Patch complexity: ---
Documentation contact:
Documentation submission:
Text to go in the release notes:
Version(s) released in:
23.05.00,22.11.06,22.05.14


Attachments
Bug 32232: Ignore DST when calculating patron age (1.34 KB, patch)
2023-04-17 00:36 UTC, David Cook
Details | Diff | Splinter Review
Bug 32232: Ignore DST when calculating patron age (1.40 KB, patch)
2023-04-17 00:41 UTC, David Cook
Details | Diff | Splinter Review
Bug 32232: Ignore DST when calculating patron age (1.47 KB, patch)
2023-04-17 13:33 UTC, Caroline Cyr La Rose
Details | Diff | Splinter Review
Bug 32232: Ignore DST when calculating patron age (1.46 KB, patch)
2023-04-17 14:49 UTC, Marius
Details | Diff | Splinter Review
Bug 32232: Ignore DST when calculating patron age (1.52 KB, patch)
2023-04-28 18:56 UTC, Nick Clemens (kidclamp)
Details | Diff | Splinter Review

Note You need to log in before you can comment on or make changes to this bug.
Description Caroline Cyr La Rose 2022-11-16 14:25:13 UTC
I think this only happens if the server/computer is set to an Eastern Canadian timezone. In 1947, 1948 and 1949, Canada decided to switch to daylight saving time at midnight rather than the usual 2am. Therefore, 1947-04-27 00:00:00, 1948-04-25 00:00:00 and 1949-04-24 00:00:00 don't exist in this timezone.

Even if borrowers.dateofbirth is set to 'date' (not 'datetime'), Koha crashes, saying the date doesn't exist. I suspect it would happen with any date, but dateofbirth is the most likely (only) field to have dates in those years.

To test:
1. Create a patron, setting the date of birth to one of these three dates
- 1947-04-27
- 1948-04-25
- 1949-04-24
2. Save the patron
--> Error

I tested this on production installations in 21.05 and it works fine. Production installations in 22.05, it crashes.
Comment 1 Shi Yao Wang 2022-12-09 22:31:56 UTC
The code fails in sub get_age of Koha/Patron.pm at line:

>return $today->subtract_datetime( $date_of_birth )->years;

because $date_of_birth is invalid for time zone America/Toronto
Comment 2 David Cook 2023-04-17 00:20:20 UTC
That's a tricky one. It looks like dt_from_string() silently will produce a floating timestamp where a valid local timestamp can't be produced. 

In this particular case, if $today were converted from local to floating, then the age in years should be able to be computed without error.

After grepping around a bit, this looks like it could actually be an all right strategy. I was worried about it pushing the problem further down the line, but it looks like dateofbirth is mostly just used for display with this one exception...
Comment 3 David Cook 2023-04-17 00:28:26 UTC
(In reply to Caroline Cyr La Rose from comment #0)
> To test:
> 1. Create a patron, setting the date of birth to one of these three dates
> - 1947-04-27
> - 1948-04-25
> - 1949-04-24
> 2. Save the patron
> --> Error



To reproduce, you'll likely need to update <timezone></timezone> to include Canada/Eastern in your koha-conf.xml and then restart Plack.
Comment 4 David Cook 2023-04-17 00:36:40 UTC
Created attachment 149737 [details] [review]
Bug 32232: Ignore DST when calculating patron age

This change calculates patron age using the floating timezone, so that
datetimes missing midnight don't cause fatal errors in the comparison.

Test plan:
0. Apply patch
1. Change <timezone></timezone> in koha-conf.xml to include Canada/Eastern
2. koha-plack --restart kohadev
3. Create a patron, setting the date of birth to one of these three dates
- 1947-04-27
- 1948-04-25
- 1949-04-24
4. Save the patron
5. Patron detail page loads instead of producing fatal error
Comment 5 David Cook 2023-04-17 00:39:31 UTC
Actually, I'm going to amend that to use the same syntax that Jonathan used in bug 23079
Comment 6 David Cook 2023-04-17 00:41:02 UTC
Created attachment 149738 [details] [review]
Bug 32232: Ignore DST when calculating patron age

This change calculates patron age using the floating timezone, so that
datetimes missing midnight don't cause fatal errors in the comparison.

Test plan:
0. Apply patch
1. Change <timezone></timezone> in koha-conf.xml to include Canada/Eastern
2. koha-plack --restart kohadev
3. Create a patron, setting the date of birth to one of these three dates
- 1947-04-27
- 1948-04-25
- 1949-04-24
4. Save the patron
5. Patron detail page loads instead of producing fatal error
Comment 7 David Cook 2023-04-17 00:42:12 UTC
Btw, it looks like t/db_dependent/Koha/Patrons.t has a unit test that covers this change.
Comment 8 Caroline Cyr La Rose 2023-04-17 13:33:17 UTC
Created attachment 149754 [details] [review]
Bug 32232: Ignore DST when calculating patron age

This change calculates patron age using the floating timezone, so that
datetimes missing midnight don't cause fatal errors in the comparison.

Test plan:
0. Apply patch
1. Change <timezone></timezone> in koha-conf.xml to include Canada/Eastern
2. koha-plack --restart kohadev
3. Create a patron, setting the date of birth to one of these three dates
- 1947-04-27
- 1948-04-25
- 1949-04-24
4. Save the patron
5. Patron detail page loads instead of producing fatal error

Signed-off-by: Caroline Cyr La Rose <caroline.cyr-la-rose@inlibro.com>
Comment 9 Caroline Cyr La Rose 2023-04-17 13:35:08 UTC
Thanks so much David!

I can't vouch for the coding part, but the patch does solve the problem of accessing the patron's file.
Comment 10 Marius 2023-04-17 14:49:40 UTC
Created attachment 149758 [details] [review]
Bug 32232: Ignore DST when calculating patron age

This change calculates patron age using the floating timezone, so that
datetimes missing midnight don't cause fatal errors in the comparison.

Test plan:
0. Apply patch
1. Change <timezone></timezone> in koha-conf.xml to include Canada/Eastern
2. koha-plack --restart kohadev
3. Create a patron, setting the date of birth to one of these three dates
- 1947-04-27
- 1948-04-25
- 1949-04-24
4. Save the patron
5. Patron detail page loads instead of producing fatal error

Signed-off-by: Marius Mandrescu <marius.mandrescu@inLibro.com>
Comment 11 Nick Clemens (kidclamp) 2023-04-28 18:56:56 UTC
Created attachment 150416 [details] [review]
Bug 32232: Ignore DST when calculating patron age

This change calculates patron age using the floating timezone, so that
datetimes missing midnight don't cause fatal errors in the comparison.

Test plan:
0. Apply patch
1. Change <timezone></timezone> in koha-conf.xml to include Canada/Eastern
2. koha-plack --restart kohadev
3. Create a patron, setting the date of birth to one of these three dates
- 1947-04-27
- 1948-04-25
- 1949-04-24
4. Save the patron
5. Patron detail page loads instead of producing fatal error

Signed-off-by: Marius Mandrescu <marius.mandrescu@inLibro.com>
Signed-off-by: Nick Clemens <nick@bywatersolutions.com>
Comment 12 Tomás Cohen Arazi 2023-05-09 13:51:30 UTC
Pushed to master for 23.05.

Nice work everyone, thanks!
Comment 13 Matt Blenkinsop 2023-05-15 10:09:55 UTC
Nice work everyone!

Pushed to stable for 22.11.x
Comment 14 Lucas Gass 2023-06-15 16:37:09 UTC
Backported to 22.05.x for upcoming 22.05.14
Comment 15 Danyon Sewell 2023-07-24 23:50:02 UTC
Not backporting to 21.11.x