I'm using QwtDateScaleDraw in Qt to plot a QDateTime at the X-axis of a graph.
I use this command to convert the QDateTime to a QwtDate::Double:
tempData->append( QPointF( QwtDate::toDouble( date.at(var) ), data.at(var) ) );
The first date/time that I have is
01/08/2014 00:00:52
but when I place the data in the graph, the first date is
31/07/2014 21:04:52.
I don't know why this is happening.
I managed to solve this using:
scaleDraw->setTimeSpec(Qt::UTC);
Related
I noticed the answer in Custom DateTime in X axis in Lightningchart JS, but it report error "AxisTickStrategies.DateTime is not a function" in lightningchartV2.2.1.
also, since AxisTickStrategies.DateTime is not a function, how can i show a full date axisx label for a series using a DateTime axis ?
Please have a look at official LCJS Interactive Examples, and search for "datetime"
For example,
https://www.arction.com/lightningchart-js-interactive-examples/examples/lcjs-example-0020-dateTimeAxis.html
This is the bare minimum required code - please refer to the examples for extended modification.
chart.getDefaultAxisX()
.setTickStrategy(
// Use DateTime TickStrategy for this Axis
AxisTickStrategies.DateTime,
)
I am attempting to do something seemingly trivial, but running into all sorts of problems.
I would like to plot certain basic information on a label every time there is an update to the current price--regardless of the timeframe of my chart.
I am able to accurately display volume and price information, however displaying the time has been a challenge.
My first attempt was to use the following code:
if (barstate.islast)
label.set_text(
id=myLabel,
text="\nTime: " + tostring(hour) + ":" + tostring(minute) + ":" + tostring(minute)
)
I quickly learned that, even though my chart is set to the timezone for New York (i.e., UTC-4), calling tostring(hour) displays the hour of UTC.
Figuring out how to specify that I want it displayed time to correspond to my chart's timezone has been the first major hurdle, and I have tangled endlessly with timestamp() and syminfo.timezone to no avail.
My second major problem is that tostring(second) does not properly display the seconds, even for UTC time.
While working on a 1m chart, I thought I managed to solve this by implementing
tostring((timenow-time)/1000)
However, the seconds do not display properly on different time frames.
This is all in addition to the fact that charts from different exchanges in different time zones will all display time "incorrectly" with respect to UTC time.
It must to be the case that I am missing something fairly basic, since time is such crucial data, but I just can't determine the proper syntax.
Thanks in advance for any assistance.
A few different issues are at play here:
Pine scripts have no visibility on the chart's timezone you may have selected manually. That only affects the display of the chart.
The minute variable returns the minute at the beginning of the bar, so will not change on script iterations in the realtime bar, until a new bar begins. To get the current minute you will need to use an overloaded version of minute where you can specify a timestamp in ms. The timenow built-in variable returns the timestamp for the time of a particular script iteration (that is true in the realtime bar; when the script is running on historical bars, timenow is only updated every second during the script's execution). So you need to use minute(timenow).
If you want minute() to return a time in another timezone than the exchange's, you can use a second parameter to specify a timezone, which is what we do in the second example here. In our example you can change the timezone through the script's "Settings/Inputs". Used with the timezone, minute() will look something like:
minute(timenow, "GMT-4").
//#version=4
study("", "Time", true)
i_timeZone = input("GMT-4")
f_print(_txt) => var _lbl = label.new(bar_index, highest(10)[1], _txt, xloc.bar_index, yloc.price, #00000000, label.style_none, color.gray, size.large, text.align_left), label.set_xy(_lbl, bar_index, highest(10)[1]), label.set_text(_lbl, _txt)
f_print(tostring(hour(timenow), "00:") + tostring(minute(timenow), "00:") + tostring(second(timenow), "00") + " (Exchange)\n")
f_print(tostring(hour(timenow, i_timeZone), "00:") + tostring(minute(timenow, i_timeZone), "00:") + tostring(second(timenow, i_timeZone), "00") + " (Input: " + i_timeZone + ")")
I am stuck with this problem.
I already got the currentUTCtime in seconds from the QDateTime.
Problem is, I can't find a possible way to convert this into the local time in seconds. There are some QDate functions like toLocalTime() which just don't seem to work. I hope somebody here can help me.
QDateTime::currentMSecsSinceEpoch();
QDateTime currentateTime = QDateTime::currentDateTime();
QDateTime UTC(QDateTime::currentDateTimeUtc());
currentDateTime.toString().toStdString();
TimeNow = currentDateTime.toMSecsSinceEpoch()/1000;
Above is my code for the currentUTC Time in seconds.
If you just need the time in seconds since the epoch you can use QDateTime::toTime_t(); this method exists in Qt 4.7 and seems to be a part of Qt 5 from the start, too.
QDateTime::currentDateTime().toTime_t()
for local time, or for UTC
QDateTime::currentDateTimeUtc().toTime_t()
Use QDateTime::fromTime_t, to which the documentation states:
Returns a datetime whose date and time are the number of seconds that have passed since 1970-01-01T00:00:00, Coordinated Universal Time (Qt::UTC) and converted to the given spec.
qint64 utcTime = QDateTime::currentMSecsSinceEpoch();
QDateTime localTime = QDateTime::fromTime_t(utcTime, Qt::LocalTime);
I am using Qt 5.4.1 and getting issue during calling QTimeZone::QTimeZone(int offsetSeconds) method. My code is :
QTimeZone zone = QTimeZone(+19800); // +19800 is offsetFromUtc in seconds for India country
qDebug()<<QLocale::countryToString(zone.country());
The above qDebug prints "Default" each time but when I am using QTimeZone(const QByteArray & ianaId) method for example :
QTimeZone zone = QTimeZone("Asia/Calcutta");
qDebug()<<QLocale::countryToString(zone.country());
The above qDebug prints "India" which is correct. But at the same time QTimeZone(int offsetSeconds) method is not working properly.....could anybody help me what I am doing wrong ?
There is no way to convert a timezone offset into a country. The relationship is 1:n from locality to offset: if you know the region/country, and current UTC, you can know the timezone offset. But there is a lot of different locales that have the same offset. And the offset it not necessarily constant over time for any given location.
I'm trying to get the current time as TimeStamp without success.
I have this code:
QDateTime setTime = QDateTime::fromString (QString("1970-07-18T14:15:09"), Qt::ISODate);
QDateTime current = QDateTime::currentDateTime();
uint msecs = setTime.time().msecsTo(current.time());
return QString::number(msecs);
The output is
Sunday, January 25th 1970, 03:17:35 (GMT)
In Qt 4.7, there is the QDateTime::currentMSecsSinceEpoch() static function, which does exactly what you need, without any intermediary steps. Hence I'd recommend that for projects using Qt 4.7 or newer.
I think you are looking for this function:
http://doc.qt.io/qt-5/qdatetime.html#toTime_t
uint QDateTime::toTime_t () const
Returns the datetime as the number of seconds that have passed since 1970-01-01T00:00:00, > Coordinated Universal Time (Qt::UTC).
On systems that do not support time zones, this function will behave as if local time were Qt::UTC.
See also setTime_t().
Since Qt 5.8, we now have QDateTime::currentSecsSinceEpoch() to deliver the seconds directly, a.k.a. as real Unix timestamp. So, no need to divide the result by 1000 to get seconds anymore.
Credits: also posted as comment to this answer. However, I think it is easier to find if it is a separate answer.