BuildmLearn Toolkit  2.0.4
BuildmLearn Toolkit is an easy-to-use program that helps users make mobile apps without any knowledge of application development.
 All Classes Functions Enumerations Groups Pages
textfactory.cpp
1 /*
2  Copyright (c) 2012, BuildmLearn Contributors listed at http://buildmlearn.org/people/
3  All rights reserved.
4 
5  Redistribution and use in source and binary forms, with or without
6  modification, are permitted provided that the following conditions are met:
7 
8  * Redistributions of source code must retain the above copyright notice, this
9  list of conditions and the following disclaimer.
10 
11  * Redistributions in binary form must reproduce the above copyright notice,
12  this list of conditions and the following disclaimer in the documentation
13  and/or other materials provided with the distribution.
14 
15  * Neither the name of the BuildmLearn nor the names of its
16  contributors may be used to endorse or promote products derived from
17  this software without specific prior written permission.
18 
19  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30 
31 #include "miscellaneous/textfactory.h"
32 
33 #include "definitions/definitions.h"
34 
35 #include <QString>
36 #include <QStringList>
37 #include <QLocale>
38 
39 
40 TextFactory::TextFactory() {
41 }
42 
43 QDateTime TextFactory::parseDateTime(const QString &date_time) {
44  QString date = date_time.simplified();
45  QStringList date_patterns;
46  QStringList timezone_offset_patterns;
47  QDateTime dt;
48  QTime time_zone_offset;
49  QLocale locale(QLocale::C);
50  bool positive_time_zone_offset = false;
51 
52  date_patterns << "yyyy-MM-ddTHH:mm:ss" << "MMM dd yyyy hh:mm:ss" <<
53  "MMM d yyyy hh:mm:ss" << "ddd, dd MMM yyyy HH:mm:ss" <<
54  "dd MMM yyyy" << "yyyy-MM-dd HH:mm:ss.z" << "yyyy-MM-dd" <<
55  "yyyy" << "yyyy-MM" << "yyyy-MM-dd" << "yyyy-MM-ddThh:mm" <<
56  "yyyy-MM-ddThh:mm:ss";
57 
58  timezone_offset_patterns << "+hh:mm" << "-hh:mm" << "+hhmm" << "-hhmm" << "+hh" << "-hh";
59 
60  if (date.size() >= 5) {
61  foreach (const QString &pattern, timezone_offset_patterns) {
62  time_zone_offset = QTime::fromString(date.right(pattern.size()), pattern);
63 
64  if (time_zone_offset.isValid()) {
65  positive_time_zone_offset = pattern.at(0) == '+';
66  break;
67  }
68  }
69  }
70 
71  // Iterate over patterns and check if input date/time matches the pattern.
72  foreach (const QString &pattern, date_patterns) {
73  dt = locale.toDateTime(date.left(pattern.size()), pattern);
74 
75  if (dt.isValid()) {
76  // Make sure that this date/time is considered UTC.
77  dt.setTimeSpec(Qt::UTC);
78 
79  if (time_zone_offset.isValid()) {
80  // Time zone offset was detected.
81  if (positive_time_zone_offset) {
82  // Offset is positive, so we have to subtract it to get
83  // the original UTC.
84  return dt.addSecs(- QTime(0, 0, 0, 0).secsTo(time_zone_offset));
85  }
86  else {
87  // Vice versa.
88  return dt.addSecs(QTime(0, 0, 0, 0).secsTo(time_zone_offset));
89  }
90  }
91  else {
92  return dt;
93  }
94  }
95  }
96 
97  // Parsing failed, return invalid datetime.
98  return QDateTime();
99 }
100 
101 QDateTime TextFactory::parseDateTime(qint64 milis_from_epoch) {
102  return QDateTime::fromMSecsSinceEpoch(milis_from_epoch);
103 }
104 
105 QString TextFactory::shorten(const QString &input, int text_length_limit) {
106  if (input.size() > text_length_limit) {
107  return input.left(text_length_limit - ELLIPSIS_LENGTH) + QString(ELLIPSIS_LENGTH, '.');
108  }
109  else {
110  return input;
111  }
112 }
static QString shorten(const QString &input, int text_length_limit)
Shortens input string according to given length limit.
static QDateTime parseDateTime(const QString &date_time)
Tries to parse input textual date/time representation.
Definition: textfactory.cpp:43