libopie PIM API Documentation

oconversion.cpp

Go to the documentation of this file.
00001 /**********************************************************************
00002 ** Copyright (C) 2003 by Stefan Eilers (eilers.stefan@epost.de)
00003 **
00004 ** This file may be distributed and/or modified under the terms of the
00005 ** GNU Lesser General Public License version 2 as published by the Free Software
00006 ** Foundation and appearing in the file LICENSE.GPL included in the
00007 ** packaging of this file.
00008 **
00009 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00010 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00011 **
00012 **********************************************************************/
00013 
00014 #include "oconversion.h"
00015 #include <qpe/timeconversion.h>
00016 
00017 
00018 QString OConversion::dateToString( const QDate &d )
00019 {
00020     if ( d.isNull() || !d.isValid() )
00021         return QString::null;
00022 
00023     // ISO format in year, month, day (YYYYMMDD); e.g. 20021231
00024     QString year = QString::number( d.year() );
00025     QString month = QString::number( d.month() );
00026     month = month.rightJustify( 2, '0' );
00027     QString day = QString::number( d.day() );
00028     day = day.rightJustify( 2, '0' );
00029 
00030     QString str = year + month + day;
00031     //qDebug( "\tPimContact dateToStr = %s", str.latin1() );
00032 
00033     return str;
00034 }
00035 
00036 QDate OConversion::dateFromString( const QString& s )
00037 {
00038     QDate date;
00039 
00040     if ( s.isEmpty() )
00041         return date;
00042 
00043     // Be backward compatible to old Opie format:
00044     // Try to load old format. If it fails, try new ISO-Format!
00045     date = TimeConversion::fromString ( s );
00046     if ( date.isValid() )
00047         return date;
00048 
00049     // Read ISO-Format (YYYYMMDD)
00050     int year = s.mid(0, 4).toInt();
00051     int month = s.mid(4,2).toInt();
00052     int day = s.mid(6,2).toInt();
00053 
00054     // do some quick sanity checking -eilers
00055         // but we isValid() again? -zecke
00056     if ( year < 1900 || year > 3000 ) {
00057         qWarning( "PimContact year is not in range");
00058         return date;
00059     }
00060     if ( month < 0 || month > 12 ) {
00061         qWarning( "PimContact month is not in range");
00062         return date;
00063     }
00064     if ( day < 0 || day > 31 ) {
00065         qWarning( "PimContact day is not in range");
00066         return date;
00067     }
00068 
00069     date.setYMD( year, month, day );
00070     if ( !date.isValid() ) {
00071         qWarning( "PimContact date is not valid");
00072         return date;
00073     }
00074 
00075     return date;
00076 }
00077 QString OConversion::dateTimeToString( const QDateTime& dt ) {
00078     if (!dt.isValid() || dt.isNull() ) return QString::null;
00079 
00080     QString year  = QString::number( dt.date().year()   );
00081     QString month = QString::number( dt.date().month()  );
00082     QString day   = QString::number( dt.date().day()    );
00083 
00084     QString hour  = QString::number( dt.time().hour()   );
00085     QString min   = QString::number( dt.time().minute() );
00086     QString sec   = QString::number( dt.time().second() );
00087 
00088     month = month.rightJustify( 2, '0' );
00089     day   = day.  rightJustify( 2, '0' );
00090     hour  = hour. rightJustify( 2, '0' );
00091     min   = min.  rightJustify( 2, '0' );
00092     sec   = sec.  rightJustify( 2, '0' );
00093 
00094     QString str = day + month + year + hour + min + sec;
00095 
00096     return str;
00097 }
00098 QDateTime OConversion::dateTimeFromString( const QString& str) {
00099 
00100     if ( str.isEmpty() ) return QDateTime();
00101     int day   = str.mid(0, 2).toInt();
00102     int month = str.mid(2, 2).toInt();
00103     int year  = str.mid(4, 4).toInt();
00104     int hour  = str.mid(8, 2).toInt();
00105     int min   = str.mid(10, 2).toInt();
00106     int sec   = str.mid(12, 2).toInt();
00107 
00108     QDate date( year, month, day );
00109     QTime time( hour, min, sec );
00110     QDateTime dt( date, time );
00111     return dt;
00112 }
00113 
KDE Logo
This file is part of the documentation for OPIE Version 1.1.
Documentation copyright © 1997-2003 the KDE developers. 2003 OPIE developers
Generated on Tue Feb 10 20:25:19 2004 by doxygen 1.3.5 written by Dimitri van Heesch, © 1997-2001