kaddressbook Library API Documentation

dateparser.cpp

00001 /*
00002     This file is part of KAddressbook.
00003     Copyright (c) 2003 Tobias Koenig <tokoe@kde.org>
00004 
00005     This program is free software; you can redistribute it and/or modify
00006     it under the terms of the GNU General Public License as published by
00007     the Free Software Foundation; either version 2 of the License, or
00008     (at your option) any later version.
00009 
00010     This program is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00013     GNU General Public License for more details.
00014 
00015     You should have received a copy of the GNU General Public License
00016     along with this program; if not, write to the Free Software
00017     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00018 
00019     As a special exception, permission is given to link this program
00020     with any edition of Qt, and distribute the resulting executable,
00021     without including the source code for Qt in the source distribution.
00022 */
00023 
00024 #include <qdatetime.h>
00025 
00026 #include "dateparser.h"
00027 
00028 DateParser::DateParser( const QString &pattern )
00029   : mPattern( pattern )
00030 {
00031 }
00032 
00033 DateParser::~DateParser()
00034 {
00035 }
00036 
00037 QDate DateParser::parse( const QString &dateStr ) const
00038 {
00039   int year, month, day;
00040   year = month = day = 0;
00041 
00042   uint currPos = 0;
00043   for ( uint i = 0; i < mPattern.length(); ++i ) {
00044     if ( mPattern[ i ] == 'y' ) { // 19YY
00045       if ( currPos + 1 < dateStr.length() ) {
00046         year = 1900 + dateStr.mid( currPos, 2 ).toInt();
00047         currPos += 2;
00048       } else
00049         return QDate();
00050     } else if ( mPattern[ i ] == 'Y' ) { // YYYY
00051       if ( currPos + 3 < dateStr.length() ) {
00052         year = dateStr.mid( currPos, 4 ).toInt();
00053         currPos += 4;
00054       } else
00055         return QDate();
00056     } else if ( mPattern[ i ] == 'm' ) { // M or MM
00057       if ( currPos + 1 < dateStr.length() ) {
00058         if ( dateStr[ currPos ].isDigit() ) {
00059           if ( dateStr[ currPos + 1 ].isDigit() ) {
00060             month = dateStr.mid( currPos, 2 ).toInt();
00061             currPos += 2;
00062             continue;
00063           }
00064         }
00065       }
00066       if ( currPos < dateStr.length() ) {
00067         if ( dateStr[ currPos ].isDigit() ) {
00068           month = dateStr.mid( currPos, 1 ).toInt();
00069           currPos++;
00070           continue;
00071         }
00072       }
00073 
00074       return QDate();
00075     } else if ( mPattern[ i ] == 'M' ) { // 0M or MM
00076       if ( currPos + 1 < dateStr.length() ) {
00077         month = dateStr.mid( currPos, 2 ).toInt();
00078         currPos += 2;
00079       } else
00080         return QDate();
00081     } else if ( mPattern[ i ] == 'd' ) { // D or DD
00082       if ( currPos + 1 < dateStr.length() ) {
00083         if ( dateStr[ currPos ].isDigit() ) {
00084           if ( dateStr[ currPos + 1 ].isDigit() ) {
00085             day = dateStr.mid( currPos, 2 ).toInt();
00086             currPos += 2;
00087             continue;
00088           }
00089         }
00090       }
00091       if ( currPos < dateStr.length() ) {
00092         if ( dateStr[ currPos ].isDigit() ) {
00093           day = dateStr.mid( currPos, 1 ).toInt();
00094           currPos++;
00095           continue;
00096         }
00097       }
00098 
00099       return QDate();
00100     } else if ( mPattern[ i ] == 'D' ) { // 0D or DD
00101       if ( currPos + 1 < dateStr.length() ) {
00102         day = dateStr.mid( currPos, 2 ).toInt();
00103         currPos += 2;
00104       } else
00105         return QDate();
00106     } else {
00107       currPos++;
00108     }
00109   }
00110 
00111   return QDate( year, month, day );
00112 }
KDE Logo
This file is part of the documentation for kaddressbook Library Version 3.2.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sat May 1 11:38:51 2004 by doxygen 1.2.15 written by Dimitri van Heesch, © 1997-2003