summaryrefslogtreecommitdiff
path: root/ext/Log4Qt/src/helpers/datetime.cpp
blob: a51b8eb2743c0ea1c98f7a20dc8899299607d2dc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/******************************************************************************
 *
 * package:     Log4Qt
 * file:        datetime.cpp
 * created:     September 2007
 * author:      Martin Heinrich
 *
 *
 * Copyright 2007 Martin Heinrich
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 ******************************************************************************/



/******************************************************************************
 * Dependencies
 ******************************************************************************/


#include "helpers/datetime.h"

#include <QtCore/QDebug>
#include "helpers/initialisationhelper.h"



namespace Log4Qt
{


	/**************************************************************************
	 *Declarations
	 **************************************************************************/



	/**************************************************************************
	 * C helper functions
	 **************************************************************************/



	/**************************************************************************
	 * Class implementation: DateTime
	 **************************************************************************/


	QString DateTime::toString(const QString &rFormat) const
	{
			QString format(rFormat);

			if (format.isEmpty())
					return QString();
			if (!isValid())
					return QString();
			if (format == QLatin1String("NONE"))
					return QString();

			if (format == QLatin1String("RELATIVE"))
					return QString::number(toMilliSeconds() - InitialisationHelper::startTime());

			if (format == QLatin1String("ISO8601"))
					format = QLatin1String("yyyy-MM-dd hh:mm:ss.zzz");
			if (format == QLatin1String("ABSOLUTE"))
					format = QLatin1String("HH:mm:ss.zzz");
			if (format == QLatin1String("DATE"))
					format = QLatin1String("dd MMM YYYY HH:mm:ss.zzzz");

			return formatDateTime(format);
	}


	QString DateTime::formatDateTime(const QString &rFormat) const
	{
			if (rFormat.isEmpty())
					return QString();
			if (!isValid())
					return QString();

			const QLatin1Char null('0');
			const QLatin1Char quote('\'');
			const QString tokens = QLatin1String("\'dMyhHmszAPapw");
			const bool am_pm = hasAMPM(rFormat);

			QString result;
			QString token;
			QChar expected = null;

			QChar c;
			int i;
			for (i = 0; i < rFormat.length(); i++)
			{
					c = rFormat.at(i);

					// Handle literal text
					if (expected == quote)
					{
							if (c == quote)
							{
									Q_ASSERT_X(i > 0, "DateTime::toString()", "Found quote with status quote at i = 0");
									if (i > 0 && rFormat.at(i - 1) == quote)
											// Second of two quotes
											result += quote;
									expected = null;
							}
							else
									// Next literal character
									result += c;
					}
					else if (c == expected)
					{
							// Extend token
							token += c;
					}
					else
					{
							// Close last token
							result += formatToken(token, am_pm);
							token.clear();
							expected = null;

							// Test for valid character
							if (tokens.indexOf(c) >= 0)
							{
									if (c == QLatin1Char('a'))
											expected = QLatin1Char('p');
									else if (c == QLatin1Char('A'))
											expected = QLatin1Char('P');
									else if (c.toLower() == QLatin1Char('p'))
											expected = null;
									else
											expected = c;
									if (c != quote)
											token += c;
							} else
									result += c;
					}
			}

			result += formatToken(token, am_pm);
			return result;
	}


