File indexing completed on 2025-01-19 04:46:30

0001 /*
0002   This file is part of KAddressBook.
0003   SPDX-FileCopyrightText: 2009 Tobias Koenig <tokoe@kde.org>
0004 
0005   SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 #include "dateparser.h"
0009 
0010 DateParser::DateParser(const QString &pattern)
0011     : mPattern(pattern)
0012 {
0013 }
0014 
0015 DateParser::~DateParser() = default;
0016 
0017 QDateTime DateParser::parse(const QString &dateStr) const
0018 {
0019     int year = 0;
0020     int month = 0;
0021     int day = 0;
0022     int hour = 0;
0023     int minute = 0;
0024     int second = 0;
0025 
0026     int currPos = 0;
0027     for (int i = 0; i < mPattern.length(); ++i) {
0028         if (mPattern[i] == QLatin1Char('y')) { // 19YY
0029             if (currPos + 1 < dateStr.length()) {
0030                 year = 1900 + QStringView(dateStr).mid(currPos, 2).toInt();
0031 
0032                 currPos += 2;
0033             } else {
0034                 return {};
0035             }
0036         } else if (mPattern[i] == QLatin1Char('Y')) { // YYYY
0037             if (currPos + 3 < dateStr.length()) {
0038                 year = QStringView(dateStr).mid(currPos, 4).toInt();
0039                 currPos += 4;
0040             } else {
0041                 return {};
0042             }
0043         } else if (mPattern[i] == QLatin1Char('m')) { // M or MM
0044             if (currPos + 1 < dateStr.length()) {
0045                 if (dateStr[currPos].isDigit()) {
0046                     if (dateStr[currPos + 1].isDigit()) {
0047                         month = QStringView(dateStr).mid(currPos, 2).toInt();
0048 
0049                         currPos += 2;
0050                         continue;
0051                     }
0052                 }
0053             }
0054             if (currPos < dateStr.length()) {
0055                 if (dateStr[currPos].isDigit()) {
0056                     month = QStringView(dateStr).mid(currPos, 1).toInt();
0057 
0058                     currPos++;
0059                     continue;
0060                 }
0061             }
0062 
0063             return {};
0064         } else if (mPattern[i] == QLatin1Char('M')) { // 0M or MM
0065             if (currPos + 1 < dateStr.length()) {
0066                 month = QStringView(dateStr).mid(currPos, 2).toInt();
0067                 currPos += 2;
0068             } else {
0069                 return {};
0070             }
0071         } else if (mPattern[i] == QLatin1Char('d')) { // D or DD
0072             if (currPos + 1 < dateStr.length()) {
0073                 if (dateStr[currPos].isDigit()) {
0074                     if (dateStr[currPos + 1].isDigit()) {
0075                         day = QStringView(dateStr).mid(currPos, 2).toInt();
0076                         currPos += 2;
0077                         continue;
0078                     }
0079                 }
0080             }
0081             if (currPos < dateStr.length()) {
0082                 if (dateStr[currPos].isDigit()) {
0083                     day = QStringView(dateStr).mid(currPos, 1).toInt();
0084                     currPos++;
0085                     continue;
0086                 }
0087             }
0088 
0089             return {};
0090         } else if (mPattern[i] == QLatin1Char('D')) { // 0D or DD
0091             if (currPos + 1 < dateStr.length()) {
0092                 day = QStringView(dateStr).mid(currPos, 2).toInt();
0093                 currPos += 2;
0094             } else {
0095                 return {};
0096             }
0097         } else if (mPattern[i] == QLatin1Char('H')) { // 0H or HH
0098             if (currPos + 1 < dateStr.length()) {
0099                 hour = QStringView(dateStr).mid(currPos, 2).toInt();
0100                 currPos += 2;
0101             } else {
0102                 return {};
0103             }
0104         } else if (mPattern[i] == QLatin1Char('I')) { // 0I or II
0105             if (currPos + 1 < dateStr.length()) {
0106                 minute = QStringView(dateStr).mid(currPos, 2).toInt();
0107                 currPos += 2;
0108             } else {
0109                 return {};
0110             }
0111         } else if (mPattern[i] == QLatin1Char('S')) { // 0S or SS
0112             if (currPos + 1 < dateStr.length()) {
0113                 second = QStringView(dateStr).mid(currPos, 2).toInt();
0114                 currPos += 2;
0115             } else {
0116                 return {};
0117             }
0118         } else {
0119             currPos++;
0120         }
0121     }
0122 
0123     return QDateTime(QDate(year, month, day), QTime(hour, minute, second));
0124 }