DateTimeFormatter.h 7.2 KB

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