DateTimeParser.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. //
  2. // DateTimeParser.h
  3. //
  4. // Library: Foundation
  5. // Package: DateTime
  6. // Module: DateTimeParser
  7. //
  8. // Definition of the DateTimeParser 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_DateTimeParser_INCLUDED
  16. #define Foundation_DateTimeParser_INCLUDED
  17. #include "Poco/Foundation.h"
  18. #include "Poco/DateTime.h"
  19. namespace Poco {
  20. class Foundation_API DateTimeParser
  21. /// This class provides a method for parsing dates and times
  22. /// from strings. All parsing methods do their best to
  23. /// parse a meaningful result, even from malformed input
  24. /// strings.
  25. ///
  26. /// The returned DateTime will always contain a time in the same
  27. /// timezone as the time in the string. Call DateTime::makeUTC()
  28. /// with the timeZoneDifferential returned by parse() to convert
  29. /// the DateTime to UTC.
  30. ///
  31. /// Note: When parsing a time in 12-hour (AM/PM) format, the hour
  32. /// (%h) must be parsed before the AM/PM designator (%a, %A),
  33. /// otherwise the AM/PM designator will be ignored.
  34. ///
  35. /// See the DateTimeFormatter class for a list of supported format specifiers.
  36. /// In addition to the format specifiers supported by DateTimeFormatter, an
  37. /// additional specifier is supported: %r will parse a year given by either
  38. /// two or four digits. Years 69-00 are interpreted in the 20th century
  39. /// (1969-2000), years 01-68 in the 21th century (2001-2068).
  40. ///
  41. /// Note that in the current implementation all characters other than format specifiers in
  42. /// the format string are ignored/not matched against the date/time string. This may
  43. /// lead to non-error results even with nonsense input strings.
  44. /// This may change in a future version to a more strict behavior.
  45. /// If more strict format validation of date/time strings is required, a regular
  46. /// expression could be used for initial validation, before passing the string
  47. /// to DateTimeParser.
  48. {
  49. public:
  50. static void parse(const std::string& fmt, const std::string& str, DateTime& dateTime, int& timeZoneDifferential);
  51. /// Parses a date and time in the given format from the given string.
  52. /// Throws a SyntaxException if the string cannot be successfully parsed.
  53. /// Please see DateTimeFormatter::format() for a description of the format string.
  54. /// Class DateTimeFormat defines format strings for various standard date/time formats.
  55. static DateTime parse(const std::string& fmt, const std::string& str, int& timeZoneDifferential);
  56. /// Parses a date and time in the given format from the given string.
  57. /// Throws a SyntaxException if the string cannot be successfully parsed.
  58. /// Please see DateTimeFormatter::format() for a description of the format string.
  59. /// Class DateTimeFormat defines format strings for various standard date/time formats.
  60. static bool tryParse(const std::string& fmt, const std::string& str, DateTime& dateTime, int& timeZoneDifferential);
  61. /// Parses a date and time in the given format from the given string.
  62. /// Returns true if the string has been successfully parsed, false otherwise.
  63. /// Please see DateTimeFormatter::format() for a description of the format string.
  64. /// Class DateTimeFormat defines format strings for various standard date/time formats.
  65. static void parse(const std::string& str, DateTime& dateTime, int& timeZoneDifferential);
  66. /// Parses a date and time from the given dateTime string. Before parsing, the method
  67. /// examines the dateTime string for a known date/time format.
  68. /// Throws a SyntaxException if the string cannot be successfully parsed.
  69. /// Please see DateTimeFormatter::format() for a description of the format string.
  70. /// Class DateTimeFormat defines format strings for various standard date/time formats.
  71. static DateTime parse(const std::string& str, int& timeZoneDifferential);
  72. /// Parses a date and time from the given dateTime string. Before parsing, the method
  73. /// examines the dateTime string for a known date/time format.
  74. /// Please see DateTimeFormatter::format() for a description of the format string.
  75. /// Class DateTimeFormat defines format strings for various standard date/time formats.
  76. static bool tryParse(const std::string& str, DateTime& dateTime, int& timeZoneDifferential);
  77. /// Parses a date and time from the given dateTime string. Before parsing, the method
  78. /// examines the dateTime string for a known date/time format.
  79. /// Please see DateTimeFormatter::format() for a description of the format string.
  80. /// Class DateTimeFormat defines format strings for various standard date/time formats.
  81. static int parseMonth(std::string::const_iterator& it, const std::string::const_iterator& end);
  82. /// Tries to interpret the given range as a month name. The range must be at least
  83. /// three characters long.
  84. /// Returns the month number (1 .. 12) if the month name is valid. Otherwise throws
  85. /// a SyntaxException.
  86. static int parseDayOfWeek(std::string::const_iterator& it, const std::string::const_iterator& end);
  87. /// Tries to interpret the given range as a weekday name. The range must be at least
  88. /// three characters long.
  89. /// Returns the weekday number (0 .. 6, where 0 = Synday, 1 = Monday, etc.) if the
  90. /// weekday name is valid. Otherwise throws a SyntaxException.
  91. protected:
  92. static int parseTZD(std::string::const_iterator& it, const std::string::const_iterator& end);
  93. static int parseAMPM(std::string::const_iterator& it, const std::string::const_iterator& end, int hour);
  94. };
  95. } // namespace Poco
  96. #endif // Foundation_DateTimeParser_INCLUDED