JDate offset issue

This forum is for reporting bugs in Joomla!. Please don't report problems with extensions in here.
Forum rules
Forum Rules
Forum Post Assistant - If you are serious about wanting help, you will use this tool to help you post.
Locked
User avatar
pollen8
Joomla! Explorer
Joomla! Explorer
Posts: 350
Joined: Wed Aug 17, 2005 10:32 pm
Location: la Rochelle - France
Contact:

JDate offset issue

Post by pollen8 » Wed Aug 20, 2008 3:16 pm

Description:
When constructing JDate for the current time, the offset parameter is not taken into effect

Reported on:
Joomla 1.5.6

Classification:
Low



Steps to replicate:

Code: Select all

//get the global server offset time

$config =& JFactory::getConfig();
$tzoffset = $config->getValue('config.offset');		
echo "offset = " .$tzoffset."<br>";

//the offset is applied when doing this:
$ok_offsetdate = JFactory::getDate();
$ok_offsetdate->setOffset($tzoffset);
echo $ok_offsetdate->toMySQL(true) . "  ";

//but not here
$not_offsetdate =& JFactory::getDate('now', $tzoffset);
echo "<br>".$not_offsetdate->toMySQL(true);
Analysis:


Proposed fix(es):
I'm not sure if this is deliberate, but the fix it would seem to me would be in
libraries/joomla/utilities/date.php:
replace

Code: Select all

$this->_date = strtotime(gmdate("M d Y H:i:s", time()));
with

Code: Select all

$this->_date = strtotime(gmdate("M d Y H:i:s", time())) + ($tzOffset * 3600);
Topic / Artifact ID:
[USER] Enter the cross reference for topic or artifact id/url when submitted

System info:
Apache2 php5.2.5 Joomla1.5 mysql5.something
aka

mic
Joomla! Guru
Joomla! Guru
Posts: 692
Joined: Thu Aug 18, 2005 10:51 pm
Location: Austria
Contact:

Re: JDate offset issue

Post by mic » Mon Oct 20, 2008 8:09 pm

Any response from somebody of the Q&T team?
I am using a fresh install of 1.5.7 AND this issue does still exist!

Could also not found any message at the bugtracker.

So, i would like to know if

1. this is already fixed
2. not fixed but will be with 1.5.8
3. should an entry at the JC-bugtracker be created

Without an fully working date/time function in Joomla, many things are not possible.

thx.
http://www.joomx.com - custom extensions and development
http://www.joomlasupportdesk.com - support, migration, training and consulting
Member of the German Joomla Translation Team

pasamio
Joomla! Ace
Joomla! Ace
Posts: 1318
Joined: Thu Aug 18, 2005 9:27 am
Location: San Jose, CA, USA
Contact:

Re: JDate offset issue

Post by pasamio » Tue Oct 21, 2008 11:05 am

JDate internally uses GMT to store its information and tries its best to do this.

Code: Select all

	 * @param mixed $date optional the date this JDate will represent.
	 * @param int $tzOffset optional the timezone $date is from
JFactory::getDate appears to be a bit vague comment wise, that is taken from the JDate constructor. As you can see the timezone specified is for the date you provide and since you ask for 'now' it is provided in GMT and the timezone offset is ignored. You can manually set the offset if you wish but by default we use GMT so that we can then convert to the appropriate offset. Keeping in mind that a user may have a different time zone again to the system configuration and would probably want to see the date in their timezone. The offset you're providing is designed to try and help JDate work out what the GMT date should be from it. Manually setting the offset like you have done is the correct way to get JDate to return a date in the configured local time.

If you are having issues with dates and times make sure that you're using the latest version of PHP. Some versions of PHP have known issues getting the date and time right, especially PHP4 versions and some early PHP5 versions. We've found this throughout the testing we did when initially building JDate for Joomla! 1.5 and trying to get it to work in the majority of cases. If you're experiencing an issue on PHP 5.2.x, there are unit tests available so that we can get an idea of what is broken to try and fix it.

With Joomla! 1.6 shifting to PHP5.x+ we're hoping we'll be able to avoid most of the date issues that we have now and help us to do a whole heap of cool new stuff. Part of 1.6 will probably involve a rethink of how we're implemented JDate, however it will always use GMT by default.
Sam Moffatt
Updater, Installer and Authentication Systems
JoomlaCode Backend Systems
Pie.

mic
Joomla! Guru
Joomla! Guru
Posts: 692
Joined: Thu Aug 18, 2005 10:51 pm
Location: Austria
Contact:

Re: JDate offset issue

Post by mic » Tue Oct 21, 2008 7:51 pm

Sorry Sam,

forgot to post my configuration:

* J.1.5.7
* php 5.2.6
* Apache 2.2.9
* mySQL 5.0.67
* FF 3.0.3

