Skip to content
This repository has been archived by the owner on Sep 17, 2018. It is now read-only.

Store Date as UTC and show it in server time zone #57

Merged
merged 7 commits into from
Jul 28, 2018

Conversation

astridx
Copy link
Contributor

@astridx astridx commented Jul 26, 2018

Pull Request for Issue #52 (comment).

Summary of Changes

The values for modified and created date are stored in UTC, but shown in server time zone.
If your server time zone is Australia
global configuration test administration
you see this time:
templates customise cassiopeia test administration 4
If your server time zone is in Europe
global configuration test administration 1
you see this time:
templates customise cassiopeia test administration 5

For Europe you see in the view this values:
templates customise cassiopeia test administration 6
In the database the UTC values are stored
localhost localhost joomla_db a_template_overrides phpmyadmin 4 5 4 1deb2ubuntu2

{
if ((int) $result->created_date)
{
$created_date = new Date($result->created_date);
Copy link
Contributor

@laoneo laoneo Jul 26, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In what for a timezone is $result->created_date?

@@ -336,10 +336,8 @@ private function saveOverrides($pks)
{
$insertQuery->clear('values');

$tz = new \DateTimeZone(Factory::getApplication()->get('offset'));
$date = new Date('now');
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@laoneo If I am right the timezone of $date = new Date('now');
is UTC as this is the default time zone.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess so too.

@anuragteapot
Copy link
Member

anuragteapot commented Jul 26, 2018

@astridx Can we do this without creating a new loop ? Mean, here

// Get timezone for converting the created and modified date.
$tz = new \DateTimeZone(Factory::getApplication()->get('offset'));

foreach ($results as $result)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This loop

@astridx
Copy link
Contributor Author

astridx commented Jul 26, 2018

Can we do this without creating a new loop ? Mean, here

@Anu1601CS I have put the code in the already existing loop.

@laoneo Is the handling of the date now like it should be done in Joomla?

if ((int) $pk->created_date)
{
$created_date = new Date($pk->created_date);
$created_date->setTimezone($tz);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These setTimezone calls should not be necessary. Instantiating a new Joomla\CMS\Date\Date object internally converts the timestamp to UTC.

{
$created_date = new Date($pk->created_date);
$created_date->setTimezone($tz);
$pk->created_date = $created_date->toSql(true);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

toSql(true) means you're getting the timestamp in the correct SQL format but localized, so this is converting from UTC to whatever timezone you're setting above.

Look at the check methods of various Joomla\CMS\Table\Table subclasses for how created and modified dates are handled.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mbabker Thank you for your help. But if I do not set the timezone, the date will be displayed in UTC.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, missed this wasn't a save operation.

The timezone shouldn't be normalized in models. This should be done in views/layouts. So you should use the JHtml::date() helper (as is done elsewhere in core) for this, as that will adjust the timezone for the user's configuration.

@astridx
Copy link
Contributor Author

astridx commented Jul 26, 2018

I moved normalizing of the timezone to the view

@anuragteapot
Copy link
Member

@dgrammatiko Javascript test fails with.

ReferenceError: customElements is not defined
at http://localhost:9876/base/media/system/webcomponents/js/joomla-field-switcher-es5.js?8673eb07a3064c4b3028e5260638e1e20334404b:265
Firefox 59.0.0 (Ubuntu 0.0.0) ERROR
ReferenceError: customElements is not defined
at /drone/src/github.com/joomla-projects/gsoc18_override_management/media/system/webcomponents/js/joomla-field-switcher-es5.js:265

@@ -57,13 +57,15 @@
<a class="hasTooltip" href="<?php echo Route::_('index.php?option=com_templates&view=template&id=' . (int) $value->extension_id . '&file=' . $value->hash_id); ?>" title="<?php echo Text::_('JACTION_EDIT'); ?>"><?php echo base64_decode($value->hash_id); ?></a>
</td>
<td>
<?php echo $value->created_date; ?>
<?php $created_date = $value->created_date; ?>
<?php echo $created_date > 0 ? HTMLHelper::_('date', $created_date, 'Y-m-d H:i:s') : '-'; ?>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are language constants for date formats. I'm not sure but the custom field is using them.

@astridx
Copy link
Contributor Author

astridx commented Jul 27, 2018

Now I use language constants for date format.

@dgrammatiko
Copy link
Contributor

@Anu1601CS the js tests should be ok for now

@laoneo laoneo merged commit 71fc7bb into master Jul 28, 2018
@laoneo laoneo deleted the astrid_correctingDates branch July 28, 2018 04:57
@laoneo
Copy link
Contributor

laoneo commented Jul 28, 2018

Thanks.

@anuragteapot
Copy link
Member

@astridx Thank you very much :)

@anuragteapot anuragteapot added the enhancement Small enhancemenet label Jul 28, 2018
@astridx
Copy link
Contributor Author

astridx commented Jul 28, 2018

Thanks. And now I understand why we store date in UTC. Up to know this was not clear to me. (I even found this unnecessary work to store a value in UTC just to convert the value back to the time zone later).
But now I had a closer look and I understand, that we do this to show the value in the user time zone - if this is set. If the user time zone is not set, we show it in the server time zone. And

  1. it's easier to do this from the fixed time-zone UTC than always need to count between different time zones.
  2. in the case of stored data you do not know in which time zone the date is stored - because it could be, that someone changed the timezone in the Joomla config.

I hope I understood this right.

@laoneo Is everything in this repo correct again? I mean, are all issues you see here are now OK: #52 (comment)

By the way: Is there a documentation about that? I only found: https://docs.joomla.org/How_to_use_JDate what is not very detailed ....

@laoneo
Copy link
Contributor

laoneo commented Jul 28, 2018

Yes, that's the way to go with dates, always UTC.

zero-24 pushed a commit that referenced this pull request Sep 6, 2018
* Load correct core files of override files (#2)

Start implements loadcorefile() in administrator/components/com_templates/Model/TemplateModel.php

* CS (#3) Coding Standards

* codingstandards

* codingstandards (#4)

* Test (#6)

Phase 2 (2 part) Mechanism to find correct core file and implementation.

* Remove Notice: Only available for html-folder

* Remove Warning if core file not found (#11)

Thanks.
So one part of the issue #12 is done.

* Implement the diff view in template manager 

Implement the diff view in template manager

* coding standard (#17)

* fix diff (#18) Fix bug in path in case of administrator template override.

Fix bug in path in case of administrator template override.

* Notification after update and TEST (#16)

Find changed files of overridden files and show message.

* coding standard (#21)

* correction

* correction (#26)

* Correcthtmlpath (#27)

* correction

* change oldhtml to newhtml

* List of updated override files. (#30)

* addcss (#34)

* Final Product  (#39)

Core and Diff view
Updated override history list.
Quick icon notification plugin.
Override control plugin.

* save 3 lines :)

* New feature show status. (#47)

show status in com_template view templates

* link

* corrected namespace

* Button to Switch (#35)

* wip add Switcher

* wip style switcher

* wip style switch make inline and change on off text

* wip start with js

* wip js

* wip delete buttons and make js more robust

* wip save to storage

* wip delete old code

* wip

* wip lint

* wip css

* set default value for switcher

* wip make switcher blue

* wip

* wip

* build

* correct names

* create new functions

* fist test code

* use onchange

* undo installer.min.js

* add forgotten new line at the end of css file

* correct align

* correct compare.es6 - only deleted the toggle part

* correct compare.js - only deleted the toggle part

* wip

* reduce timeout

* wrap in funcitons

* wip

* add use strict to both js-files(compare and toggle)

* add the timeout value of 500 again, because 200 are not enought in my case

* use css class 'active' for toggle views

* add strict

* time out for editor

* wip

* improvments use newActive and switch

* correction

* width of switcher-spans

* correct align

* do not use global

* wip

* removed timeouts

* JTEXT to TEXT

* forgotton last line

* deleted duplicated comments

* css fix align

* use unnamed functions in es6

* Sql files for fix database (#50)

* sql files for database fix

* delete space

* Suggestion for displaying Dates in view updates files (#52)

Correct Dates and do not use date of file any more

* Store Date as UTC and show it in server time zone (#57)

* modified and created date are created and stored in UTC

* convert dates for displaying in model

* spar a loop

* normalize timezone in view

* use language constants for dateformat

* JToolbarHelper to ToolbarHelper

* CS

* namespace

* plural

* name

* clean

* text

* fx

* sin

* files

* s

* Suggestion for language strings (#60)

* language strings

* correct typo

* delete media folder plg_quickicon

* add folder plg_quickicon to build/media_src

* delete files in media folder

* Move media folder - System (#66)

* multi

* cs

* delete files in media folder for joomla toolbar (#67)

* Fix button switchers style. (#70)

* button

* CS

* changed uitab.addTab for updated files

* Bring back core.js changes. (#69)

* core.js

* const

* fix

* form

* core

* hound

* CS

* scopr

* grid

* alpha

* cs

* lang

* only override file

* lang

* override lang installer

* Cs

* sub

* Update list of core extensions (#71)

* Language changes (#76)

* update

* Update en-GB.com_templates.ini

* override JLIB_HTML_PUBLISH_ITEM

this is the hover text on the publish icon in the list of files

* Change icon (#74)

change the icon to use an outline for more consistency

* lang

* not core (#75)

* not core

* Update en-GB.plg_installer_override.ini

* namespace

* cs

* Updated files (#82)

* Update default_updated_files.php

* Update en-GB.com_templates.ini

* Update en-GB.com_templates.ini (#81)

* Update en-GB.plg_quickicon_overridecheck.ini (#80)

* Update en-GB.plg_quickicon_overridecheck.ini (#79)

* remove space (#78)

* Update en-GB.plg_quickicon_overridecheck.ini

* Update en-GB.plg_quickicon_overridecheck.sys.ini

* remove hardcoded id

* null get function

* state

* clean

* More changes "core" to "original" (#85)

* cs

* update

* plural
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement Small enhancemenet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants