| Summary: | Make formatting date and datetime fields in notices a bit shorter/easier | ||
|---|---|---|---|
| Product: | Koha | Reporter: | Marcel de Rooy <m.de.rooy> |
| Component: | Notices | Assignee: | Marcel de Rooy <m.de.rooy> |
| Status: | Pushed to stable --- | QA Contact: | Martin Renvoize (ashimema) <martin.renvoize> |
| Severity: | enhancement | ||
| Priority: | P5 - low | CC: | david, jonathan.druart, martin.renvoize, paul.derscheid, wainuiwitikapark |
| Version: | Main | ||
| Hardware: | All | ||
| OS: | All | ||
| GIT URL: | Initiative type: | --- | |
| Sponsorship status: | --- | Crowdfunding goal: | 0 |
| Patch complexity: | Small patch | Documentation contact: | |
| Documentation submission: | Text to go in the release notes: |
This enhancement adds an easier way to format dates in notices, and minimise potential errors - strftime. It can be used for both date and date time fields, and is locale friendly.
Examples:
- Date field: [% borrower.dateexpiry.strftime('%d-%m-%y') %]
- Date and time field: [% borrower.lastseen.strftime("%d-%m-%y %H:%M") %]
- Locale: [% borrower.dateexpiry.strftime("%d %B %Y", "nl_NL") %]
|
|
| Version(s) released in: |
25.05.00,24.11.01
|
Circulation function: | |
| Attachments: |
Bug 38758: Add strftime as TT virtual method
Bug 38758: Add subtest to Letters.t Bug 38758: Add strftime as TT virtual method Bug 38758: Add subtest to Letters.t Bug 38758: Add strftime as TT virtual method Bug 38758: Add subtest to Letters.t |
||
Created attachment 175818 [details] [review] Bug 38758: Add strftime as TT virtual method Test plan: Try things like [% borrower.dateexpiry.strftime('%d-%m-%y') %] in a notice. Also test datetime fields like borrower.lastseen. Add locale support with strftime('%d %B', 'nl_NL') etc. Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl> Created attachment 175819 [details] [review] Bug 38758: Add subtest to Letters.t Test plan: Run t/db_dependent/Letters.t Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl> Created attachment 175820 [details] [review] Bug 38758: Add strftime as TT virtual method Test plan: Try things like [% borrower.dateexpiry.strftime('%d-%m-%y') %] in a notice. Also test datetime fields like borrower.lastseen. Add locale support with strftime('%d %B', 'nl_NL') etc. Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl> Signed-off-by: Paul Derscheid <paul.derscheid@lmscloud.de> Created attachment 175821 [details] [review] Bug 38758: Add subtest to Letters.t Test plan: Run t/db_dependent/Letters.t Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl> Signed-off-by: Paul Derscheid <paul.derscheid@lmscloud.de> Created attachment 175822 [details] [review] Bug 38758: Add strftime as TT virtual method Test plan: Try things like [% borrower.dateexpiry.strftime('%d-%m-%y') %] in a notice. Also test datetime fields like borrower.lastseen. Add locale support with strftime('%d %B', 'nl_NL') etc. Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl> Signed-off-by: Paul Derscheid <paul.derscheid@lmscloud.de> Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com> Created attachment 175823 [details] [review] Bug 38758: Add subtest to Letters.t Test plan: Run t/db_dependent/Letters.t Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl> Signed-off-by: Paul Derscheid <paul.derscheid@lmscloud.de> Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com> I'd be tempted to add the option to the filter call too I've confirmed it works as is.. though I'm not entirely sure I understand the innards of TT plugins to know why. It might be nice to be able to call it as so [% borrower.lastseen | $KohaDates with_format = '%d-%m' %] (In reply to Martin Renvoize (ashimema) from comment #7) > I'd be tempted to add the option to the filter call too > > I've confirmed it works as is.. though I'm not entirely sure I understand > the innards of TT plugins to know why. > > It might be nice to be able to call it as so > > [% borrower.lastseen | $KohaDates with_format = '%d-%m' %] Thanks for QA. I would prefer to change the filter sub of KohaDates on another report though. We have a bit of documentation for formatting dates in the TT notices in the wiki, can you please update? https://wiki.koha-community.org/wiki/Notices_with_Template_Toolkit#Example:_Format_dates Pushed for 25.05! Well done everyone, thank you! Nice work everyone! Pushed to 24.11.x for 24.11.01 I had a go at a relase note - I'm not sure if I've got this right, particularly showing the date time field example. That's elegant but definitely not consistent with anything else. IMO it should have been a method of KohaDates (matching the existing pattern: KohaDates.output_preference) Not backporting to 24.05 unless requested (In reply to Katrin Fischer from comment #9) > We have a bit of documentation for formatting dates in the TT notices in the > wiki, can you please update? > > https://wiki.koha-community.org/wiki/Notices_with_Template_Toolkit#Example: > _Format_dates Added a few lines. |
Currently, date formatting in notices with the TT Date plugin is a bit user unfriendly since the plugin has very simplistic parsing that only works on datetime or timestamp fields but not on date only fields (like commonly used fields as borrower.dateexpiry or reserves.reservedate etc.) Should a user of Reports know that? (No.) So this works: [% date.format(borrower.updated_on, "%B %d, %Y") %] But this does not: [% date.format(borrower.dateexpiry, "%B %d, %Y") %] ERROR PROCESSING TEMPLATE: date error - bad time/date string: expects 'h:m:s d:m:y' But we can overcome that by doing something like (for both date as well as datetime): [% KohaDates.datetime_from_string(mydate).strftime("%B %d, %Y") %] # where e.g. mydate == borrower.dateexpiry Note that it is a bit long and somewhat complex (unfriendly). (And btw, datetime_from_string does not check the result of dt_from_string but just assumes to get a DateTime object.) What about the shorter and more intuitive (for both date and datetime fields): [% mydate.strftime("%B %d, %Y") %] Or giving yet another example with a locale different from the default en_US: [% KohaDates.datetime_from_string(mydate).set_locale('nl_NL').strftime("%d %B %Y") %] Would this not read better: [% mydate.strftime("%d %B %Y", "nl_NL") %] The proposed patch adds a TT virtual method strftime that can be run on scalars as in the examples above (for date and datetime). No mention of KohaDates needed, no chaining of set_locale. Wont crash on invalid dates, just passes the original value if date could not be parsed. And importantly, the former KohaDates constructs with strftime still work (going via DateTime->strftime), so backwards compatible. Shorter and easier! What do you think?