Another thing i found while the research for this issue:

Thai language use an own date.php.
In the JFactory class there is a reference to this file:

Code: Select all

$localePath = JPATH_ROOT . DS . 'language' . DS . $mainLocale . DS . $mainLocale . '.date.php';
My questions:

1. what for exactly should this file for (or what can we do with it)?
2. why is it not included in the latest (1.5.7.) package?

thx in advance.
http://www.joomx.com - custom extensions and development
http://www.joomlasupportdesk.com - support, migration, training and consulting
Member of the German Joomla Translation Team

pasamio
Joomla! Ace
Joomla! Ace
Posts: 1318
Joined: Thu Aug 18, 2005 9:27 am
Location: San Jose, CA, USA
Contact:

Re: JDate offset issue

Post by pasamio » Wed Oct 22, 2008 12:56 am

Very very late in the process we got a request to display dates in local format, in your case the Thai Bhuddist calendar. So JDate has the ability to try and use the localised version of JDate. If you are expereincing date issues it may also be an issue with a local date override not doing its job properly. If you're happy to go without Bhuddist dates then you could rename the file in the language to revert to the normal JDate format.

One of the things we need to do is rethink how these things work and fix the many issues that arise when using a custom date in places. This is worse when using a custom date system (e.g. Thai), but to really fix the problem we need to sit back and look at how we do things and rebuild the system in a way that works and behaves better across different situations.

So,
1) That file is the localised version of JDate to support a local calendar (e.g. Thai Bhuddist)
2) Its not shipped with J! because we don't know all of the calenders and this is typically very closely tied to the language that is installed. We can't possiibly build it all into one JDate so this task is left to the localisation team who is best able to handle date related issues
Sam Moffatt
Updater, Installer and Authentication Systems
JoomlaCode Backend Systems
Pie.

User avatar
mblodau
Joomla! Apprentice
Joomla! Apprentice
Posts: 13
Joined: Wed Aug 13, 2008 8:15 am
Location: Stockholm (Sweden)
Contact:

Re: JDate offset issue

Post by mblodau » Wed Dec 30, 2009 10:15 am

1,5 years have passed since this report and the bug is still there.

Preparatory code:

Code: Select all

$app =& JFactory::getApplication();
$date = JFactory::getDate();
$date->setOffset($app->getCfg('offset'));
The following code still ignores the time offset:

Code: Select all

echo ($date->toMySQL());
Whereas this code outputs the time with the correct offset:

Code: Select all

echo ($date->toFormat());
One would think this wouldn't be too hard to fix in the core?

Sysinfo for reference.

PHP byggd på: Linux olivia 2.6.27-gentoo-r10 #1 SMP Wed Apr 15 13:47:44 CEST 2009 i686
Databasversion: 5.0.70-log
Sorteringsordning: utf8_general_ci
PHP-version: 5.2.11-pl0-gentoo
Webbserver: Apache
Webbserver till PHP-interface: cgi-fcgi
Joomla! version: Joomla! 1.5.15 Stable [ Wojmamni Ama Mamni ] 05-November-2009 04:00 GMT
Webbklient: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6
Martin Blodau
Joomla! Web Developer @ Pixpro

pasamio
Joomla! Ace
Joomla! Ace
Posts: 1318
Joined: Thu Aug 18, 2005 9:27 am
Location: San Jose, CA, USA
Contact:

Re: JDate offset issue

Post by pasamio » Wed Dec 30, 2009 12:55 pm

toMySQL should always return the GMT version of the date for being stored in the database, are you saying its not returning the GMT version of the date?

The reason for this is to use a fixed time zone in the database and then when it is needed to convert to the relevant time zone. This way if you shift your server from one location to another or a user has a different time zone you're not trying to guess offsets to display it appropriately.
Sam Moffatt
Updater, Installer and Authentication Systems
JoomlaCode Backend Systems
Pie.

User avatar
micah123
Joomla! Apprentice
Joomla! Apprentice
Posts: 25
Joined: Wed Jan 21, 2009 1:13 am
Location: Canada
Contact:

Re: JDate offset issue

Post by micah123 » Sun Feb 27, 2011 10:34 pm

I ran into a similar problem with JDate on 1.5.22, where offset does not seem to have proper effect.

The problem seems to be in JDate constructor where code is:

if (is_numeric($date))
{
$this->_date = $date - $tzOffset;

return;
}

To apply the $tzOffset the constructor must be passed a numeric date/time value that can be created with a call such as:

$offset = $mainframe->getCfg('offset');

$server_time = gmmktime();

$my_today = new JDate($server_time, $offset);


Note: using $my_today = new JDate('now', $offset) does not work because the is_numeric($date) will bypass the offset adjustment. Using a call with a timestamp will apply the offset, but does not take into consideration negative offset. So the offset calculation should be changed as follows;

