DateTimeParser.h 5.5 KB

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