ASE Home Page Products Download Purchase Support About ASE
ChartDirector Support
Forum HomeForum Home   SearchSearch

Message ListMessage List     Post MessagePost Message

  Daylight Savings Time with chartTime
Posted by Chris Tonks on Oct-22-2015 01:31
Hi,

I'm using ChartDirector PHP 5.1 to plot data along time on the x-axis, and I'm having
trouble with timezones. I know there are a lot of threads about timezones but I couldn't
find one that covers this particular problem.

I'm trying to plot against UTC/GMT. I retrieve the data from the database in that
timezone, and feed the timestamp into chartTime2 to get the value needed to print
dates on the x-axis. But if daylight savings time is in effect the result of the chartTime2
function is one hour ahead of UTC.

An example:

UNIX timestamp: 1445447971
Local time: 2015-10-21 19:19:31
UTC/GMT: 2015-10-21 17:19:31
CD timestamp: 63581051971
CD epoch (1/1/1970): 62135600400
CD seconds since 1/1/1970: 1445451571
Difference from UNIX timestamp: 3600

Here is if I'm using a timezone that does not have DST (or I've disabled DST by other
means):

UNIX timestamp: 1445448002
Local time: 2015-10-21 19:20:02
UTC/GMT: 2015-10-21 17:20:02
CD timestamp: 63581048402
CD epoch (1/1/1970): 62135600400
CD seconds since 1/1/1970: 1445448002
Difference from UNIX timestamp: 0

PHP's date() function works as expected, and will change based on the setting of the
date.timezone parameter (set either in php.ini or via the function ini_set). But
chartTime2 seems to ignore the timezone and apply DST if it is enabled on the machine
even if it's not enabled in PHP.

I note that the ChartDirector epoch (the result of chartTime2(0)) is the same in both
cases. Only the result of chartTime2([UNIX timestamp]) changes with DST.

I've tried this on PHP 5.5.15 on Windows and 5.3.3 on Linux.

I need chartTime2 to obey the PHP settings and not apply DST if I don't want it to!

Best regards,

- Chris Tonks

  Re: Daylight Savings Time with chartTime
Posted by Chris Tonks on Oct-22-2015 18:48
A new day, a new frame of mind!

I've got around the problem by offsetting my timestamps from the result of chartTime2(0).
So instead of this:

$plottableTimebase = array_map(chartTime2, $originalTimebase);

...I'm now doing this:

function offsetCdTime($original)
{
    return $original + chartTime2(0);
}
$plottableTimebase = array_map(offsetCdTime, $originalTimebase);

(Well, not literally that, because it's a little inefficient. But you get my point.)

I still think it's a bug though, so you may want to look into it if it's new to you.

Cheers,

- Chris

  Re: Daylight Savings Time with chartTime
Posted by Chris Tonks on Oct-22-2015 19:06
Oops, I take that back. My workaround didn't fix it.

chartTime2 rigidly follows not only the DST settings of the host machine but also the
timezone, irrespective of what PHP is set to use. So a machine with a +00:00 offset
returns a chartTime2(0) value of 62135596800, while a machine with a +01:00 offset
returns 62135600400.

My workaround gets rid of the DST offset, but does not get rid of the timezone offset. I
need to offset my x-axis range not by chartTime2(0), but by chartTime2(0)-[offset of the
host system]. I'm not sure how to get the offset of the host system. I can't rely on PHP's
date.timezone parameter because that could differ from what the machine (and therefore
ChartDirector) uses.

  Re: Daylight Savings Time with chartTime
Posted by Peter Kwan on Oct-23-2015 01:25
Hi Chris,

ChartDirector is written in C++, and ChartDirector for PHP is a wrapper for the C++ library.
For the chartTime2, ChartDirector will use the operating system at the C level to convert a
given UNIX timestamp into [yyyy, mm, dd, hh, nn, ss].

If a PHP function can convert the timestamps to the [yyyy, mm, dd, hh, nn, ss], please just
use that PHP function to convert the timestamps to [yyyy, mm, dd, hh, nn, ss], and then
pass the [yyyy, mm, dd, hh, nn, ss] to ChartDirector using chartTime. For example, suppose
the PHP function "localtime" can get the [yyyy, mm, dd, hh, nn, ss] you would like to use,
the code would something like:

for ($i = 0; $i < count($timeStamps); ++$i) {
     $ymdhns = localtime($timeStamps[$i]);
     $timeStamps[i] = chartTime($ymdhns["tm_year"] + 1900, $ymdhns["tm_mon"] + 1,
.......);
}

In brief, if there exists a method that can achieve what you need, please use that method,
and pass the result to ChartDirector using chartTime.

Hope this can help.

Regards
Peter Kwan