kaddressbook

dateparser.cpp
1 /*
2  This file is part of KAddressbook.
3  Copyright (c) 2003 Tobias Koenig <tokoe@kde.org>
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program; if not, write to the Free Software
17  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 
19  As a special exception, permission is given to link this program
20  with any edition of TQt, and distribute the resulting executable,
21  without including the source code for TQt in the source distribution.
22 */
23 
24 #include <tqdatetime.h>
25 
26 #include "dateparser.h"
27 
28 DateParser::DateParser( const TQString &pattern )
29  : mPattern( pattern )
30 {
31 }
32 
33 DateParser::~DateParser()
34 {
35 }
36 
37 TQDate DateParser::parse( const TQString &dateStr ) const
38 {
39  int year, month, day;
40  year = month = day = 0;
41 
42  uint currPos = 0;
43  for ( uint i = 0; i < mPattern.length(); ++i ) {
44  if ( mPattern[ i ] == 'y' ) { // 19YY
45  if ( currPos + 1 < dateStr.length() ) {
46  year = 1900 + dateStr.mid( currPos, 2 ).toInt();
47  currPos += 2;
48  } else
49  return TQDate();
50  } else if ( mPattern[ i ] == 'Y' ) { // YYYY
51  if ( currPos + 3 < dateStr.length() ) {
52  year = dateStr.mid( currPos, 4 ).toInt();
53  currPos += 4;
54  } else
55  return TQDate();
56  } else if ( mPattern[ i ] == 'm' ) { // M or MM
57  if ( currPos + 1 < dateStr.length() ) {
58  if ( dateStr[ currPos ].isDigit() ) {
59  if ( dateStr[ currPos + 1 ].isDigit() ) {
60  month = dateStr.mid( currPos, 2 ).toInt();
61  currPos += 2;
62  continue;
63  }
64  }
65  }
66  if ( currPos < dateStr.length() ) {
67  if ( dateStr[ currPos ].isDigit() ) {
68  month = dateStr.mid( currPos, 1 ).toInt();
69  currPos++;
70  continue;
71  }
72  }
73 
74  return TQDate();
75  } else if ( mPattern[ i ] == 'M' ) { // 0M or MM
76  if ( currPos + 1 < dateStr.length() ) {
77  month = dateStr.mid( currPos, 2 ).toInt();
78  currPos += 2;
79  } else
80  return TQDate();
81  } else if ( mPattern[ i ] == 'd' ) { // D or DD
82  if ( currPos + 1 < dateStr.length() ) {
83  if ( dateStr[ currPos ].isDigit() ) {
84  if ( dateStr[ currPos + 1 ].isDigit() ) {
85  day = dateStr.mid( currPos, 2 ).toInt();
86  currPos += 2;
87  continue;
88  }
89  }
90  }
91  if ( currPos < dateStr.length() ) {
92  if ( dateStr[ currPos ].isDigit() ) {
93  day = dateStr.mid( currPos, 1 ).toInt();
94  currPos++;
95  continue;
96  }
97  }
98 
99  return TQDate();
100  } else if ( mPattern[ i ] == 'D' ) { // 0D or DD
101  if ( currPos + 1 < dateStr.length() ) {
102  day = dateStr.mid( currPos, 2 ).toInt();
103  currPos += 2;
104  } else
105  return TQDate();
106  } else {
107  currPos++;
108  }
109  }
110 
111  return TQDate( year, month, day );
112 }