datetime.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /******************************************************************************
  2. *
  3. * package: Log4Qt
  4. * file: datetime.h
  5. * created: September 2007
  6. * author: Martin Heinrich
  7. *
  8. *
  9. * changes: Sep 2008, Martin Heinrich:
  10. * - Resolved compilation problem with Microsoft Visual Studio 2005
  11. *
  12. *
  13. * Copyright 2007 - 2008 Martin Heinrich
  14. *
  15. * Licensed under the Apache License, Version 2.0 (the "License");
  16. * you may not use this file except in compliance with the License.
  17. * You may obtain a copy of the License at
  18. *
  19. * http://www.apache.org/licenses/LICENSE-2.0
  20. *
  21. * Unless required by applicable law or agreed to in writing, software
  22. * distributed under the License is distributed on an "AS IS" BASIS,
  23. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  24. * See the License for the specific language governing permissions and
  25. * limitations under the License.
  26. *
  27. ******************************************************************************/
  28. #ifndef LOG4QT_HELPERS_DATETIME_H
  29. #define LOG4QT_HELPERS_DATETIME_H
  30. /******************************************************************************
  31. * Dependencies
  32. ******************************************************************************/
  33. #include <QtCore/QDateTime>
  34. /******************************************************************************
  35. * Declarations
  36. ******************************************************************************/
  37. namespace Log4Qt
  38. {
  39. /*!
  40. * \brief The class DateTime provides extended functionality for QDateTime.
  41. *
  42. * The class DateTime implements additional formatting options for
  43. * toString() and provides conversion functions from and to milliseconds.
  44. */
  45. class DateTime : public QDateTime
  46. {
  47. public:
  48. /*!
  49. * Constructs a null date time.
  50. *
  51. * \sa QDateTime::QDateTime()
  52. */
  53. DateTime();
  54. // DateTime(const DateTime &rOther); // Use compiler default
  55. /*!
  56. * Constructs a copy of another QDateTime.
  57. *
  58. * \sa QDateTime::QDateTime(const QDateTime &rOther)
  59. */
  60. DateTime(const QDateTime &rOther);
  61. /*!
  62. * Constructs a datetime with the given \a rDate and \a rTime, using
  63. * the time specification defined by \a timeSpec.
  64. *
  65. * \sa QDateTime::QDateTime(const QDate &rDate, const QTime &rTime,
  66. * Qt::TimeSpec timeSpec = Qt::LocalTime)
  67. */
  68. DateTime(const QDate &rDate,
  69. const QTime &rTime,
  70. Qt::TimeSpec timeSpec = Qt::LocalTime);
  71. // virtual ~DateTime(); // Use compiler default
  72. /*!
  73. * Assigns \a rOther to this DateTime and returns a reference to it.
  74. */
  75. DateTime &operator=(const DateTime &rOther);
  76. /*!
  77. * Returns the datetime as the number of milliseconds that have passed
  78. * since 1970-01-01T00:00:00,000, Coordinated Universal Time (Qt::UTC).
  79. *
  80. * \sa QDateTime::toTime_t()
  81. */
  82. qint64 toMilliSeconds() const;
  83. /*!
  84. * Returns the datetime as a string. The \a rFormat parameter
  85. * determines the format of the result string.
  86. *
  87. * In addition to the expressions of QDateTime::toString(const QString
  88. * &rFormat) the following expression can be used.
  89. *
  90. * <table align="center" border="1" cellpadding="2" cellspacing="0" bordercolor="#84b0c7">
  91. * <tr bgcolor="#d5e1e8">
  92. * <th width="20%"> Expression </th>
  93. * <th> Output </th>
  94. * </tr><tr>
  95. * <td> w </td>
  96. * <td> the week of the year as number without a leading zero (1 to 53) </td>
  97. * </tr><tr bgcolor="#ffffff">
  98. * <td> ww </td>
  99. * <td> the week of the year as number with a leading zero (01 to 53) </td>
  100. * </tr>
  101. * </table>
  102. *
  103. * Alternatively the \a rFormat parameter can specify one of the
  104. * following strings.
  105. *
  106. * <table align="center" border="1" cellpadding="2" cellspacing="0" bordercolor="#84b0c7">
  107. * <tr bgcolor="#d5e1e8">
  108. * <th width="20%"> String </th>
  109. * <th> Format </th>
  110. * </tr><tr>
  111. * <td> TIME_ABSOLUTE </td>
  112. * <td> uses the format HH:mm:ss.zzz </td>
  113. * </tr><tr bgcolor="#ffffff">
  114. * <td> DATE </td>
  115. * <td> uses the format dd MMM YYYY HH:mm:ss.zzzz </td>
  116. * </tr><tr>
  117. * <td> ISO8601 </td>
  118. * <td> uses the format yyyy-MM-dd hh:mm:ss.zzz </td>
  119. * </tr><tr bgcolor="#ffffff">
  120. * <td> NONE </td>
  121. * <td> uses an empty string as format </td>
  122. * </tr><tr bgcolor="#ffffff">
  123. * <td> TIME_RELATIVE </td>
  124. * <td> returns the milliseconds since start of the program</td>
  125. * </tr>
  126. * </table>
  127. *
  128. * \sa QDateTime::toString(const QString &rFormat)
  129. */
  130. QString toString(const QString &rFormat) const;
  131. /*!
  132. * Returns the current datetime, as reported by the system clock, in
  133. * the local time zone.
  134. *
  135. * \sa QDateTime::currentDateTime()
  136. */
  137. static DateTime currentDateTime();
  138. /*!
  139. * Returns a datetime whose date and time are the number of
  140. * milliseconds that have passed since 1970-01-01T00:00:00,
  141. * Coordinated Universal Time (Qt::UTC).
  142. *
  143. * \sa QDateTime::fromTime_t(uint seconds)
  144. */
  145. static DateTime fromMilliSeconds(qint64 milliSeconds);
  146. private:
  147. QString formatDateTime(const QString &rFormat) const;
  148. QString formatToken(const QString &rToken, bool am_pm) const;
  149. static bool hasAMPM(const QString &rFormat);
  150. };
  151. /**************************************************************************
  152. * Operators, Helper
  153. **************************************************************************/
  154. /**************************************************************************
  155. * Inline
  156. **************************************************************************/
  157. inline DateTime::DateTime() : QDateTime()
  158. {}
  159. inline DateTime::DateTime(const QDateTime &rOther) : QDateTime(rOther)
  160. {}
  161. inline DateTime::DateTime(const QDate &rDate,
  162. const QTime &rTime,
  163. Qt::TimeSpec timeSpec) :
  164. QDateTime(rDate, rTime, timeSpec)
  165. {}
  166. inline DateTime &DateTime::operator=(const DateTime &rOther)
  167. { QDateTime::operator=(rOther); return *this; }
  168. inline qint64 DateTime::toMilliSeconds() const
  169. { return (qint64)1000 * toTime_t() + time().msec(); }
  170. inline DateTime DateTime::currentDateTime()
  171. { return DateTime(QDateTime::currentDateTime()); }
  172. inline DateTime DateTime::fromMilliSeconds(qint64 milliSeconds)
  173. { return DateTime(QDateTime::fromTime_t(milliSeconds / 1000).addMSecs(milliSeconds % 1000)); }
  174. } // namespace Log4Qt
  175. Q_DECLARE_TYPEINFO(Log4Qt::DateTime, Q_MOVABLE_TYPE);
  176. #endif // LOG4QT_HELPERS_DATETIME_H