Bug 24455 - Date formatting from JS
Summary: Date formatting from JS
Status: Pushed to master
Alias: None
Product: Koha
Classification: Unclassified
Component: OPAC (show other bugs)
Version: master
Hardware: All All
: P5 - low enhancement (vote)
Assignee: Agustín Moyano
QA Contact: Testopia
URL:
Keywords:
: 24650 (view as bug list)
Depends on:
Blocks: 20936 24608 24980 24987
  Show dependency treegraph
 
Reported: 2020-01-17 18:12 UTC by Agustín Moyano
Modified: 2020-04-03 22:20 UTC (History)
6 users (show)

See Also:
Change sponsored?: ---
Patch complexity: Small patch
Who signed the patch off:
Text to go in the release notes:
This patchset lays the foundations for applying date formatting as described in the koha system preferences to datetimes returned by the API in RFC3339 format.
Version(s) released in:
20.05.00


Attachments
Bug 24455: Add moment library in opac (52.83 KB, patch)
2020-01-17 20:20 UTC, Agustín Moyano
Details | Diff | Splinter Review
Bug 24455: Add moment-timezone library in opac (32.98 KB, patch)
2020-01-17 20:20 UTC, Agustín Moyano
Details | Diff | Splinter Review
Bug 24455: Add JS functions to format date and datetime strings (4.92 KB, patch)
2020-01-17 20:20 UTC, Agustín Moyano
Details | Diff | Splinter Review
Bug 24455: Add moment library in opac (52.83 KB, patch)
2020-01-17 21:09 UTC, Agustín Moyano
Details | Diff | Splinter Review
Bug 24455: Add moment library in staff (52.84 KB, patch)
2020-01-17 21:09 UTC, Agustín Moyano
Details | Diff | Splinter Review
Bug 24455: Add moment-timezone library in opac (32.98 KB, patch)
2020-01-17 21:09 UTC, Agustín Moyano
Details | Diff | Splinter Review
Bug 24455: Add moment-timezone library in staff (32.99 KB, patch)
2020-01-17 21:09 UTC, Agustín Moyano
Details | Diff | Splinter Review
Bug 24455: Add JS functions to format date and datetime strings (7.71 KB, patch)
2020-01-17 21:09 UTC, Agustín Moyano
Details | Diff | Splinter Review
Bug 24455: Add JS functions to format date and datetime strings (7.66 KB, patch)
2020-02-09 20:53 UTC, Agustín Moyano
Details | Diff | Splinter Review
test tz (2.07 KB, patch)
2020-02-18 15:49 UTC, Jonathan Druart
Details | Diff | Splinter Review
Bug 24455: Add JS functions to format date and datetime strings (7.66 KB, patch)
2020-02-18 17:55 UTC, Agustín Moyano
Details | Diff | Splinter Review
[DO NOT PUSH] Bug 24455: test timezone (1.38 KB, patch)
2020-02-18 18:31 UTC, Agustín Moyano
Details | Diff | Splinter Review
Bug 24455: Add moment library in opac (52.90 KB, patch)
2020-03-02 10:28 UTC, Andrew Isherwood
Details | Diff | Splinter Review
Bug 24455: Add moment library in staff (52.91 KB, patch)
2020-03-02 10:28 UTC, Andrew Isherwood
Details | Diff | Splinter Review
Bug 24455: Add moment-timezone library in opac (33.06 KB, patch)
2020-03-02 10:28 UTC, Andrew Isherwood
Details | Diff | Splinter Review
Bug 24455: Add moment-timezone library in staff (33.06 KB, patch)
2020-03-02 10:29 UTC, Andrew Isherwood
Details | Diff | Splinter Review
Bug 24455: Add JS functions to format date and datetime strings (7.75 KB, patch)
2020-03-02 10:29 UTC, Andrew Isherwood
Details | Diff | Splinter Review
Bug 24455: test timezone (1.37 KB, patch)
2020-03-02 10:29 UTC, Andrew Isherwood
Details | Diff | Splinter Review
Bug 24455: [DO NOT PUSH] test timezone (1.38 KB, patch)
2020-03-02 10:32 UTC, Andrew Isherwood
Details | Diff | Splinter Review
Bug 24455: Add moment library in opac (52.97 KB, patch)
2020-03-04 13:52 UTC, Kyle M Hall
Details | Diff | Splinter Review
Bug 24455: Add moment library in staff (52.98 KB, patch)
2020-03-04 13:52 UTC, Kyle M Hall
Details | Diff | Splinter Review
Bug 24455: Add moment-timezone library in opac (33.12 KB, patch)
2020-03-04 13:52 UTC, Kyle M Hall
Details | Diff | Splinter Review
Bug 24455: Add moment-timezone library in staff (33.13 KB, patch)
2020-03-04 13:52 UTC, Kyle M Hall
Details | Diff | Splinter Review
Bug 24455: Add JS functions to format date and datetime strings (7.80 KB, patch)
2020-03-04 13:52 UTC, Kyle M Hall
Details | Diff | Splinter Review
Bug 24455: [DO NOT PUSH] test timezone (1.45 KB, patch)
2020-03-04 13:52 UTC, Kyle M Hall
Details | Diff | Splinter Review
Bug 24455: Add Moment.js to About page (1.19 KB, patch)
2020-03-04 13:52 UTC, Kyle M Hall
Details | Diff | Splinter Review
Bug 24455: Add moment library in opac (53.02 KB, patch)
2020-03-25 13:42 UTC, Martin Renvoize
Details | Diff | Splinter Review
Bug 24455: Add moment library in staff (53.02 KB, patch)
2020-03-25 13:42 UTC, Martin Renvoize
Details | Diff | Splinter Review
Bug 24455: Add moment-timezone library in opac (33.17 KB, patch)
2020-03-25 13:42 UTC, Martin Renvoize
Details | Diff | Splinter Review
Bug 24455: Add moment-timezone library in staff (33.17 KB, patch)
2020-03-25 13:42 UTC, Martin Renvoize
Details | Diff | Splinter Review
Bug 24455: Add JS functions to format date and datetime strings (7.84 KB, patch)
2020-03-25 13:42 UTC, Martin Renvoize
Details | Diff | Splinter Review
Bug 24455: [DO NOT PUSH] test timezone (1.50 KB, patch)
2020-03-25 13:42 UTC, Martin Renvoize
Details | Diff | Splinter Review
Bug 24455: Add Moment.js to About page (1.24 KB, patch)
2020-03-25 13:42 UTC, Martin Renvoize
Details | Diff | Splinter Review
Bug 24455: (follow-up) Document function (1.51 KB, patch)
2020-03-25 13:42 UTC, Martin Renvoize
Details | Diff | Splinter Review

