PDA

View Full Version : Timezone offset



pdoria
11th July 2009, 16:39
Hi,

I need some help computing the timezone offset regarding DST.
Although I searched through the docs and Googled quite a bit I couldn't find an easy way/function to get this... :o

Specifically I need to insert timestamps with time zone into a postgresql server in this format:
yyyy-mm-dd+offset - e.g. 2009-07-11+01

How do I get the '+01' part?

Already thankful for any advice,
Best regards,
Pedro Doria Meunier

pdoria
11th July 2009, 20:00
Replying to myself :rolleyes:

Here's what I've come with:



/************************************************** *************************
* Copyright (C) 2009 by Pedro Doria Meunier *
* pdoria@netmadeira.com *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
************************************************** *************************/


#include <QCoreApplication>
#include <QDateTime>
#include <stdlib.h>
#include <iostream>

// helper functions
QString getTimezoneDiff( QDateTime dt );

int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);

QDateTime localDate = QDateTime::fromString("2009-07-11 16:53:00","yyyy-MM-dd hh:mm:ss");

QString diffStr;
diffStr = getTimezoneDiff( localDate );



printf ("Local Date: %s\r\n", localDate.toString().toLatin1().data() );
//printf ("UTC Date: %s\r\n", utcTime.toLatin1().data() );
printf ("Offset : %s\r\n", diffStr.toLatin1().data() );



return 0;
}


/**
* QString getTimezoneDiff( QDateTime dt )
* @param dt well formated date time (YYYY-MM-DD hh:mm:ss)
* @return tzDiff (<+->hhmm)
*
* Purpose: return a timezone offset string to feed to PostgreSQL
*/
QString getTimezoneDiff( QDateTime dt ) {

QString localdt = dt.toString ( "hhmm" ); // get the local time part from the @param
QString utcDT = dt.toUTC().toString ( "hhmm" ); // get the UTC time part from the @param
QString tzDiff; // this will hold the returned timezone diff.
qint8 hhmmdt = localdt.toInt();
qint8 hhmmUTC= utcDT.toInt();
qint8 offset = hhmmdt - hhmmUTC;

// is it a positive offset?
if (offset>=0) {
tzDiff = "+" + tzDiff.setNum(offset);;
}
else
tzDiff.setNum(offset);
return tzDiff;
}


If there's (I'm sure there is! ;) ) a more elegant solution I'd love to see it!

BR,
Pedro Doria Meunier

mattc
13th July 2009, 12:59
I had the same problem a while ago, this is what I came up with (Qt 4.5.2):


QString getTimeZoneOffset()
{
QDateTime dt1 = QDateTime::currentDateTime();
QDateTime dt2 = dt1.toUTC();
dt1.setTimeSpec(Qt::UTC);

int offset = dt2.secsTo(dt1) / 3600;

if (offset > 0)
return QString("+%1").arg(offset);

return QString("%1").arg(offset);
}

Roman
25th June 2010, 18:01
Well guess in Qt 4.6 both solutions will fail during the sommertime due to Daylight saving time!
Does anyone have a solution to this problem?

thanks
Roman

QtCutie
18th September 2012, 23:08
This function returns the UTC offset for any timezone. For negative UTC offsets you must override this function and check the boolean "isNegative". I use this for sending requests to a server, if I want to check that its not the day the clock moves forwards or backwards I just call the function twice, once with today's date and then with tomorrow's date. If they both return the same then we know that the clock isn't switching in the next 24 hrs for Daylight Savings time.


QTime Calendar::getTimeZoneDiff(QDateTime midnightDateTime, bool &isNegative){
midnightDateTime.setTime(QTime(0,0));
QDateTime utc = midnightDateTime.toUTC();
QDateTime local = midnightDateTime;
local.setTimeSpec(Qt::LocalTime);
QDateTime offset = local.toUTC();
QTime properTimeOffset = QTime(offset.time().hour(), offset.time().minute());
offset.setTimeSpec(Qt::LocalTime);
utc.setTimeSpec(Qt::UTC);

if(offset.secsTo(utc) < 0){
isNegative = true;
}else{
isNegative = false;
properTimeOffset.setHMS(24 - properTimeOffset.hour() - (properTimeOffset.minute()/60.0) - (properTimeOffset.second()/3600.0), properTimeOffset.minute() - (properTimeOffset.second()/60.0), properTimeOffset.second());
if(!properTimeOffset.isValid()){ //Midnight case
properTimeOffset.setHMS(0,0,0);
}
}
return properTimeOffset;
}

jamadagni
7th February 2014, 06:57
QDateTime dt2 = dt1.toUTC();
The toUTC() is not needed in the above line. All that we need is a copy-constructed QDateTime.


Well guess in Qt 4.6 both solutions will fail during the sommertime due to Daylight saving time!
Can you please clarify why it would fail? I am using Qt 4.8.4 and mattc's solution works for me. I did not try the OP's self-reply since mattc's solution seems more elegant and reliable.