Results 1 to 6 of 6

Thread: Timezone offset

  1. #1
    Join Date
    Jan 2008
    Posts
    107
    Thanks
    36
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Lightbulb Timezone offset

    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...

    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
    Last edited by pdoria; 11th July 2009 at 20:49. Reason: SOLVED

  2. #2
    Join Date
    Jan 2008
    Posts
    107
    Thanks
    36
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Lightbulb Re: Timezone offset (SELF SOLVED)

    Replying to myself

    Here's what I've come with:

    Qt Code:
    1. /***************************************************************************
    2.  * Copyright (C) 2009 by Pedro Doria Meunier *
    3.  * pdoria@netmadeira.com *
    4.  * *
    5.  * This program is free software; you can redistribute it and/or modify *
    6.  * it under the terms of the GNU General Public License as published by *
    7.  * the Free Software Foundation; either version 2 of the License, or *
    8.  * (at your option) any later version. *
    9.  * *
    10.  * This program is distributed in the hope that it will be useful, *
    11.  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
    12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
    13.  * GNU General Public License for more details. *
    14. ***************************************************************************/
    15.  
    16.  
    17. #include <QCoreApplication>
    18. #include <QDateTime>
    19. #include <stdlib.h>
    20. #include <iostream>
    21.  
    22. // helper functions
    23. QString getTimezoneDiff( QDateTime dt );
    24.  
    25. int main(int argc, char *argv[])
    26. {
    27. QCoreApplication app(argc, argv);
    28.  
    29. QDateTime localDate = QDateTime::fromString("2009-07-11 16:53:00","yyyy-MM-dd hh:mm:ss");
    30.  
    31. QString diffStr;
    32. diffStr = getTimezoneDiff( localDate );
    33.  
    34.  
    35.  
    36. printf ("Local Date: %s\r\n", localDate.toString().toLatin1().data() );
    37. //printf ("UTC Date: %s\r\n", utcTime.toLatin1().data() );
    38. printf ("Offset : %s\r\n", diffStr.toLatin1().data() );
    39.  
    40.  
    41.  
    42. return 0;
    43. }
    44.  
    45.  
    46. /**
    47.  * QString getTimezoneDiff( QDateTime dt )
    48.  * @param dt well formated date time (YYYY-MM-DD hh:mm:ss)
    49.  * @return tzDiff (<+->hhmm)
    50.  *
    51.  * Purpose: return a timezone offset string to feed to PostgreSQL
    52.  */
    53. QString getTimezoneDiff( QDateTime dt ) {
    54.  
    55. QString localdt = dt.toString ( "hhmm" ); // get the local time part from the @param
    56. QString utcDT = dt.toUTC().toString ( "hhmm" ); // get the UTC time part from the @param
    57. QString tzDiff; // this will hold the returned timezone diff.
    58. qint8 hhmmdt = localdt.toInt();
    59. qint8 hhmmUTC= utcDT.toInt();
    60. qint8 offset = hhmmdt - hhmmUTC;
    61.  
    62. // is it a positive offset?
    63. if (offset>=0) {
    64. tzDiff = "+" + tzDiff.setNum(offset);;
    65. }
    66. else
    67. tzDiff.setNum(offset);
    68. return tzDiff;
    69. }
    To copy to clipboard, switch view to plain text mode 

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

    BR,
    Pedro Doria Meunier

  3. #3
    Join Date
    Apr 2009
    Location
    Italy
    Posts
    70
    Thanks
    23
    Thanked 15 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Timezone offset

    I had the same problem a while ago, this is what I came up with (Qt 4.5.2):

    Qt Code:
    1. QString getTimeZoneOffset()
    2. {
    3. QDateTime dt1 = QDateTime::currentDateTime();
    4. QDateTime dt2 = dt1.toUTC();
    5. dt1.setTimeSpec(Qt::UTC);
    6.  
    7. int offset = dt2.secsTo(dt1) / 3600;
    8.  
    9. if (offset > 0)
    10. return QString("+%1").arg(offset);
    11.  
    12. return QString("%1").arg(offset);
    13. }
    To copy to clipboard, switch view to plain text mode 

  4. The following user says thank you to mattc for this useful post:

    jamadagni (7th February 2014)

  5. #4
    Join Date
    Apr 2010
    Posts
    13
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Timezone offset

    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

  6. #5
    Join Date
    Sep 2012
    Posts
    2
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Timezone offset

    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.

    Qt Code:
    1. QTime Calendar::getTimeZoneDiff(QDateTime midnightDateTime, bool &isNegative){
    2. midnightDateTime.setTime(QTime(0,0));
    3. QDateTime utc = midnightDateTime.toUTC();
    4. QDateTime local = midnightDateTime;
    5. local.setTimeSpec(Qt::LocalTime);
    6. QDateTime offset = local.toUTC();
    7. QTime properTimeOffset = QTime(offset.time().hour(), offset.time().minute());
    8. offset.setTimeSpec(Qt::LocalTime);
    9. utc.setTimeSpec(Qt::UTC);
    10.  
    11. if(offset.secsTo(utc) < 0){
    12. isNegative = true;
    13. }else{
    14. isNegative = false;
    15. properTimeOffset.setHMS(24 - properTimeOffset.hour() - (properTimeOffset.minute()/60.0) - (properTimeOffset.second()/3600.0), properTimeOffset.minute() - (properTimeOffset.second()/60.0), properTimeOffset.second());
    16. if(!properTimeOffset.isValid()){ //Midnight case
    17. properTimeOffset.setHMS(0,0,0);
    18. }
    19. }
    20. return properTimeOffset;
    21. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by QtCutie; 18th September 2012 at 23:33.

  7. #6
    Join Date
    Jan 2006
    Location
    11 N 78 E
    Posts
    110
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Timezone offset

    Quote Originally Posted by mattc View Post
    Qt Code:
    1. QDateTime dt2 = dt1.toUTC();
    To copy to clipboard, switch view to plain text mode 
    The toUTC() is not needed in the above line. All that we need is a copy-constructed QDateTime.

    Quote Originally Posted by Roman View Post
    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.
    Penguin #395953 using Qt for open-source development on X11 using C++ and
    Python via PyQt

Similar Threads

  1. LIMIT and OFFSET for QSqlRelationalTableModel
    By mkarakaplan in forum Newbie
    Replies: 8
    Last Post: 6th November 2007, 00:06
  2. QTableWidget, row offset
    By georgie in forum Qt Programming
    Replies: 2
    Last Post: 11th May 2006, 11:23
  3. Replies: 6
    Last Post: 10th January 2006, 13:07

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.