DataTables / DataTablesSrc

DataTables source repository
https://datatables.net
MIT License
622 stars 422 forks source link

Feature suggestion: Timezone support for datetime() renderer #289

Open xJuvi opened 1 month ago

xJuvi commented 1 month ago

Hey there,

i store all timestamps as unix timestamp in my database. When i display them in a form or other page, they will be displayed in UTC+2 (german summertime). Since a few weeks i doesn't convert the timestamps before i display them with datatables. But all timestamps will be displayed in UTC+0. So the time is two hours in the past.

I tried a look in the renderer source code.....

https://github.com/DataTables/DataTablesSrc/blob/18e89b984e01160631890b326914f3f98bbdf629/js/ext/ext.helpers.js#L43-L68

I think where wo use luxon or moment it was very easy to convert the time object in another timezone. The are several ways possible. As fourth parameter we can add the destination timezone. And if we want, we can also add a fifth parameter as current timezone.

Are there any plans for this? Is it allowed to make a short pull request to add this feature? I really need it. Otherwise the datetime renderer isn't useful for me and i need to write my own one. But my suggested changes are no breaking changes and maybe will help some other people.

Kind regards

AllanJard commented 1 month ago

No plans for this at the moment, as I think you are the first to ask for it, but I think it is a fair request, and a PR would be welcome. There might be a number of nuances though, for example what if the date/time given doesn't include timezone information. Do we assume that it is UTC, or local time?

xJuvi commented 1 month ago

Difficult question. Thanks for you quick reply. It only relates when no timezone information is added and a conversion should be done. In any other situation wie can only change the format like now.

I think the best way for timezone conversion is to use UTC as default. Then every user has the same result. Otherwise users with different settings have different results. So if you input isn't UTC and you want to convert the timezone, you must also add the current given timezone. Or do you have another opinion?

AllanJard commented 1 month ago

I think that is probably fair. The results could be rather unpredictable otherwise.

So:

I'm a little worried about how much code this might add to the library, and there will need to be a good number of unit tests for it (the unit tests I think actually only pass in GMT timezone already at the moment - that's something I've been meaning to look into!).

In principle I think it is a good idea to handle this. It might not be as simple as passing a timezone as an extra parameter though. There is an argument to say that the middle two should be considered invalid inputs and shouldn't be considered... But I just know devs will feed it those combinations.

xJuvi commented 1 month ago

Ok well. That's so complicated. I read the source code again an checked the momentjs and luxon docu.

At this time you use this for momentjs. There, the given time is handled as UTC+0. https://github.com/DataTables/DataTablesSrc/blob/18e89b984e01160631890b326914f3f98bbdf629/js/ext/ext.helpers.js#L49-L49

With this small optimization, momentjs handle the given time as local time:

dt = __moment( d, format, locale, true );

Luxon use the same handle but it's correct imlemented.

This small change solve my problem because all my users are live in germany. I think this change isn't a feature request more a bugfix. Did you agree?

To change the time to another timezone is bit more difficut because we can't use the logic from momentjs an luxon. Maybe it's a feature for a later release? With both libraries it isn't much code, but a good testig id needed.

If you confirm my small suggestion to remove the moment.utc() function against moment() i can create a fix.

EDIT: Here the link to the momentjs docu: https://momentjs.com/docs/#/parsing/

AllanJard commented 1 month ago

I think that is okay! I really need to add some unit tests to check. Feel free to send a PR and when I get a chance to write some tests, I'll merge it in.