|
chartTime vs chartTime2 |
Posted by Marc on Jul-25-2012 04:58 |
|
What is the difference between these?
Also I see in finance chart:
// Convert from chartTime to QDateTime
static QDateTime ChartTimeToQDateTime(double t)
{
double ymdhms = floor(t);
int ms = (int)(floor((t - ymdhms) * 1000));
int ymd = Chart::getChartYMD(ymdhms);
int hms = (int)fmod(ymdhms, 86400);
return QDateTime(QDate(ymd / 10000, (ymd % 10000) / 100, ymd % 100),
QTime(hms / 3600, (hms % 3600) / 60, hms % 60, ms));
}
Isn't this much slower than:
static double epoch = Chart::chartTime(1970, 1, 1);
QDateTime dt;
double ms = (qint64)((t-epoch)*1000+0.5);
dt.setMSecsSinceEpoch(ms); |
Re: chartTime vs chartTime2 |
Posted by Peter Kwan on Jul-25-2012 22:28 |
|
Hi Marc,
In terms of the CPU time used, I believe the two methods are both negligible when compared to plotting the chart.
The UNIX timestamp method is not used because of the way the "number of seconds elapsed" is defined in ChartDirector. In ChartDirector. it computes the "number of seconds" assuming 1 day has 24 hours and 1 hour has 60 minutes. Basically, it is an encoding of the human readable time yyyy-mm-dd hh:nn:ss to a number.
In contrast, the UNIX timestamp refers to "physical elapsed seconds", in which 1 day can have 23 to 25 hours due to daylight savings time (DST). In some countries, 1 hour can have 30 minutes (again due to DST - as the DST rule in some countries can adjust the clock by 30 minutes).
To convert UNIX timestamps to ChartDirector chartTime (which is based on the human readable date/time), the DST rule must be known. However, as the DST rule often changes, it is difficult to determine the DST rule for historical time and certainly not possible to predict it for future times. (In some countries, the DST rule changes every year.) That's why in ChartTimeToQDateTime, the UNIX timestamp method is not used.
Regards
Peter Kwan |
Re: chartTime vs chartTime2 |
Posted by Marc on Jul-25-2012 22:49 |
|
But what if the difference between the two methods chartTime and chartTime2? When
should we use one over the other? Is one better than the other?
Thank you for your answer about ChartTimeToQDateTime. |
Re: chartTime vs chartTime2 |
Posted by Marc on Jul-25-2012 22:49 |
|
But what is the difference between the two methods chartTime and chartTime2? When
should we use one over the other? Is one better than the other?
Thank you for your answer about ChartTimeToQDateTime. |
Re: chartTime vs chartTime2 |
Posted by Peter Kwan on Jul-26-2012 01:02 |
|
Hi Marc,
The chartTime is used if you know the components of the date/time (year, month, day, hour, minute, seconds). It converts the date/time into the ChartDirector format.
The chartTime2 converts the UNIX timestamp to a ChartDirector format.
Although UNIX timestamps cannot be reliably converted to a human readable format, if the conversion is applied twice, once in the forward direction and once in the reverse direction, then any error will be cancelled.
For example, suppose you have a MySQL database. Like ChartDirector, most database will not use UNIX timestamps. The MySQL database will use its own MySQL format. If the SQL query requires the MySQL database to return UNIX timestamps, it must convert from the MySQL format to UNIX timestamps using the operating system DST rule. Then ChartDirector can convert the UNIX timestamps to ChartDirector date/time format with chartTime2, again using the same operating system DST rule. So even the DST rule can be incorrect, the overall result is correct, because the DST rule is applied twice, once in the forward direction and once in the reverse direction.
I think in practice, the actual data source (no matter it is human input, from a database, computer clock, text file, etc) is probably not in UNIX timestamp format. If there is a UNIX timestamp, it is probably produced on the fly using the "current operating system DST rule". So chartTime2 will work, because even if the DST rule is incorrect, the errors cancel out.
Hope this can help.
Regards
Peter Kwan |
|