DateTimeFormatter.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. //
  2. // DateTimeFormatter.h
  3. //
  4. // $Id: //poco/1.4/Foundation/include/Poco/DateTimeFormatter.h#2 $
  5. //
  6. // Library: Foundation
  7. // Package: DateTime
  8. // Module: DateTimeFormatter
  9. //
  10. // Definition of the DateTimeFormatter class.
  11. //
  12. // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
  13. // and Contributors.
  14. //
  15. // SPDX-License-Identifier: BSL-1.0
  16. //
  17. #ifndef Foundation_DateTimeFormatter_INCLUDED
  18. #define Foundation_DateTimeFormatter_INCLUDED
  19. #include "Poco/Foundation.h"
  20. #include "Poco/DateTime.h"
  21. #include "Poco/LocalDateTime.h"
  22. namespace Poco {
  23. class Timestamp;
  24. class Timespan;
  25. class Foundation_API DateTimeFormatter
  26. /// This class converts dates and times into strings, supporting a
  27. /// variety of standard and custom formats.
  28. ///
  29. /// There are two kind of static member functions:
  30. /// * format* functions return a std::string containing
  31. /// the formatted value.
  32. /// * append* functions append the formatted value to
  33. /// an existing string.
  34. {
  35. public:
  36. enum
  37. {
  38. UTC = 0xFFFF /// Special value for timeZoneDifferential denoting UTC.
  39. };
  40. static std::string format(const Timestamp& timestamp, const std::string& fmt, int timeZoneDifferential = UTC);
  41. /// Formats the given timestamp according to the given format.
  42. /// The format string is used as a template to format the date and
  43. /// is copied character by character except for the following special characters,
  44. /// which are replaced by the corresponding value.
  45. ///
  46. /// * %w - abbreviated weekday (Mon, Tue, ...)
  47. /// * %W - full weekday (Monday, Tuesday, ...)
  48. /// * %b - abbreviated month (Jan, Feb, ...)
  49. /// * %B - full month (January, February, ...)
  50. /// * %d - zero-padded day of month (01 .. 31)
  51. /// * %e - day of month (1 .. 31)
  52. /// * %f - space-padded day of month ( 1 .. 31)
  53. /// * %m - zero-padded month (01 .. 12)
  54. /// * %n - month (1 .. 12)
  55. /// * %o - space-padded month ( 1 .. 12)
  56. /// * %y - year without century (70)
  57. /// * %Y - year with century (1970)
  58. /// * %H - hour (00 .. 23)
  59. /// * %h - hour (00 .. 12)
  60. /// * %a - am/pm
  61. /// * %A - AM/PM
  62. /// * %M - minute (00 .. 59)
  63. /// * %S - second (00 .. 59)
  64. /// * %s - seconds and microseconds (equivalent to %S.%F)
  65. /// * %i - millisecond (000 .. 999)
  66. /// * %c - centisecond (0 .. 9)
  67. /// * %F - fractional seconds/microseconds (000000 - 999999)
  68. /// * %z - time zone differential in ISO 8601 format (Z or +NN.NN)
  69. /// * %Z - time zone differential in RFC format (GMT or +NNNN)
  70. /// * %% - percent sign
  71. ///
  72. /// Class DateTimeFormat defines format strings for various standard date/time formats.
  73. static std::string format(const DateTime& dateTime, const std::string& fmt, int timeZoneDifferential = UTC);
  74. /// Formats the given date and time according to the given format.
  75. /// See format(const Timestamp&, const std::string&, int) for more information.
  76. static std::string format(const LocalDateTime& dateTime, const std::string& fmt);
  77. /// Formats the given local date and time according to the given format.
  78. /// See format(const Timestamp&, const std::string&, int) for more information.
  79. static std::string format(const Timespan& timespan, const std::string& fmt = "%dd %H:%M:%S.%i");
  80. /// Formats the given timespan according to the given format.
  81. /// The format string is used as a template to format the date and
  82. /// is copied character by character except for the following special characters,
  83. /// which are replaced by the corresponding value.
  84. ///
  85. /// * %d - days
  86. /// * %H - hours (00 .. 23)
  87. /// * %h - total hours (0 .. n)
  88. /// * %M - minutes (00 .. 59)
  89. /// * %m - total minutes (0 .. n)
  90. /// * %S - seconds (00 .. 59)
  91. /// * %s - total seconds (0 .. n)
  92. /// * %i - milliseconds (000 .. 999)
  93. /// * %c - centisecond (0 .. 9)
  94. /// * %F - fractional seconds/microseconds (000000 - 999999)
  95. /// * %% - percent sign
  96. static void append(std::string& str, const Timestamp& timestamp, const std::string& fmt, int timeZoneDifferential = UTC);
  97. /// Formats the given timestamp according to the given format and appends it to str.
  98. ///
  99. /// See format() for documentation of the formatting string.
  100. static void append(std::string& str, const DateTime& dateTime, const std::string& fmt, int timeZoneDifferential = UTC);
  101. /// Formats the given date and time according to the given format and appends it to str.
  102. ///
  103. /// See format() for documentation of the formatting string.
  104. static void append(std::string& str, const LocalDateTime& dateTime, const std::string& fmt);
  105. /// Formats the given local date and time according to the given format and appends it to str.
  106. ///
  107. /// See format() for documentation of the formatting string.
  108. static void append(std::string& str, const Timespan& timespan, const std::string& fmt = "%dd %H:%M:%S.%i");
  109. /// Formats the given timespan according to the given format and appends it to str.
  110. ///
  111. /// See format() for documentation of the formatting string.
  112. static std::string tzdISO(int timeZoneDifferential);
  113. /// Formats the given timezone differential in ISO format.
  114. /// If timeZoneDifferential is UTC, "Z" is returned,
  115. /// otherwise, +HH.MM (or -HH.MM) is returned.
  116. static std::string tzdRFC(int timeZoneDifferential);
  117. /// Formats the given timezone differential in RFC format.
  118. /// If timeZoneDifferential is UTC, "GMT" is returned,
  119. /// otherwise ++HHMM (or -HHMM) is returned.
  120. static void tzdISO(std::string& str, int timeZoneDifferential);
  121. /// Formats the given timezone differential in ISO format
  122. /// and appends it to the given string.
  123. /// If timeZoneDifferential is UTC, "Z" is returned,
  124. /// otherwise, +HH.MM (or -HH.MM) is returned.
  125. static void tzdRFC(std::string& str, int timeZoneDifferential);
  126. /// Formats the given timezone differential in RFC format
  127. /// and appends it to the given string.
  128. /// If timeZoneDifferential is UTC, "GMT" is returned,
  129. /// otherwise ++HHMM (or -HHMM) is returned.
  130. };
  131. //
  132. // inlines
  133. //
  134. inline std::string DateTimeFormatter::format(const Timestamp& timestamp, const std::string& fmt, int timeZoneDifferential)
  135. {
  136. DateTime dateTime(timestamp);
  137. return format(dateTime, fmt, timeZoneDifferential);
  138. }
  139. inline std::string DateTimeFormatter::format(const DateTime& dateTime, const std::string& fmt, int timeZoneDifferential)
  140. {
  141. std::string result;
  142. result.reserve(64);
  143. append(result, dateTime, fmt, timeZoneDifferential);
  144. return result;
  145. }
  146. inline std::string DateTimeFormatter::format(const LocalDateTime& dateTime, const std::string& fmt)
  147. {
  148. return format(dateTime._dateTime, fmt, dateTime._tzd);
  149. }
  150. inline std::string DateTimeFormatter::format(const Timespan& timespan, const std::string& fmt)
  151. {
  152. std::string result;
  153. result.reserve(32);
  154. append(result, timespan, fmt);
  155. return result;
  156. }
  157. inline void DateTimeFormatter::append(std::string& str, const Timestamp& timestamp, const std::string& fmt, int timeZoneDifferential)
  158. {
  159. DateTime dateTime(timestamp);
  160. append(str, dateTime, fmt, timeZoneDifferential);
  161. }
  162. inline std::string DateTimeFormatter::tzdISO(int timeZoneDifferential)
  163. {
  164. std::string result;
  165. result.reserve(8);
  166. tzdISO(result, timeZoneDifferential);
  167. return result;
  168. }
  169. inline std::string DateTimeFormatter::tzdRFC(int timeZoneDifferential)
  170. {
  171. std::string result;
  172. result.reserve(8);
  173. tzdRFC(result, timeZoneDifferential);
  174. return result;
  175. }
  176. } // namespace Poco
  177. #endif // Foundation_DateTimeFormatter_INCLUDED