To display the age of patrons in a table we will need to do the calculation in JS
Created attachment 130358 [details] [review] Bug 30059: Add JS equivalent for Koha::Patron->get_age On bug 30055 we are going to use the REST API to display the patron search result, we will then need to calculate patron's age client-side. This is moved to its own bug report in case we need to reuse it somewhere else. Test plan: Copy/paste the JS function in your browser's console then call it and confirm that the result is correct For instance: $get_age('2000-01-01')
Created attachment 130665 [details] [review] Bug 30059: Add JS equivalent for Koha::Patron->get_age On bug 30055 we are going to use the REST API to display the patron search result, we will then need to calculate patron's age client-side. This is moved to its own bug report in case we need to reuse it somewhere else. Test plan: Copy/paste the JS function in your browser's console then call it and confirm that the result is correct For instance: $get_age('2000-01-01')
get_age('1979-02-17') = 43 get_age('1979-02-18') = 43 get_age('1979-02-19') = 43 XXX Today is 2022-02-18 - Not birthday yet! Stop calling me old!!! get_age('1979-02-20') = 42
Might be an unpopular view.. but don't we already pull in moment.js for other date manipulations.. so couldn't we use https://momentjs.com/docs/#/displaying/difference/ to get a rather accurate result?
(In reply to Nick Clemens from comment #3) > get_age('1979-02-17') = 43 > get_age('1979-02-18') = 43 > get_age('1979-02-19') = 43 XXX Today is 2022-02-18 - Not birthday yet! Stop > calling me old!!! > get_age('1979-02-20') = 42 Are you sure? Today is 2022-02-21: $get_age("1979-02-19") = 43 $get_age("1979-02-20") = 43 $get_age("1979-02-21") = 43 # today $get_age("1979-02-22") = 42 So I modified the function with "var today = new Date("2022-02-18");" $get_age('1979-02-17') = 43 $get_age('1979-02-18') = 43 # "today" $get_age('1979-02-19') = 42 $get_age('1979-02-20') = 42
(In reply to Jonathan Druart from comment #5) > Are you sure? Yes, tested again this morning, same results, I believe time portion causes problems: let dob = new Date('2022-02-23'); dob: Date Thu Feb 22 1979 19:00:00 GMT-0500 (Eastern Standard Time)
(In reply to Martin Renvoize from comment #4) > Might be an unpopular view.. but don't we already pull in moment.js for > other date manipulations.. so couldn't we use > https://momentjs.com/docs/#/displaying/difference/ to get a rather accurate > result? I'd agree with this. I also wonder... if the browser date should be used, or the server's... I guess it is not that important. But if we think the UI could make a decision, based on the age, and place a request, this could be rejected because the server could have its own age check, leading to inconsistent behavior. In that case, I'd just allow embedding the age like in: GET /patrons x-koha-emebed: age (it would require renaming the ->get_age method)
The problem is the timezone, not the calculation. I don't think we need a lib for that. (In reply to Martin Renvoize from comment #4) > Might be an unpopular view.. but don't we already pull in moment.js for > other date manipulations.. so couldn't we use > https://momentjs.com/docs/#/displaying/difference/ to get a rather accurate > result? I didn't manage to make it work, I get "tz is not a function". I've tried with moment-timezone as well, and both with the latest version. Also worth noting: "Moment.js is a legacy project, now in maintenance mode. In most cases, you should choose a different library.", from https://github.com/moment/moment (In reply to Tomás Cohen Arazi from comment #7) > In that case, I'd just allow embedding the age like in: > > GET /patrons > x-koha-emebed: age > > (it would require renaming the ->get_age method) I had that concern some months ago (we will have twice the same business logic), but we agreed on providing a "JS equivalent" (bug 28874). Here it's over-complicating things anyway, we just want to subtract two dates. We can also rely on the client to make extra processing, which is good for server perfs.
Created attachment 131309 [details] [review] Bug 30059: Deal with timezones
This patches fixes the problem. Test plan: open the console and define a timezone: let tz = "UTC" or "America/Buenos_Aires" then call the function.
Created attachment 131672 [details] [review] Bug 30059: Add JS equivalent for Koha::Patron->get_age On bug 30055 we are going to use the REST API to display the patron search result, we will then need to calculate patron's age client-side. This is moved to its own bug report in case we need to reuse it somewhere else. Test plan: Copy/paste the JS function in your browser's console then call it and confirm that the result is correct For instance: $get_age('2000-01-01') Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
Created attachment 131673 [details] [review] Bug 30059: Deal with timezones Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
Does Bug 30061 have any impact here ?
(In reply to Fridolin Somers from comment #13) > Does Bug 30061 have any impact here ? Unrelated.
Created attachment 132178 [details] [review] Bug 30059: Add JS equivalent for Koha::Patron->get_age On bug 30055 we are going to use the REST API to display the patron search result, we will then need to calculate patron's age client-side. This is moved to its own bug report in case we need to reuse it somewhere else. Test plan: Copy/paste the JS function in your browser's console then call it and confirm that the result is correct For instance: $get_age('2000-01-01') Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io> Signed-off-by: Nick Clemens <nick@bywatersolutions.com>
Created attachment 132179 [details] [review] Bug 30059: Deal with timezones Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io> Signed-off-by: Nick Clemens <nick@bywatersolutions.com>
Pushed to master for 22.05, thanks to everybody involved [U+1F984]