Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

opimdateconversion.cpp

Go to the documentation of this file.
00001 /*
00002                             This file is part of the Opie Project
00003              =.             Copyright (C) The Opie Team <opie-devel@handhelds.org>
00004            .=l.
00005           .>+-=
00006 _;:,     .>    :=|.         This program is free software; you can
00007 .> <`_,   >  .   <=         redistribute it and/or  modify it under
00008 :`=1 )Y*s>-.--   :          the terms of the GNU Library General Public
00009 .="- .-=="i,     .._        License as published by the Free Software
00010 - .   .-<_>     .<>         Foundation; either version 2 of the License,
00011     ._= =}       :          or (at your option) any later version.
00012    .%`+i>       _;_.
00013    .i_,=:_.      -<s.       This program is distributed in the hope that
00014     +  .  -:.       =       it will be useful,  but WITHOUT ANY WARRANTY;
00015    : ..    .:,     . . .    without even the implied warranty of
00016    =_        +     =;=|`    MERCHANTABILITY or FITNESS FOR A
00017  _.=:.       :    :=>`:     PARTICULAR PURPOSE. See the GNU
00018 ..}^=.=       =       ;     Library General Public License for more
00019 ++=   -.     .`     .:      details.
00020 :     =  ...= . :.=-
00021 -.   .:....=;==+<;          You should have received a copy of the GNU
00022  -_. . .   )=.  =           Library General Public License along with
00023    --        :-=`           this library; see the file COPYING.LIB.
00024                             If not, write to the Free Software Foundation,
00025                             Inc., 59 Temple Place - Suite 330,
00026                             Boston, MA 02111-1307, USA.
00027 */
00028 
00029 /* OPIE */
00030 #include <opie2/opimdateconversion.h>
00031 #include <opie2/odebug.h>
00032 
00033 #include <qpe/timeconversion.h>
00034 
00035 namespace Opie
00036 {
00037 
00038 QString OPimDateConversion::dateToString( const QDate &d )
00039 {
00040     if ( d.isNull() || !d.isValid() )
00041         return QString::null;
00042 
00043     // ISO format in year, month, day (YYYYMMDD); e.g. 20021231
00044     QString year = QString::number( d.year() );
00045     QString month = QString::number( d.month() );
00046     month = month.rightJustify( 2, '0' );
00047     QString day = QString::number( d.day() );
00048     day = day.rightJustify( 2, '0' );
00049 
00050     QString str = year + month + day;
00051     //odebug << "\tPimContact dateToStr = " << str << "" << oendl;
00052 
00053     return str;
00054 }
00055 
00056 
00057 QDate OPimDateConversion::dateFromString( const QString& s )
00058 {
00059     QDate date;
00060 
00061     if ( s.isEmpty() )
00062         return date;
00063 
00064     // Be backward compatible to old Opie format:
00065     // Try to load old format. If it fails, try new ISO-Format!
00066     date = TimeConversion::fromString ( s );
00067     if ( date.isValid() )
00068         return date;
00069 
00070     // Read ISO-Format (YYYYMMDD)
00071     int year = s.mid( 0, 4 ).toInt();
00072     int month = s.mid( 4, 2 ).toInt();
00073     int day = s.mid( 6, 2 ).toInt();
00074 
00075     // do some quick sanity checking -eilers
00076     // but we isValid() again? -zecke
00077     if ( year < 1900 || year > 3000 )
00078     {
00079         owarn << "PimContact year is not in range" << oendl;
00080         return date;
00081     }
00082     if ( month < 0 || month > 12 )
00083     {
00084         owarn << "PimContact month is not in range" << oendl;
00085         return date;
00086     }
00087     if ( day < 0 || day > 31 )
00088     {
00089         owarn << "PimContact day is not in range" << oendl;
00090         return date;
00091     }
00092 
00093     date.setYMD( year, month, day );
00094     if ( !date.isValid() )
00095     {
00096         owarn << "PimContact date is not valid" << oendl;
00097         return date;
00098     }
00099 
00100     return date;
00101 }
00102 
00103 
00104 QString OPimDateConversion::dateTimeToString( const QDateTime& dt )
00105 {
00106     if ( !dt.isValid() || dt.isNull() )
00107         return QString::null;
00108 
00109     QString year = QString::number( dt.date().year() );
00110     QString month = QString::number( dt.date().month() );
00111     QString day = QString::number( dt.date().day() );
00112 
00113     QString hour = QString::number( dt.time().hour() );
00114     QString min = QString::number( dt.time().minute() );
00115     QString sec = QString::number( dt.time().second() );
00116 
00117     month = month.rightJustify( 2, '0' );
00118     day = day. rightJustify( 2, '0' );
00119     hour = hour. rightJustify( 2, '0' );
00120     min = min. rightJustify( 2, '0' );
00121     sec = sec. rightJustify( 2, '0' );
00122 
00123     return day + month + year + hour + min + sec;
00124 }
00125 
00126 
00127 QDateTime OPimDateConversion::dateTimeFromString( const QString& str )
00128 {
00129 
00130     if ( str.isEmpty() )
00131         return QDateTime();
00132     int day = str.mid( 0, 2 ).toInt();
00133     int month = str.mid( 2, 2 ).toInt();
00134     int year = str.mid( 4, 4 ).toInt();
00135     int hour = str.mid( 8, 2 ).toInt();
00136     int min = str.mid( 10, 2 ).toInt();
00137     int sec = str.mid( 12, 2 ).toInt();
00138 
00139     QDate date( year, month, day );
00140     QTime time( hour, min, sec );
00141     QDateTime dt( date, time );
00142     return dt;
00143 }
00144 
00145 }

Generated on Sat Nov 5 16:16:19 2005 for OPIE by  doxygen 1.4.2