• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepim API Reference
  • KDE Home
  • Contact Us
 

kaddressbook

  • sources
  • kde-4.12
  • kdepim
  • kaddressbook
  • xxport
  • csv
dateparser.cpp
Go to the documentation of this file.
1 /*
2  This file is part of KAddressBook.
3  Copyright (c) 2009 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 along
16  with this program; if not, write to the Free Software Foundation, Inc.,
17  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 */
19 
20 #include "dateparser.h"
21 
22 DateParser::DateParser( const QString &pattern )
23  : mPattern( pattern )
24 {
25 }
26 
27 DateParser::~DateParser()
28 {
29 }
30 
31 QDateTime DateParser::parse( const QString &dateStr ) const
32 {
33  int year, month, day, hour, minute, second;
34  year = month = day = hour = minute = second = 0;
35 
36  int currPos = 0;
37  for ( int i = 0; i < mPattern.length(); ++i ) {
38  if ( mPattern[ i ] == QLatin1Char('y') ) { // 19YY
39  if ( currPos + 1 < dateStr.length() ) {
40  year = 1900 + dateStr.mid( currPos, 2 ).toInt();
41  currPos += 2;
42  } else {
43  return QDateTime();
44  }
45  } else if ( mPattern[ i ] == QLatin1Char('Y') ) { // YYYY
46  if ( currPos + 3 < dateStr.length() ) {
47  year = dateStr.mid( currPos, 4 ).toInt();
48  currPos += 4;
49  } else {
50  return QDateTime();
51  }
52  } else if ( mPattern[ i ] == QLatin1Char('m') ) { // M or MM
53  if ( currPos + 1 < dateStr.length() ) {
54  if ( dateStr[ currPos ].isDigit() ) {
55  if ( dateStr[ currPos + 1 ].isDigit() ) {
56  month = dateStr.mid( currPos, 2 ).toInt();
57  currPos += 2;
58  continue;
59  }
60  }
61  }
62  if ( currPos < dateStr.length() ) {
63  if ( dateStr[ currPos ].isDigit() ) {
64  month = dateStr.mid( currPos, 1 ).toInt();
65  currPos++;
66  continue;
67  }
68  }
69 
70  return QDateTime();
71  } else if ( mPattern[ i ] == QLatin1Char('M') ) { // 0M or MM
72  if ( currPos + 1 < dateStr.length() ) {
73  month = dateStr.mid( currPos, 2 ).toInt();
74  currPos += 2;
75  } else {
76  return QDateTime();
77  }
78  } else if ( mPattern[ i ] == QLatin1Char('d') ) { // D or DD
79  if ( currPos + 1 < dateStr.length() ) {
80  if ( dateStr[ currPos ].isDigit() ) {
81  if ( dateStr[ currPos + 1 ].isDigit() ) {
82  day = dateStr.mid( currPos, 2 ).toInt();
83  currPos += 2;
84  continue;
85  }
86  }
87  }
88  if ( currPos < dateStr.length() ) {
89  if ( dateStr[ currPos ].isDigit() ) {
90  day = dateStr.mid( currPos, 1 ).toInt();
91  currPos++;
92  continue;
93  }
94  }
95 
96  return QDateTime();
97  } else if ( mPattern[ i ] == QLatin1Char('D') ) { // 0D or DD
98  if ( currPos + 1 < dateStr.length() ) {
99  day = dateStr.mid( currPos, 2 ).toInt();
100  currPos += 2;
101  } else {
102  return QDateTime();
103  }
104  } else if ( mPattern[ i ] == QLatin1Char('H') ) { // 0H or HH
105  if ( currPos + 1 < dateStr.length() ) {
106  hour = dateStr.mid( currPos, 2 ).toInt();
107  currPos += 2;
108  } else {
109  return QDateTime();
110  }
111  } else if ( mPattern[ i ] == QLatin1Char('I') ) { // 0I or II
112  if ( currPos + 1 < dateStr.length() ) {
113  minute = dateStr.mid( currPos, 2 ).toInt();
114  currPos += 2;
115  } else {
116  return QDateTime();
117  }
118  } else if ( mPattern[ i ] == QLatin1Char('S') ) { // 0S or SS
119  if ( currPos + 1 < dateStr.length() ) {
120  second = dateStr.mid( currPos, 2 ).toInt();
121  currPos += 2;
122  } else {
123  return QDateTime();
124  }
125  } else {
126  currPos++;
127  }
128  }
129 
130  return QDateTime( QDate( year, month, day ), QTime( hour, minute, second ) );
131 }
DateParser::parse
QDateTime parse(const QString &dateStr) const
Definition: dateparser.cpp:31
DateParser::~DateParser
~DateParser()
Definition: dateparser.cpp:27
dateparser.h
DateParser::DateParser
DateParser(const QString &pattern)
Definition: dateparser.cpp:22
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:55:51 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kaddressbook

Skip menu "kaddressbook"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdepim API Reference

Skip menu "kdepim API Reference"
  • akonadi_next
  • akregator
  • blogilo
  • calendarsupport
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt2
  • kjots
  • kleopatra
  • kmail
  • knode
  • knotes
  • kontact
  • korgac
  • korganizer
  • ktimetracker
  • libkdepim
  • libkleo
  • libkpgp
  • mailcommon
  • messagelist
  • messageviewer

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal