# 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;
}