Note You need to log in before you can comment on or make changes to this bug.
Description Agustín Moyano 2020-01-17 18:12:44 UTC
API calls in OPAC brings raw JSON.

We need a way to format data into the configured date from JS.
Comment 1 Agustín Moyano 2020-01-17 20:20:31 UTC Comment hidden (obsolete)
Comment 2 Agustín Moyano 2020-01-17 20:20:36 UTC Comment hidden (obsolete)
Comment 3 Agustín Moyano 2020-01-17 20:20:39 UTC Comment hidden (obsolete)
Comment 4 Agustín Moyano 2020-01-17 21:09:42 UTC Comment hidden (obsolete)
Comment 5 Agustín Moyano 2020-01-17 21:09:46 UTC Comment hidden (obsolete)
Comment 6 Agustín Moyano 2020-01-17 21:09:50 UTC Comment hidden (obsolete)
Comment 7 Agustín Moyano 2020-01-17 21:09:55 UTC Comment hidden (obsolete)
Comment 8 Agustín Moyano 2020-01-17 21:09:58 UTC Comment hidden (obsolete)
Comment 9 Agustín Moyano 2020-02-09 20:53:23 UTC Comment hidden (obsolete)
Comment 10 Jonathan Druart 2020-02-18 09:17:04 UTC
Typo:
+            var tz = (opitons&&options.tz)||def_tz;

