Home > Software engineering >  QT creator get the time from another time zone (not local time zone)
QT creator get the time from another time zone (not local time zone)

Time:03-06

I was trying to find how to get a current time, however, I am looking to get a time from another time zone. I tried to play with it, even trying to simply add hours to the current time, to make up to what I am trying to get, but it did not work well

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    QTimer *timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()),this, SLOT(showTime()));
    timer->start();

    QDate date = QDate::currentDate();
    QString dateTimeText = date.toString();
    ui->date->setText(dateTimeText);


}

void MainWindow::showTime()
{
    QTime time = QTime::currentTime();
    QString time_text = time .toString("hh : mm : ss");
    if((time.second() % 2) == 0)
    {
        time_text[3] = ' ';
        time_text[8] = ' ';
    }
    ui->Digital_clock->setText(time_text);

}

CodePudding user response:

To get QTime for different time zone, you could just create new QTime object based on currentTime() with specific offset.

To do that you can just call addSecs(int) function like below:

QTime currentTimeZoneTime = QTime::currentTime(); // Your time zone current time
QTime differentTimeZoneTime = localTime.addSecs(3600); // Add  1 hour to your time zone current time
  • Related