Bugzilla – Attachment 99195 Details for
Bug 24455
Add ability to apply Koha formatting to dates from Javascript
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
Bug 24455: Add JS functions to format date and datetime strings
Bug-24455-Add-JS-functions-to-format-date-and-date.patch (text/plain), 7.66 KB, created by
Agustín Moyano
on 2020-02-18 17:55:01 UTC
(
hide
)
Description:
Bug 24455: Add JS functions to format date and datetime strings
Filename:
MIME Type:
Creator:
Agustín Moyano
Created:
2020-02-18 17:55:01 UTC
Size:
7.66 KB
patch
obsolete
>From 10a5c7f7088374f8cffabc5a8d726001e2e07245 Mon Sep 17 00:00:00 2001 >From: Agustin Moyano <agustinmoyano@theke.io> >Date: Fri, 17 Jan 2020 16:19:30 -0300 >Subject: [PATCH] Bug 24455: Add JS functions to format date and datetime > strings > >This patch adds the a js-date-format.inc file on each opac and staff interface (date-format.inc whas not available as name) > >When you include that file in your TT, you'll get the following functions available: > >1. $date(raw_date_string, options) > > This function parses a date string, as produced by an api call, and returns the corresponding date formatted according to 'dateformat' and 'TimeFormat' parameters. > > For example: > > // dateformat: us > // timeformat: 12hr > // Timezone: UTC > > $date('2020-03-23T15:00:00+01:00') // You will get '03/23/2020' > > $date('2020-03-23T15:00:00+01:00', {withtime: true}) // You will get '03/23/2020 14:00' > > $date('2020-03-23T15:00:00+01:00', {withtime: true, tz: 'Europe/Paris'}) // You will get '03/23/2020 15:00+01:00' > > Options: > > * dateformat: override date format as configured in staff interface (accepts also 'rfc3339') > > * timeformat: override time format as configured in staff client (available options are '12hr' and '24hr') > > * withtime: also print the time part (default false) > > * tz: set the timezone > >2. $datetime(raw_date_string, option) > > The same as $date but sets withtime to true > >3. $time(raw_date_string, option) > > The same as $datetime but shows only the time part > >To test you must implement and test bug 20936, where it will be used >--- > Koha/Template/Plugin/KohaDates.pm | 5 ++ > .../prog/en/includes/js-date-format.inc | 62 +++++++++++++++++++ > .../bootstrap/en/includes/js-date-format.inc | 62 +++++++++++++++++++ > 3 files changed, 129 insertions(+) > create mode 100644 koha-tmpl/intranet-tmpl/prog/en/includes/js-date-format.inc > create mode 100644 koha-tmpl/opac-tmpl/bootstrap/en/includes/js-date-format.inc > >diff --git a/Koha/Template/Plugin/KohaDates.pm b/Koha/Template/Plugin/KohaDates.pm >index e87d478adc..042268fc97 100644 >--- a/Koha/Template/Plugin/KohaDates.pm >+++ b/Koha/Template/Plugin/KohaDates.pm >@@ -23,6 +23,7 @@ use Template::Plugin::Filter; > use base qw( Template::Plugin::Filter ); > > use Koha::DateUtils; >+use C4::Context; > our $DYNAMIC = 1; > > sub filter { >@@ -42,4 +43,8 @@ sub output_preference { > return output_pref( @params ); > } > >+sub tz { >+ return C4::Context->tz->name; >+} >+ > 1; >diff --git a/koha-tmpl/intranet-tmpl/prog/en/includes/js-date-format.inc b/koha-tmpl/intranet-tmpl/prog/en/includes/js-date-format.inc >new file mode 100644 >index 0000000000..91a770a5a6 >--- /dev/null >+++ b/koha-tmpl/intranet-tmpl/prog/en/includes/js-date-format.inc >@@ -0,0 +1,62 @@ >+[% USE Koha %] >+[% USE raw %] >+[% USE Asset %] >+[% USE KohaDates %] >+[% Asset.js("lib/moment/moment.min.js") | $raw %] >+[% Asset.js("lib/moment/moment-timezone-with-data-10-year-range.min.js") | $raw %] >+<script> >+ (function() { >+ var def_date_format = '[% Koha.Preference('dateformat') | html %]'; >+ var def_time_format = '[% Koha.Preference('TimeFormat') | html %]'; >+ var def_tz = '[% KohaDates.tz | html %]'; >+ >+ var get_date_pattern = function(format) { >+ var date_pattern = 'YYYY-MM-DD'; >+ if(format == 'us') date_pattern = 'MM/DD/YYYY'; >+ if(format == 'metric') date_pattern = 'DD/MM/YYYY'; >+ if(format == 'dmydot') date_pattern = 'DD.MM.YYYY'; >+ return date_pattern; >+ }; >+ >+ var get_time_pattern = function(format) { >+ var time_pattern = 'HH:mm'; >+ if(format == '12hr') time_pattern = 'hh:mm a'; >+ return time_pattern; >+ }; >+ >+ window.$date = function(value, options) { >+ var tz = (options&&options.tz)||def_tz; >+ var m = moment(value); >+ if(tz) m.tz(tz); >+ >+ var dateformat = (options&&options.dateformat)||def_date_format; >+ var withtime = (options&&options.withtime)||false; >+ >+ if(dateformat=='rfc3339' && withtime) return m.format(); >+ >+ var timeformat = (options&&options.timeformat)||def_time_format; >+ var date_pattern = get_date_pattern(dateformat); >+ var time_pattern = !withtime?'':' '+get_time_pattern(timeformat); >+ >+ return m.format(date_pattern+time_pattern); >+ } >+ >+ window.$datetime = function(value, options) { >+ options = options||{}; >+ options.withtime = true; >+ return $date(value, options); >+ }; >+ >+ window.$time = function(value, options) { >+ var tz = (opitons&&options.tz)||def_tz; >+ var m = moment(value); >+ if(tz) m.tz(tz); >+ >+ var dateformat = (options&&options.dateformat); >+ var timeformat = (dateformat=='rfc3339'&&'24hr')||(options&&options.timeformat)||def_time_format; >+ >+ return m.format(get_time_pattern(timeformat)+(dateformat=='rfc3339'?':ss'+(!m.isUTC()?'Z':''):''))+(dateformat=='rfc3339' && m.isUTC()?'Z':''); >+ } >+ >+ })(); >+</script> >\ No newline at end of file >diff --git a/koha-tmpl/opac-tmpl/bootstrap/en/includes/js-date-format.inc b/koha-tmpl/opac-tmpl/bootstrap/en/includes/js-date-format.inc >new file mode 100644 >index 0000000000..9381684bc6 >--- /dev/null >+++ b/koha-tmpl/opac-tmpl/bootstrap/en/includes/js-date-format.inc >@@ -0,0 +1,62 @@ >+[% USE Koha %] >+[% USE raw %] >+[% USE Asset %] >+[% USE KohaDates %] >+[% Asset.js("lib/moment.min.js") | $raw %] >+[% Asset.js("lib/moment-timezone-with-data-10-year-range.min.js") | $raw %] >+<script> >+ (function() { >+ var def_date_format = '[% Koha.Preference('dateformat') | html %]'; >+ var def_time_format = '[% Koha.Preference('TimeFormat') | html %]'; >+ var def_tz = '[% KohaDates.tz | html %]'; >+ >+ var get_date_pattern = function(format) { >+ var date_pattern = 'YYYY-MM-DD'; >+ if(format == 'us') date_pattern = 'MM/DD/YYYY'; >+ if(format == 'metric') date_pattern = 'DD/MM/YYYY'; >+ if(format == 'dmydot') date_pattern = 'DD.MM.YYYY'; >+ return date_pattern; >+ }; >+ >+ var get_time_pattern = function(format) { >+ var time_pattern = 'HH:mm'; >+ if(format == '12hr') time_pattern = 'hh:mm a'; >+ return time_pattern; >+ }; >+ >+ window.$date = function(value, options) { >+ var tz = (options&&options.tz)||def_tz; >+ var m = moment(value); >+ if(tz) m.tz(tz); >+ >+ var dateformat = (options&&options.dateformat)||def_date_format; >+ var withtime = (options&&options.withtime)||false; >+ >+ if(dateformat=='rfc3339' && withtime) return m.format(); >+ >+ var timeformat = (options&&options.timeformat)||def_time_format; >+ var date_pattern = get_date_pattern(dateformat); >+ var time_pattern = !withtime?'':' '+get_time_pattern(timeformat); >+ >+ return m.format(date_pattern+time_pattern); >+ } >+ >+ window.$datetime = function(value, options) { >+ options = options||{}; >+ options.withtime = true; >+ return $date(value, options); >+ }; >+ >+ window.$time = function(value, options) { >+ var tz = (opitons&&options.tz)||def_tz; >+ var m = moment(value); >+ if(tz) m.tz(tz); >+ >+ var dateformat = (options&&options.dateformat); >+ var timeformat = (dateformat=='rfc3339'&&'24hr')||(options&&options.timeformat)||def_time_format; >+ >+ return m.format(get_time_pattern(timeformat)+(dateformat=='rfc3339'?':ss'+(!m.isUTC()?'Z':''):''))+(dateformat=='rfc3339' && m.isUTC()?'Z':''); >+ } >+ >+ })(); >+</script> >\ No newline at end of file >-- >2.17.1
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
|
Splinter Review
Attachments on
bug 24455
:
97567
|
97568
|
97569
|
97574
|
97575
|
97576
|
97577
|
97578
|
98625
|
99192
|
99195
|
99198
|
99869
|
99870
|
99871
|
99872
|
99873
|
99874
|
99875
|
100119
|
100120
|
100121
|
100122
|
100123
|
100124
|
100125
|
101708
|
101709
|
101710
|
101711
|
101712
|
101713
|
101714
|
101715