$tzOffset *= 3600;
if (is_numeric($date))
{

if($tzOffset > 0){
$this->_date = $date - $tzOffset;
}else{
$this->_date = $date + $tzOffset;
}

return;
}

Hope this helps.

Micah

tadosborn
Joomla! Apprentice
Joomla! Apprentice
Posts: 12
Joined: Tue Dec 11, 2007 6:01 pm

Re: JDate offset issue

Post by tadosborn » Wed Nov 23, 2011 3:16 am

I'm running into the same issue. No offset!

donluismx
Joomla! Fledgling
Joomla! Fledgling
Posts: 1
Joined: Thu Aug 23, 2012 6:28 pm

Re: JDate offset issue

Post by donluismx » Thu Aug 23, 2012 7:11 pm

As taken from here http://www.webamoeba.co.uk/site/index.p ... -date-time
BTW mine is Joomla 2.5.6

In addition to the date and time that a JDate object represents, a JDate object can also record a time zone in which we want to output formatted dates. We can set and get this value using the JDate::setOffset() and JDate::getOffset() methods. When we set an offset we are setting the number of hours offset from UTC in which we want to display the date and time. The example below sets the time zone to UTC+1.

$myDate->setOffset(1);

To retrieve this value we use the getter method.

$offset = $myDate->getOffset();
Once we have set the offset we can use any of the five methods described in the previous section to get a string representation of the date and time. There are however some important differences between JDate::toFormat() and the other four methods. The JDate::toFormat() method always applies the offset.

// custom format with offset applied
$string = $myDate->toFormat('%Y-%m-%d %H:%M:%S');

The remaining four methods do not apply the offset unless explicitly requested to do so. We can tell these methods to apply the offset using the one and only parameter that all of these methods accept, $local which is by default false. The JDate::toUnix() and JDate::toMySQL() methods do not define the time zone offset in the returned values.
// D, d M Y H:i:s
// Tuesday, 06 October 2009 12:54:37+0000
$rfc2822 = $myDate->toRFC822();
// Tuesday, 06 October 2009 13:54:37 +0100
$rfc2822 = $myDate->toRFC822(true);

// Y-m-dTH:i:s
// 2009-10-06T12:54:37Z
$iso8601 = $myDate->toISO8601();
// 2009-10-06T13:54:37+01:00
$iso8601 = $myDate->toISO8601(true);

// Unix timestamp
// 1254833677
$unix = $myDate->toUnix();
// 1254837277
$unix = $myDate->toUnix(true);

// Y-m-d H:i:s
// 2009-10-06 12:54:37
$mysql = $myDate->toMySQL();
// 2009-10-06 13:54:37
$mysql = $myDate->toMySQL(true);


After reading that, I thought I had it all set, but I discovered that working with this offset setup has a nasty issue. Sometimes it kinda goes on the next lower pair level, for example, if you set the offset to 1, it remains 0, but if you set it to 2, it goes up to the correct value of 7200 (secs). My problem was that I wanted to use -5 (-18000) but JDate applied -4 (-14400) which is as useless as not applying any offset at all. I guess I'll go with JFactory::getDate() and work it out with what it has to offer.

Hope it helps.

User avatar
coalaweb
Joomla! Apprentice
Joomla! Apprentice
Posts: 41
Joined: Wed Jun 30, 2010 6:42 pm
Location: New York
Contact:

Re: JDate offset issue

Post by coalaweb » Sat Nov 10, 2012 11:56 am

I have been working with this on and off for months with different version of Jooma but to reduce confusion the examples below have been tested on the system outlined at the bottom.

If you want the offset added to the datetime (MySQL formatted) you will have to include true at the end of the ->toMySQL statement.

Code: Select all

      
$config   =& JFactory::getConfig();
$siteOffset = $config->getValue('config.offset');
$dtnow =& JFactory::getDate('now', $siteOffset);
$now = $dtnow->toMySQL(true);
echo $now;
True:This will output the date/time with the current server offset where in the example below is +1.
2012-11-10 12:51:36
Now if I change it to false as seen below I get the date time with out the offset.

Code: Select all

$now = $dtnow->toMySQL(false);
False:
2012-11-10 11:51:36
Hope that helps


System:

PHP Built On: Linux
Database Version: 5.5.23-55
Database Collation: utf8_general_ci
PHP Version: 5.3.15
Web Server: Apache
Joomla! Version: Joomla! 2.5.8 Stable [ Ember ] 8-November-2012 14:00 GMT
Joomla! Platform: Version Joomla Platform 11.4.0 Stable [ Brian Kernighan ] 03-Jan-2012 00:00 GMT
User Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:16.0) Gecko/20100101 Firefox/16.0


Locked

Return to “Joomla! 1.5 Bug Reporting”