How is this related to bug 24650?
It seems that it achieves the same goal without extra lib and very few lines.
Comment 11 Agustín Moyano 2020-02-18 15:11:12 UTC
(In reply to Jonathan Druart from comment #10)
> Typo:
> +            var tz = (opitons&&options.tz)||def_tz;

Ups, seems I forgot to attach the patch where I fixed that.. 
 
> How is this related to bug 24650?
> It seems that it achieves the same goal without extra lib and very few lines.

I hadn't seen that bug.. seems this bug is previous to that one.. but the main difference is that this implementation takes into account the timezone, so if a date comes from the api with UTC timezone it will be correctly translated to your current timezone.
Comment 12 Jonathan Druart 2020-02-18 15:49:45 UTC Comment hidden (obsolete)
Comment 13 Jonathan Druart 2020-02-18 15:52:35 UTC
(In reply to Agustín Moyano from comment #11)
> (In reply to Jonathan Druart from comment #10)
> > How is this related to bug 24650?
> > It seems that it achieves the same goal without extra lib and very few lines.
> 
> I hadn't seen that bug.. seems this bug is previous to that one.. but the
> main difference is that this implementation takes into account the timezone,
> so if a date comes from the api with UTC timezone it will be correctly
> translated to your current timezone.

What do you mean by correctly translated to your current timezone? Do you mean the TZ from the server?

I tried this patch, with Buenos Aires in the timezone config, I see:
2020-02-18T12:46:05-03:00
02/18/2020 16:46

(current date of the server is 16:46)
Comment 14 Agustín Moyano 2020-02-18 17:55:01 UTC Comment hidden (obsolete)
Comment 15 Agustín Moyano 2020-02-18 17:57:49 UTC
(In reply to Jonathan Druart from comment #13)
> (In reply to Agustín Moyano from comment #11)
> > (In reply to Jonathan Druart from comment #10)
> > > How is this related to bug 24650?
> > > It seems that it achieves the same goal without extra lib and very few lines.
> > 
> > I hadn't seen that bug.. seems this bug is previous to that one.. but the
> > main difference is that this implementation takes into account the timezone,
> > so if a date comes from the api with UTC timezone it will be correctly
> > translated to your current timezone.
> 
> What do you mean by correctly translated to your current timezone? Do you
> mean the TZ from the server?
> 
> I tried this patch, with Buenos Aires in the timezone config, I see:
> 2020-02-18T12:46:05-03:00
> 02/18/2020 16:46
> 
> (current date of the server is 16:46)

according to the patch your uploaded, I believe you where using the code of bug 24650.

I'll try to upload a patch with [DO NOT PUSH] with a test case
Comment 16 Agustín Moyano 2020-02-18 18:31:52 UTC Comment hidden (obsolete)
Comment 17 Jonathan Druart 2020-02-19 08:05:51 UTC
So, in your opinion, do we need both approaches or only one? (bug 24455 and bug 24650)
I miss the real-life use case.
Comment 18 Agustín Moyano 2020-02-19 11:16:16 UTC
(In reply to Jonathan Druart from comment #17)
> So, in your opinion, do we need both approaches or only one? (bug 24455 and
> bug 24650)

I believe both do basically the same

> I miss the real-life use case.

https://bugs.koha-community.org/bugzilla3/attachment.cgi?id=98630&action=diff#a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-holdsrecord.tt_sec1

In line 170, I use it to format dates retrieved from the API into datatables.
Comment 19 Andrew Isherwood 2020-03-02 10:21:19 UTC
I'd say this bug looks far more useful than mine in bug 24650, which only solves a single problem I had. If we are moving more towards API driven interfaces, the need to reliably parse and manipulate dates in JS is crucial. Since moment.js is the defacto way of doing this in JS, it seems by far the best way forward.

I'll apply and sign this off now.
Comment 20 Andrew Isherwood 2020-03-02 10:28:50 UTC
Created attachment 99869 [details] [review]
Bug 24455: Add moment library in opac

This patch adds koha-tmpl/opac-tmpl/bootstrap/lib/moment.min.js in order to use the moment library (https://momentjs.com/)

Signed-off-by: Andrew Isherwood <andrew.isherwood@ptfs-europe.com>
Comment 21 Andrew Isherwood 2020-03-02 10:28:54 UTC
Created attachment 99870 [details] [review]
Bug 24455: Add moment library in staff

This patch adds koha-tmpl/intranet-tmpl/lib/moment/moment.min.js in order to use the moment library (https://momentjs.com/)

Signed-off-by: Andrew Isherwood <andrew.isherwood@ptfs-europe.com>
Comment 22 Andrew Isherwood 2020-03-02 10:28:57 UTC
Created attachment 99871 [details] [review]
Bug 24455: Add moment-timezone library in opac

This patch adds koha-tmpl/opac-tmpl/bootstrap/lib/moment-timezone-with-data-10-year-range.min.js in order to use the moment library with timezones (https://momentjs.com/timezone/)

Signed-off-by: Andrew Isherwood <andrew.isherwood@ptfs-europe.com>
Comment 23 Andrew Isherwood 2020-03-02 10:29:00 UTC
Created attachment 99872 [details] [review]
Bug 24455: Add moment-timezone library in staff

This patch adds koha-tmpl/intranet-tmpl/lib/moment/moment-timezone-with-data-10-year-range.min.js in order to use the moment library with timezones (https://momentjs.com/timezone/)

Signed-off-by: Andrew Isherwood <andrew.isherwood@ptfs-europe.com>
Comment 24 Andrew Isherwood 2020-03-02 10:29:03 UTC
Created attachment 99873 [details] [review]
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

Signed-off-by: Andrew Isherwood <andrew.isherwood@ptfs-europe.com>
Comment 25 Andrew Isherwood 2020-03-02 10:29:06 UTC Comment hidden (obsolete)
Comment 26 Andrew Isherwood 2020-03-02 10:32:11 UTC
Created attachment 99875 [details] [review]
Bug 24455: [DO NOT PUSH] test timezone

This is just a test.. I added the .inc file to adimn's main page

To test:

1. go to Koha main page and open chrome or firefox console
2. type

   $datetime('2020-02-18T12:46:05Z', {tz: 'America/Argentina/Buenos_Aires'})

   and you should get

   "02/18/2020 09:46"

   because America/Argentina/Buenos_Aires is -03:00

3. type

   $datetime('2020-02-18T12:46:05Z', {tz: 'Asia/Shanghai'})

   and you should get

   "02/18/2020 20:46"

   because Asia/Shanghai is +08:00

4. type

   $datetime('2020-02-18T12:46:05Z')

   and it should be translated to your koha instance configured timezone.
Comment 27 Andrew Isherwood 2020-03-02 10:33:39 UTC
Signed off on the basis of passing the tests in comment #16
Comment 28 Katrin Fischer 2020-03-02 10:38:04 UTC
Thx, Andrew! If this solves your need - should we mark bug 24650 duplicate or resolved moved?
Comment 29 Andrew Isherwood 2020-03-02 10:50:00 UTC
*** Bug 24650 has been marked as a duplicate of this bug. ***
Comment 30 Andrew Isherwood 2020-03-02 10:51:12 UTC
(In reply to Katrin Fischer from comment #28)
> Thx, Andrew! If this solves your need - should we mark bug 24650 duplicate
> or resolved moved?

Done :)
Comment 31 Kyle M Hall 2020-03-04 13:52:16 UTC
Created attachment 100119 [details] [review]
Bug 24455: Add moment library in opac

This patch adds koha-tmpl/opac-tmpl/bootstrap/lib/moment.min.js in order to use the moment library (https://momentjs.com/)

Signed-off-by: Andrew Isherwood <andrew.isherwood@ptfs-europe.com>

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Comment 32 Kyle M Hall 2020-03-04 13:52:35 UTC
Created attachment 100120 [details] [review]
Bug 24455: Add moment library in staff

This patch adds koha-tmpl/intranet-tmpl/lib/moment/moment.min.js in order to use the moment library (https://momentjs.com/)

Signed-off-by: Andrew Isherwood <andrew.isherwood@ptfs-europe.com>

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Comment 33 Kyle M Hall 2020-03-04 13:52:37 UTC
Created attachment 100121 [details] [review]
Bug 24455: Add moment-timezone library in opac

This patch adds koha-tmpl/opac-tmpl/bootstrap/lib/moment-timezone-with-data-10-year-range.min.js in order to use the moment library with timezones (https://momentjs.com/timezone/)

Signed-off-by: Andrew Isherwood <andrew.isherwood@ptfs-europe.com>

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Comment 34 Kyle M Hall 2020-03-04 13:52:40 UTC
Created attachment 100122 [details] [review]
Bug 24455: Add moment-timezone library in staff

This patch adds koha-tmpl/intranet-tmpl/lib/moment/moment-timezone-with-data-10-year-range.min.js in order to use the moment library with timezones (https://momentjs.com/timezone/)

Signed-off-by: Andrew Isherwood <andrew.isherwood@ptfs-europe.com>

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Comment 35 Kyle M Hall 2020-03-04 13:52:43 UTC
Created attachment 100123 [details] [review]
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

Signed-off-by: Andrew Isherwood <andrew.isherwood@ptfs-europe.com>

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Comment 36 Kyle M Hall 2020-03-04 13:52:47 UTC
Created attachment 100124 [details] [review]
Bug 24455: [DO NOT PUSH] test timezone

This is just a test.. I added the .inc file to adimn's main page

To test:

1. go to Koha main page and open chrome or firefox console
2. type

   $datetime('2020-02-18T12:46:05Z', {tz: 'America/Argentina/Buenos_Aires'})

   and you should get

   "02/18/2020 09:46"

   because America/Argentina/Buenos_Aires is -03:00

3. type

   $datetime('2020-02-18T12:46:05Z', {tz: 'Asia/Shanghai'})

   and you should get

   "02/18/2020 20:46"

   because Asia/Shanghai is +08:00

4. type

   $datetime('2020-02-18T12:46:05Z')

   and it should be translated to your koha instance configured timezone.

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Comment 37 Kyle M Hall 2020-03-04 13:52:50 UTC
Created attachment 100125 [details] [review]
Bug 24455: Add Moment.js to About page

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Comment 38 Jonathan Druart 2020-03-06 13:25:20 UTC
WARNING: This must NOT be pushed before I have a better clarity of the needs.
This does not need to be pushed before one of the 2 dependent bugs anyway.

See for instance bug 24608 comment 26.

(This bug does not need to be marked as blocked, the dep is the other way around)
Comment 39 Martin Renvoize 2020-03-18 11:04:48 UTC
I'm facing similar challenges here as I did with bug 24454.

Now I'm wrapping my head around it, there seems to be a fair bit of duplicate code introduced here that already exists in koha-tmpl/intranet-tmpl/prog/en/includes/calendar.inc

I'm by no means against introducing moment.js, I like the idea of using well maintained and thought out third party libraries and the was of accessing the functions written here is nice and elegant.. much like the way you introduced elegant accessors for currency handling in bug 24454 (I'd like to see some of those idea's taken forward too).

But.. I don't like that we're duplicating logic and making it somewhat less clear which functions should be used for the job so would like some form of merge/aggregation of such functionality and a clear documentation somewhere for how to use it before I'm happy pushing this.
Comment 40 Martin Renvoize 2020-03-18 11:24:48 UTC
Examples on things that need clarifying..

1) Is the intention to always pass in a 'String' (and if so, of what format) to the new $date, $datetime and $time window functions.. or are you deliberately allow Date prototypes to be passed?
2) You allow to pass in a timezone in the options, but the string value may also include an offset if it's a RFC3339 so how are the two being ratified..?
Comment 41 Martin Renvoize 2020-03-18 11:29:01 UTC
I'd be tempted to have some clearer function names.. like fromkohadate which would take a syspref formatted date string and return a moment prototype and tokohadate which would take a moment and return a string formatted per koha sysprefs ?
Comment 42 Agustín Moyano 2020-03-18 15:58:01 UTC
(In reply to Martin Renvoize from comment #39)
> I'm facing similar challenges here as I did with bug 24454.
> 
> Now I'm wrapping my head around it, there seems to be a fair bit of
> duplicate code introduced here that already exists in
> koha-tmpl/intranet-tmpl/prog/en/includes/calendar.inc
> 
> I'm by no means against introducing moment.js, I like the idea of using well
> maintained and thought out third party libraries and the was of accessing
> the functions written here is nice and elegant.. much like the way you
> introduced elegant accessors for currency handling in bug 24454 (I'd like to
> see some of those idea's taken forward too).
> 
> But.. I don't like that we're duplicating logic and making it somewhat less
> clear which functions should be used for the job so would like some form of
> merge/aggregation of such functionality and a clear documentation somewhere
> for how to use it before I'm happy pushing this.

Hello Martin, functions in calendar.inc has similar logic, but they do the oposite job of this functions.

calendar.inc function's inputs are date/datetime strings as specified by dateformat preference and returns a JS Date object.

This function's inputs are date/datetime string as returned from the api (RFC3339) and returns a date/datetime/time string as specified by dateformat preference, and translated to corresponding timezone.
Comment 43 Agustín Moyano 2020-03-18 16:00:11 UTC
(In reply to Agustín Moyano from comment #42)
> (In reply to Martin Renvoize from comment #39)
> > I'm facing similar challenges here as I did with bug 24454.
> > 
> > Now I'm wrapping my head around it, there seems to be a fair bit of
> > duplicate code introduced here that already exists in
> > koha-tmpl/intranet-tmpl/prog/en/includes/calendar.inc
> > 
> > I'm by no means against introducing moment.js, I like the idea of using well
> > maintained and thought out third party libraries and the was of accessing
> > the functions written here is nice and elegant.. much like the way you
> > introduced elegant accessors for currency handling in bug 24454 (I'd like to
> > see some of those idea's taken forward too).
> > 
> > But.. I don't like that we're duplicating logic and making it somewhat less
> > clear which functions should be used for the job so would like some form of
> > merge/aggregation of such functionality and a clear documentation somewhere
> > for how to use it before I'm happy pushing this.
> 
> Hello Martin, functions in calendar.inc has similar logic, but they do the
> oposite job of this functions.
> 
> calendar.inc function's inputs are date/datetime strings as specified by
> dateformat preference and returns a JS Date object.
> 
> This function's inputs are date/datetime string as returned from the api
> (RFC3339) and returns a date/datetime/time string as specified by dateformat
> preference, and translated to corresponding timezone.

I believe Calendar.inc functions are used to get a date from tt variable, and put that date into a jquery calendar input.

This functions should be used to format dates from the api
Comment 44 Agustín Moyano 2020-03-18 16:09:04 UTC
(In reply to Martin Renvoize from comment #40)
> Examples on things that need clarifying..
> 
> 1) Is the intention to always pass in a 'String' (and if so, of what format)
> to the new $date, $datetime and $time window functions.. or are you
> deliberately allow Date prototypes to be passed?

It was designed to transform dates from the api, so input is always RFC3339

> 2) You allow to pass in a timezone in the options, but the string value may
> also include an offset if it's a RFC3339 so how are the two being ratified..?

For example, if you pass the following value '2020-03-18T07:00:00-03:00' and you set the timezone for an equivalent of +03:00 the result of calling $datetime should be '2020-03-18T13:00:00+03:00' each one being equivalent to '2020-03-18T10:00:00Z'
Comment 45 Martin Renvoize 2020-03-24 14:52:12 UTC
I'm looking to add a followup of my own here to clarify the situation.. struggled to put it into words when talking to Agustin so I'm going to let code speak ;)
Comment 46 Martin Renvoize 2020-03-25 13:42:33 UTC
Created attachment 101708 [details] [review]
Bug 24455: Add moment library in opac

This patch adds koha-tmpl/opac-tmpl/bootstrap/lib/moment.min.js in order to use the moment library (https://momentjs.com/)

Signed-off-by: Andrew Isherwood <andrew.isherwood@ptfs-europe.com>
Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Comment 47 Martin Renvoize 2020-03-25 13:42:36 UTC
Created attachment 101709 [details] [review]
Bug 24455: Add moment library in staff

This patch adds koha-tmpl/intranet-tmpl/lib/moment/moment.min.js in order to use the moment library (https://momentjs.com/)

Signed-off-by: Andrew Isherwood <andrew.isherwood@ptfs-europe.com>
Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Comment 48 Martin Renvoize 2020-03-25 13:42:40 UTC
Created attachment 101710 [details] [review]
Bug 24455: Add moment-timezone library in opac

This patch adds koha-tmpl/opac-tmpl/bootstrap/lib/moment-timezone-with-data-10-year-range.min.js in order to use the moment library with timezones (https://momentjs.com/timezone/)

Signed-off-by: Andrew Isherwood <andrew.isherwood@ptfs-europe.com>
Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Comment 49 Martin Renvoize 2020-03-25 13:42:44 UTC
Created attachment 101711 [details] [review]
Bug 24455: Add moment-timezone library in staff

This patch adds koha-tmpl/intranet-tmpl/lib/moment/moment-timezone-with-data-10-year-range.min.js in order to use the moment library with timezones (https://momentjs.com/timezone/)

Signed-off-by: Andrew Isherwood <andrew.isherwood@ptfs-europe.com>
Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Comment 50 Martin Renvoize 2020-03-25 13:42:47 UTC
Created attachment 101712 [details] [review]
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

Signed-off-by: Andrew Isherwood <andrew.isherwood@ptfs-europe.com>
Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Comment 51 Martin Renvoize 2020-03-25 13:42:50 UTC
Created attachment 101713 [details] [review]
Bug 24455: [DO NOT PUSH] test timezone

This is just a test.. I added the .inc file to adimn's main page

To test:

1. go to Koha main page and open chrome or firefox console
2. type

   $datetime('2020-02-18T12:46:05Z', {tz: 'America/Argentina/Buenos_Aires'})

   and you should get

   "02/18/2020 09:46"

   because America/Argentina/Buenos_Aires is -03:00

3. type

   $datetime('2020-02-18T12:46:05Z', {tz: 'Asia/Shanghai'})

   and you should get

   "02/18/2020 20:46"

   because Asia/Shanghai is +08:00

4. type

   $datetime('2020-02-18T12:46:05Z')

   and it should be translated to your koha instance configured timezone.

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Comment 52 Martin Renvoize 2020-03-25 13:42:54 UTC
Created attachment 101714 [details] [review]
Bug 24455: Add Moment.js to About page

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Comment 53 Martin Renvoize 2020-03-25 13:42:57 UTC
Created attachment 101715 [details] [review]
Bug 24455: (follow-up) Document function

This patch just adds some minimal documentation to the main exported JS
function.

Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Comment 54 Martin Renvoize 2020-03-25 13:47:12 UTC
Back to PQA.. I've added a followup which documents the main function.

I'm happy to add moment.js as it's not a huge library and is pretty much the defacto library used in the wild for datetime manipulation.

I still think it's somewhat unclear how Calendar.inc and this include differ from the end developers perspective and would love to see something akin to 'date_inputs.inc' and 'date_outputs.inc' to make it clear how the two relate to each other.. and perhaps even have some of the functionality shared.

I'll try to work out some followup bugs for these parts however.
Comment 55 Martin Renvoize 2020-03-25 13:55:10 UTC
Nice work everyone!

Pushed to master for 20.05
Comment 56 Joy Nelson 2020-04-03 22:20:42 UTC
enhancement not backported to 19.11.x