datetime.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /******************************************************************************
  2. *
  3. * package: Log4Qt
  4. * file: datetime.cpp
  5. * created: September 2007
  6. * author: Martin Heinrich
  7. *
  8. *
  9. * Copyright 2007 Martin Heinrich
  10. *
  11. * Licensed under the Apache License, Version 2.0 (the "License");
  12. * you may not use this file except in compliance with the License.
  13. * You may obtain a copy of the License at
  14. *
  15. * http://www.apache.org/licenses/LICENSE-2.0
  16. *
  17. * Unless required by applicable law or agreed to in writing, software
  18. * distributed under the License is distributed on an "AS IS" BASIS,
  19. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  20. * See the License for the specific language governing permissions and
  21. * limitations under the License.
  22. *
  23. ******************************************************************************/
  24. /******************************************************************************
  25. * Dependencies
  26. ******************************************************************************/
  27. #include "log4qt/helpers/datetime.h"
  28. #include <QtCore/QDebug>
  29. #include "log4qt/helpers/initialisationhelper.h"
  30. namespace Log4Qt
  31. {
  32. /**************************************************************************
  33. *Declarations
  34. **************************************************************************/
  35. /**************************************************************************
  36. * C helper functions
  37. **************************************************************************/
  38. /**************************************************************************
  39. * Class implementation: DateTime
  40. **************************************************************************/
  41. QString DateTime::toString(const QString &rFormat) const
  42. {
  43. QString format(rFormat);
  44. if (format.isEmpty())
  45. return QString();
  46. if (!isValid())
  47. return QString();
  48. if (format == QLatin1String("NONE"))
  49. return QString();
  50. if (format == QLatin1String("TIME_RELATIVE"))
  51. return QString::number(toMilliSeconds() - InitialisationHelper::startTime());
  52. if (format == QLatin1String("ISO8601"))
  53. format = QLatin1String("yyyy-MM-dd hh:mm:ss.zzz");
  54. if (format == QLatin1String("TIME_ABSOLUTE"))
  55. format = QLatin1String("HH:mm:ss.zzz");
  56. if (format == QLatin1String("DATE"))
  57. format = QLatin1String("dd MMM YYYY HH:mm:ss.zzzz");
  58. return formatDateTime(format);
  59. }
  60. QString DateTime::formatDateTime(const QString &rFormat) const
  61. {
  62. if (rFormat.isEmpty())
  63. return QString();
  64. if (!isValid())
  65. return QString();
  66. const QLatin1Char null('0');
  67. const QLatin1Char quote('\'');
  68. const QString tokens = QLatin1String("\'dMyhHmszAPapw");
  69. const bool am_pm = hasAMPM(rFormat);
  70. QString result;
  71. QString token;
  72. QChar expected = null;
  73. QChar c;
  74. int i;
  75. for (i = 0; i < rFormat.length(); i++)
  76. {
  77. c = rFormat.at(i);
  78. // Handle literal text
  79. if (expected == quote)
  80. {
  81. if (c == quote)
  82. {
  83. Q_ASSERT_X(i > 0, "DateTime::toString()", "Found quote with status quote at i = 0");
  84. if (i > 0 && rFormat.at(i - 1) == quote)
  85. // Second of two quotes
  86. result += quote;
  87. expected = null;
  88. }
  89. else
  90. // Next literal character
  91. result += c;
  92. }
  93. else if (c == expected)
  94. {
  95. // Extend token
  96. token += c;
  97. }
  98. else
  99. {
  100. // Close last token
  101. result += formatToken(token, am_pm);
  102. token.clear();
  103. expected = null;
  104. // Test for valid character
  105. if (tokens.indexOf(c) >= 0)
  106. {
  107. if (c == QLatin1Char('a'))
  108. expected = QLatin1Char('p');
  109. else if (c == QLatin1Char('A'))
  110. expected = QLatin1Char('P');
  111. else if (c.toLower() == QLatin1Char('p'))
  112. expected = null;
  113. else
  114. expected = c;
  115. if (c != quote)
  116. token += c;
  117. } else
  118. result += c;
  119. }
  120. }
  121. result += formatToken(token, am_pm);
  122. return result;
  123. }
  124. QString DateTime::formatToken(const QString &rToken, bool am_pm) const
  125. {
  126. if (rToken.isEmpty())
  127. return QString();
  128. const QChar c = rToken.at(0);
  129. QString result;
  130. int used;
  131. // Qt data format strings
  132. if (rToken.startsWith(QLatin1String("dddd")))
  133. {
  134. result = QDate::longDayName(date().dayOfWeek());
  135. used = 4;
  136. }
  137. else if (rToken.startsWith(QLatin1String("ddd")))
  138. {
  139. result = QDate::shortDayName(date().dayOfWeek());
  140. used = 3;
  141. }
  142. else if (rToken.startsWith(QLatin1String("dd")))
  143. {
  144. result = QString::number(date().day()).rightJustified(2, QLatin1Char('0'), true);
  145. used = 2;
  146. }
  147. else if (c == QLatin1Char('d'))
  148. {
  149. result = QString::number(date().day());
  150. used = 1;
  151. }
  152. else if (rToken.startsWith(QLatin1String("MMMM")))
  153. {
  154. result = QDate::longMonthName(date().month());
  155. used = 4;
  156. }
  157. else if (rToken.startsWith(QLatin1String("MMM")))
  158. {
  159. result = QDate::shortMonthName(date().month());
  160. used = 3;
  161. }
  162. else if (rToken.startsWith(QLatin1String("MM")))
  163. {
  164. result = QString::number(date().month()).rightJustified(2, QLatin1Char('0'), true);
  165. used = 2;
  166. }
  167. else if (c == QLatin1Char('M'))
  168. {
  169. result = QString::number(date().month());
  170. used = 1;
  171. }
  172. else if (rToken.startsWith(QLatin1String("yyyy")))
  173. {
  174. result = QString::number(date().year());
  175. used = 4;
  176. }
  177. else if (rToken.startsWith(QLatin1String("yy")))
  178. {
  179. result = QString::number(date().year() % 100).rightJustified(2, QLatin1Char('0'), true);
  180. used = 2;
  181. }
  182. // Qt time format strings
  183. else if (rToken.startsWith(QLatin1String("hh")) || rToken.startsWith(QLatin1String("HH")))
  184. {
  185. int hour = time().hour();
  186. if (am_pm && c == QLatin1Char('h') && hour > 12)
  187. hour -= 12;
  188. result = QString::number(hour).rightJustified(2, QLatin1Char('0'), true);
  189. used = 2;
  190. }
  191. else if (c == QLatin1Char('h') || c == QLatin1Char('H'))
  192. {
  193. int hour = time().hour();
  194. if (am_pm && c == QLatin1Char('h') && hour > 12)
  195. hour -= 12;
  196. result = QString::number(hour);
  197. used = 2;
  198. }
  199. else if (rToken.startsWith(QLatin1String("mm")))
  200. {
  201. result = QString::number(time().minute()).rightJustified(2, QLatin1Char('0'), true);
  202. used = 2;
  203. }
  204. else if (c == (QLatin1Char('m')))
  205. {
  206. result = QString::number(time().minute());
  207. used = 1;
  208. }
  209. else if (rToken.startsWith(QLatin1String("ss")))
  210. {
  211. result = QString::number(time().second()).rightJustified(2, QLatin1Char('0'), true);
  212. used = 2;
  213. }
  214. else if (c == QLatin1Char('s'))
  215. {
  216. result = QString::number(time().second());
  217. used = 1;
  218. }
  219. else if (rToken.startsWith(QLatin1String("zzz")))
  220. {
  221. result = QString::number(time().msec()).rightJustified(3, QLatin1Char('0'), true);
  222. used = 3;
  223. }
  224. else if (c == QLatin1Char('z'))
  225. {
  226. result = QString::number(time().msec());
  227. used = 1;
  228. }
  229. else if (c.toLower() == QLatin1Char('a'))
  230. {
  231. bool is_lower = c == QLatin1Char('a');
  232. if (time().hour() < 12)
  233. result = QLatin1String("AM");
  234. else
  235. result = QLatin1String("PM");
  236. if (is_lower)
  237. result = result.toLower();
  238. if (rToken.size() > 1 &&
  239. ((is_lower && rToken.at(1) == QLatin1Char('p')) ||
  240. (!is_lower && rToken.at(1) == QLatin1Char('P')))
  241. )
  242. used = 2;
  243. else
  244. used = 1;
  245. }
  246. // Extension for week number
  247. else if (rToken.startsWith(QLatin1String("ww")))
  248. {
  249. result = QString::number(date().weekNumber()).rightJustified(2, QLatin1Char('0'), true);
  250. used = 2;
  251. }
  252. else if (c == QLatin1Char('w'))
  253. {
  254. result = QString::number(date().weekNumber());
  255. used = 1;
  256. }
  257. if (used)
  258. return result + formatToken(rToken.mid(used), am_pm);
  259. else
  260. return result;
  261. }
  262. bool DateTime::hasAMPM(const QString &rToken)
  263. {
  264. bool in_literal = false;
  265. QChar c;
  266. int i;
  267. for (i = 0; i < rToken.length(); i++)
  268. {
  269. c = rToken.at(i);
  270. if (c == QLatin1Char('\''))
  271. in_literal = !in_literal;
  272. else if (!in_literal && c.toLower() == QLatin1Char('a'))
  273. return true;
  274. }
  275. return false;
  276. }
  277. /**************************************************************************
  278. * Implementation: Operators, Helper
  279. **************************************************************************/
  280. } // namespace Log4Qt