Welcome, Guest
Username: Password: Remember me

TOPIC: [FIXED] GMT used when saving record to table

GMT used when saving record to table 03 Mar 2016 11:56 #13842

  • Romkabouter
  • Romkabouter's Avatar
  • Offline
  • Elite Member
  • Posts: 310
  • Thank you received: 131
  • Karma: 48
Hi,

When using the creation_date and modification_date wizards I came across the fact that saving an item uses the GMT date as creation date.
This is a problem when using DST time.

When my local time is 12.38, saving an item results in a creation date 11.38, because of GMT.
This is caused by two code "problems":

- In the generated prepareTable function from the model file it uses:
$date = JFactory::getDate();

I believe the config offset should be taken into account (see also docs.joomla.org/JFactory/getDate):
$date = JFactory::getDate('',JFactory::getConfig()->get('offset'));

Next stop is the generated toSql function in the HelperDates class.
In the prepareTable it is called ilke this:
if (empty($table->id))
{
    //Creation date
    if (empty($table->creation_date))
        $table->creation_date = <CompName>HelperDates::toSql($date);
}

The function in the HelperDates class:
	public static function toSql($date)
	{
		$version = new JVersion();
		if ($version->isCompatible('3.0'))
			return $date->toSql();	
		else
			return $date->toMySQL();
	}

For the toSQL I believe it should use the local time, that can be implemented using true as parameter
See api.joomla.org/cms-3/classes/JDate.html#method_toSql
and
api.joomla.org/cms-2.5/classes/JDate.html#method_toMySQL

Complete code:
	public static function toSql($date)
	{
		$version = new JVersion();
		if ($version->isCompatible('3.0'))
			return $date->toSql(true);	
		else
			return $date->toMySQL(true);
	}

I am now override the prepareTable like this:
	protected function prepareTable($table)
	{
		$date = JFactory::getDate('',JFactory::getConfig()->get('offset'));
 		if (empty($table->id))
		{
			//Creation date
			if (empty($table->creation_date))
				$table->creation_date = $date->toSql(true);
		}
		else
		{
			//Modification date
			$table->modification_date = $date->toSql(true);
		}
	}

This suits my needs, but I would also be glad to hear your opinions.
The administrator has disabled public write access.
The following user(s) said Thank You: admin

GMT used when saving record to table 04 Mar 2016 13:39 #13843

  • admin
  • admin's Avatar
  • Online
  • Administrator
  • Chef
  • Posts: 3711
  • Thank you received: 985
  • Karma: 140
My opnion is this is a great code !
I will implement it soon.

K++
Coding is now a piece of cake
The administrator has disabled public write access.

GMT used when saving record to table 07 Mar 2016 11:26 #13844

  • admin
  • admin's Avatar
  • Online
  • Administrator
  • Chef
  • Posts: 3711
  • Thank you received: 985
  • Karma: 140
Wait a minute.
I think, the server should store this information in UTC/GMT. Then the user effectively see the time converted to its local area.

Because if a user change its account parameter, it can affect the security. Never store local time if it is not mandatory. (Or store the offset in a separate field)
Well, we can debate around this. I prefer UTC because most of the time, those dates are important for security reasons, and not only for the user. If you want to have a consistant database, use UTC. You will avoid many problems.

(Note : We will use the term UTC, wich fits better for us (as developers). GMT = UTC, but it is just a naming convention)

So the code is correct but not at the good place. It should be in the date field rendering (both for FORM or FLY, GRID)
At the moment this is located in JDom.

Hold on, I am making a fix.
Coding is now a piece of cake
The administrator has disabled public write access.

GMT used when saving record to table 07 Mar 2016 11:59 #13845

  • admin
  • admin's Avatar
  • Online
  • Administrator
  • Chef
  • Posts: 3711
  • Thank you received: 985
  • Karma: 140
Ok...

Everything is (almost) working correctly.
What is missing is a parameter to JDom (Now I remember after reading the code... lol)

It is now FIXED in the builder, with a new version of JDom:
github.com/Cook-Self-Service/JDom/commit...25eadcf3a176c20fb929

In case, if you do not want to upgrade JDom, you can pass a parameter to it :
JDom::_('...',  array(
...
'timezone' => 'USER_UTC',
...
);

Add this parameter everywhere that the user is reading the time.

The other possible value is : "SERVER_UTC", which will convert the same UTC datetime to the server offset. It can be usefull for administrators.

Hope it has helped you.
I will create a documentation for that. (Took note)
Coding is now a piece of cake
The administrator has disabled public write access.

GMT used when saving record to table 07 Mar 2016 12:05 #13846

  • admin
  • admin's Avatar
  • Online
  • Administrator
  • Chef
  • Posts: 3711
  • Thank you received: 985
  • Karma: 140
I can add a small hit...

Actually, you can eventually store in database with the SERVER_UTC offset, but if an admin change it, we will face the same security problem, or instability and inconsistencies. So I do not recommend it, but if in a particular case you need a static offset in the DB, use the SERVER_UTC, and use your code (in the model), but not with USER_UTC.
Coding is now a piece of cake
The administrator has disabled public write access.

GMT used when saving record to table 07 Mar 2016 12:11 #13847

  • Romkabouter
  • Romkabouter's Avatar
  • Offline
  • Elite Member
  • Posts: 310
  • Thank you received: 131
  • Karma: 48
Hmm, yes that is true.

Great work!
The administrator has disabled public write access.
The following user(s) said Thank You: admin
Time to create page: 0.127 seconds

Get Started