digiKam
geodataparser_time.h
Go to the documentation of this file.
1 /* ============================================================
2  *
3  * This file is a part of digiKam project
4  * https://www.digikam.org
5  *
6  * Date : 2010-01-17
7  * Description : A function to parse time strings to QDateTime.
8  * Put into an extra file for easier testing.
9  *
10  * Copyright (C) 2010 by Michael G. Hansen <mike at mghansen dot de>
11  * Copyright (C) 2017-2022 by Gilles Caulier <caulier dot gilles at gmail dot com>
12  *
13  * This program is free software; you can redistribute it
14  * and/or modify it under the terms of the GNU General
15  * Public License as published by the Free Software Foundation;
16  * either version 2, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * ============================================================ */
24 
25 #ifndef DIGIKAM_GEO_DATA_PARSER_TIME_H
26 #define DIGIKAM_GEO_DATA_PARSER_TIME_H
27 
28 // Qt includes
29 
30 #include <QDateTime>
31 
33 {
34 
35 QDateTime GeoDataParserParseTime(QString timeString)
36 {
37  if (timeString.isEmpty())
38  {
39  return QDateTime();
40  }
41 
42  // we want to be able to parse these formats:
43  // "2010-01-14T09:26:02.287-02:00" <-- here we have to cut off the -02:00 and replace it with 'Z'
44  // "2010-01-14T09:26:02.287+02:00" <-- here we have to cut off the +02:00 and replace it with 'Z'
45  // "2009-03-11T13:39:55.622Z"
46 
47  const int timeStringLength = timeString.length();
48  const int timeZoneSignPosition = timeStringLength-6;
49 
50  // does the string contain a timezone offset?
51 
52  int timeZoneOffsetSeconds = 0;
53  const int timeZonePlusPosition = timeString.lastIndexOf(QLatin1Char('+'));
54  const int timeZoneMinusPosition = timeString.lastIndexOf(QLatin1Char('-'));
55 
56  if ((timeZonePlusPosition == timeZoneSignPosition) ||
57  (timeZoneMinusPosition == timeZoneSignPosition))
58  {
59  const int timeZoneSign = (timeZonePlusPosition == timeZoneSignPosition) ? +1 : -1;
60 
61  // cut off the last digits:
62 
63  const QString timeZoneString = timeString.right(6);
64  timeString.chop(6);
65  timeString += QLatin1Char('Z');
66 
67  // determine the time zone offset:
68 
69  bool okayHour = false;
70  bool okayMinute = false;
71  const int hourOffset = timeZoneString.mid(1, 2).toInt(&okayHour);
72  const int minuteOffset = timeZoneString.mid(4, 2).toInt(&okayMinute);
73 
74  if (okayHour && okayMinute)
75  {
76  timeZoneOffsetSeconds = hourOffset*3600 + minuteOffset*60;
77  timeZoneOffsetSeconds *= timeZoneSign;
78  }
79  }
80 
81  QDateTime theTime = QDateTime::fromString(timeString, Qt::ISODate);
82  theTime = theTime.addSecs(-timeZoneOffsetSeconds);
83 
84  return theTime;
85 }
86 
87 } // namespace DigikamGenericGeolocationEditPlugin
88 
89 #endif // DIGIKAM_GEO_DATA_PARSER_TIME_H
Definition: geolocationedit.cpp:97
QDateTime GeoDataParserParseTime(QString timeString)
Definition: geodataparser_time.h:35