timeconversion.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <qglobal.h>
00022 #include <qtopia/timeconversion.h>
00023 #include <qregexp.h>
00024 #include <stdlib.h>
00025
00026 QString TimeConversion::toString( const QDate &d )
00027 {
00028
00029
00030
00031
00032 QString r = QString::number( d.day() ) + "." +
00033 QString::number( d.month() ) + "." +
00034 QString::number( d.year() );
00035
00036
00037 return r;
00038 }
00039
00040 QDate TimeConversion::fromString( const QString &datestr )
00041 {
00042
00043
00044
00045
00046 int monthPos = datestr.find('.');
00047 int yearPos = datestr.find('.', monthPos+1 );
00048 if ( monthPos == -1 || yearPos == -1 ) {
00049 qDebug("fromString didn't find . in str = %s; mpos = %d ypos = %d", datestr.latin1(), monthPos, yearPos );
00050 return QDate();
00051 }
00052 int d = datestr.left( monthPos ).toInt();
00053 int m = datestr.mid( monthPos+1, yearPos - monthPos - 1 ).toInt();
00054 int y = datestr.mid( yearPos+1 ).toInt();
00055 QDate date ( y,m,d );
00056
00057 return date;
00058 }
00059
00060 time_t TimeConversion::toUTC( const QDateTime& dt )
00061 {
00062 time_t tmp;
00063 struct tm *lt;
00064
00065 #if defined(_OS_WIN32) || defined (Q_OS_WIN32) || defined (Q_OS_WIN64)
00066 _tzset();
00067 #else
00068 tzset();
00069 #endif
00070
00071
00072 tmp = time( 0 );
00073 lt = localtime( &tmp );
00074
00075 lt->tm_sec = dt.time().second();
00076 lt->tm_min = dt.time().minute();
00077 lt->tm_hour = dt.time().hour();
00078 lt->tm_mday = dt.date().day();
00079 lt->tm_mon = dt.date().month() - 1;
00080 lt->tm_year = dt.date().year() - 1900;
00081
00082
00083 lt->tm_wday = -1;
00084 lt->tm_yday = -1;
00085
00086 lt->tm_isdst = -1;
00087
00088 tmp = mktime( lt );
00089 return tmp;
00090 }
00091
00092 QDateTime TimeConversion::fromUTC( time_t time )
00093 {
00094 struct tm *lt;
00095
00096 #if defined(_OS_WIN32) || defined (Q_OS_WIN32) || defined (Q_OS_WIN64)
00097 _tzset();
00098 #else
00099 tzset();
00100 #endif
00101 lt = localtime( &time );
00102 QDateTime dt;
00103 dt.setDate( QDate( lt->tm_year + 1900, lt->tm_mon + 1, lt->tm_mday ) );
00104 dt.setTime( QTime( lt->tm_hour, lt->tm_min, lt->tm_sec ) );
00105 return dt;
00106 }
00107
00108
00109 int TimeConversion::secsTo( const QDateTime &from, const QDateTime &to )
00110 {
00111 return toUTC( to ) - toUTC( from );
00112 }
00113
00114 QCString TimeConversion::toISO8601( const QDate &d )
00115 {
00116 time_t tmp = toUTC( d );
00117 struct tm *utc = gmtime( &tmp );
00118
00119 QCString str;
00120 str.sprintf("%04d%02d%02d", (utc->tm_year + 1900), utc->tm_mon+1, utc->tm_mday );
00121 return str;
00122 }
00123
00124 QCString TimeConversion::toISO8601( const QDateTime &dt )
00125 {
00126 time_t tmp = toUTC( dt );
00127 struct tm *utc = gmtime( &tmp );
00128
00129 QCString str;
00130 str.sprintf("%04d%02d%02dT%02d%02d%02dZ",
00131 (utc->tm_year + 1900), utc->tm_mon+1, utc->tm_mday,
00132 utc->tm_hour, utc->tm_min, utc->tm_sec );
00133 return str;
00134 }
00135
00136 QDateTime TimeConversion::fromISO8601( const QCString &s )
00137 {
00138
00139 #if defined(_OS_WIN32) || defined (Q_OS_WIN32) || defined (Q_OS_WIN64)
00140 _tzset();
00141 #else
00142 tzset();
00143 #endif
00144
00145 struct tm thetime;
00146
00147 QCString str = s.copy();
00148 str.replace(QRegExp("-"), "" );
00149 str.replace(QRegExp(":"), "" );
00150 str.stripWhiteSpace();
00151 str = str.lower();
00152
00153 int i = str.find( "t" );
00154 QCString date;
00155 QCString timestr;
00156 if ( i != -1 ) {
00157 date = str.left( i );
00158 timestr = str.mid( i+1 );
00159 } else {
00160 date = str;
00161 }
00162
00163
00164 memset( &thetime, 0, sizeof(tm) );
00165 thetime.tm_year = 100;
00166 thetime.tm_mon = 0;
00167 thetime.tm_mday = 0;
00168 thetime.tm_hour = 0;
00169 thetime.tm_min = 0;
00170 thetime.tm_sec = 0;
00171
00172
00173
00174 switch( date.length() ) {
00175 case 8:
00176 thetime.tm_mday = date.right( 2 ).toInt();
00177 case 6:
00178 thetime.tm_mon = date.mid( 4, 2 ).toInt() - 1;
00179 case 4:
00180 thetime.tm_year = date.left( 4 ).toInt();
00181 thetime.tm_year -= 1900;
00182 break;
00183 default:
00184 break;
00185 }
00186
00187 int tzoff = 0;
00188 bool inLocalTime = FALSE;
00189 if ( timestr.find( 'z' ) == (int)timestr.length() - 1 )
00190
00191 timestr = timestr.left( timestr.length() -1 );
00192 else {
00193 int plus = timestr.find( "+" );
00194 int minus = timestr.find( "-" );
00195 if ( plus != -1 || minus != -1 ) {
00196
00197 plus = (plus != -1) ? plus : minus;
00198 QCString off = timestr.mid( plus );
00199 timestr = timestr.left( plus );
00200
00201 int tzoffhour = 0;
00202 int tzoffmin = 0;
00203 switch( off.length() ) {
00204 case 5:
00205 tzoffmin = off.mid(3).toInt();
00206 case 3:
00207 tzoffhour = off.left(3).toInt();
00208 default:
00209 break;
00210 }
00211 tzoff = 60*tzoffhour + tzoffmin;
00212 } else
00213 inLocalTime = TRUE;
00214 }
00215
00216
00217 switch( timestr.length() ) {
00218 case 6:
00219 thetime.tm_sec = timestr.mid( 4 ).toInt();
00220 case 4:
00221 thetime.tm_min = timestr.mid( 2, 2 ).toInt();
00222 case 2:
00223 thetime.tm_hour = timestr.left( 2 ).toInt();
00224 default:
00225 break;
00226 }
00227
00228 int tzloc = 0;
00229 time_t tmp = time( 0 );
00230 if ( !inLocalTime ) {
00231
00232 struct tm *lt = localtime( &tmp );
00233 tzloc = mktime( lt );
00234 struct tm *ut = gmtime( &tmp );
00235 tzloc -= mktime( ut );
00236 }
00237
00238
00239
00240 tmp = mktime( &thetime );
00241 tmp += 60*(-tzloc + tzoff);
00242
00243
00244
00245 return fromUTC( tmp );
00246 }
00247
This file is part of the documentation for OPIE Version 1.5.5.