	QString DateTime::formatToken(const QString &rToken, bool am_pm) const
	{
			if (rToken.isEmpty())
					return QString();

			const QChar c = rToken.at(0);
			QString result;
			int used = 0;

			// Qt data format strings
			if (rToken.startsWith(QLatin1String("dddd")))
			{
					result = QDate::longDayName(date().dayOfWeek());
					used = 4;
			}
			else if (rToken.startsWith(QLatin1String("ddd")))
			{
					result = QDate::shortDayName(date().dayOfWeek());
					used = 3;
			}
			else if (rToken.startsWith(QLatin1String("dd")))
			{
					result = QString::number(date().day()).rightJustified(2, QLatin1Char('0'), true);
					used = 2;
			}
			else if (c == QLatin1Char('d'))
			{
					result = QString::number(date().day());
					used = 1;
			}
			else if (rToken.startsWith(QLatin1String("MMMM")))
			{
					result = QDate::longMonthName(date().month());
					used = 4;
			}
			else if (rToken.startsWith(QLatin1String("MMM")))
			{
					result = QDate::shortMonthName(date().month());
					used = 3;
			}
			else if (rToken.startsWith(QLatin1String("MM")))
			{
					result = QString::number(date().month()).rightJustified(2, QLatin1Char('0'), true);
					used = 2;
			}
			else if (c == QLatin1Char('M'))
			{
					result = QString::number(date().month());
					used = 1;
			}
			else if (rToken.startsWith(QLatin1String("yyyy")))
			{
					result = QString::number(date().year());
					used = 4;
			}
			else if (rToken.startsWith(QLatin1String("yy")))
			{
					result = QString::number(date().year() % 100).rightJustified(2, QLatin1Char('0'), true);
					used = 2;
			}

			// Qt time format strings
			else if (rToken.startsWith(QLatin1String("hh")) || rToken.startsWith(QLatin1String("HH")))
			{
					int hour = time().hour();
					if (am_pm && c == QLatin1Char('h') && hour > 12)
							hour -= 12;
					result = QString::number(hour).rightJustified(2, QLatin1Char('0'), true);
					used = 2;
			}
			else if (c == QLatin1Char('h') || c == QLatin1Char('H'))
			{
					int hour = time().hour();
					if (am_pm && c == QLatin1Char('h') && hour > 12)
							hour -= 12;
					result = QString::number(hour);
					used = 2;
			}
			else if (rToken.startsWith(QLatin1String("mm")))
			{
					result = QString::number(time().minute()).rightJustified(2, QLatin1Char('0'), true);
					used = 2;
			}
			else if (c == (QLatin1Char('m')))
			{
					result = QString::number(time().minute());
					used = 1;
			}
			else if (rToken.startsWith(QLatin1String("ss")))
			{
					result = QString::number(time().second()).rightJustified(2, QLatin1Char('0'), true);
					used = 2;
			}
			else if (c == QLatin1Char('s'))
			{
					result = QString::number(time().second());
					used = 1;
			}
			else if (rToken.startsWith(QLatin1String("zzz")))
			{
					result = QString::number(time().msec()).rightJustified(3, QLatin1Char('0'), true);
					used = 3;
			}
			else if (c == QLatin1Char('z'))
			{
					result = QString::number(time().msec());
					used = 1;
			}
			else if (c.toLower() == QLatin1Char('a'))
			{
					bool is_lower = c == QLatin1Char('a');
					if (time().hour() < 12)
							result = QLatin1String("AM");
					else
							result = QLatin1String("PM");
					if (is_lower)
							result = result.toLower();
					if (rToken.size() > 1 &&
							((is_lower && rToken.at(1) == QLatin1Char('p')) ||
							 (!is_lower && rToken.at(1) == QLatin1Char('P')))
							)
							used = 2;
					else
							used = 1;
			}

			// Extension for week number
			else if (rToken.startsWith(QLatin1String("ww")))
			{
					result = QString::number(date().weekNumber()).rightJustified(2, QLatin1Char('0'), true);
					used = 2;
			}
			else if (c == QLatin1Char('w'))
			{
					result = QString::number(date().weekNumber());
					used = 1;
			}

			if (used)
					return result + formatToken(rToken.mid(used), am_pm);
			else
					return result;
	}


	bool DateTime::hasAMPM(const QString &rToken)
	{
			bool in_literal = false;
			QChar c;
			int i;
			for (i = 0; i < rToken.length(); i++)
			{
					c = rToken.at(i);
					if (c == QLatin1Char('\''))
							in_literal = !in_literal;
					else if (!in_literal && c.toLower() == QLatin1Char('a'))
							return true;
			}
			return false;
	}



	/**************************************************************************
	 * Implementation: Operators, Helper
	 **************************************************************************/



} // namespace Log4Qt