1 | // |
---|
2 | // DateTimeParser.h |
---|
3 | // |
---|
4 | // $Id: //poco/1.3/Foundation/include/Poco/DateTimeParser.h#3 $ |
---|
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 | // Permission is hereby granted, free of charge, to any person or organization |
---|
16 | // obtaining a copy of the software and accompanying documentation covered by |
---|
17 | // this license (the "Software") to use, reproduce, display, distribute, |
---|
18 | // execute, and transmit the Software, and to prepare derivative works of the |
---|
19 | // Software, and to permit third-parties to whom the Software is furnished to |
---|
20 | // do so, all subject to the following: |
---|
21 | // |
---|
22 | // The copyright notices in the Software and this entire statement, including |
---|
23 | // the above license grant, this restriction and the following disclaimer, |
---|
24 | // must be included in all copies of the Software, in whole or in part, and |
---|
25 | // all derivative works of the Software, unless such copies or derivative |
---|
26 | // works are solely in the form of machine-executable object code generated by |
---|
27 | // a source language processor. |
---|
28 | // |
---|
29 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
30 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
31 | // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT |
---|
32 | // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE |
---|
33 | // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, |
---|
34 | // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
35 | // DEALINGS IN THE SOFTWARE. |
---|
36 | // |
---|
37 | |
---|
38 | |
---|
39 | #ifndef Foundation_DateTimeParser_INCLUDED |
---|
40 | #define Foundation_DateTimeParser_INCLUDED |
---|
41 | |
---|
42 | |
---|
43 | #include "Poco/Foundation.h" |
---|
44 | #include "Poco/DateTime.h" |
---|
45 | |
---|
46 | |
---|
47 | namespace Poco { |
---|
48 | |
---|
49 | |
---|
50 | class Foundation_API DateTimeParser |
---|
51 | /// This class provides a method for parsing dates and times |
---|
52 | /// from strings. All parsing methods do their best to |
---|
53 | /// parse a meaningful result, even from malformed input |
---|
54 | /// strings. |
---|
55 | /// |
---|
56 | /// The returned DateTime will always contain a time in the same |
---|
57 | /// timezone as the time in the string. Call DateTime::makeUTC() |
---|
58 | /// with the timeZoneDifferential returned by parse() to convert |
---|
59 | /// the DateTime to UTC. |
---|
60 | /// |
---|
61 | /// Note: When parsing a time in 12-hour (AM/PM) format, the hour |
---|
62 | /// (%h) must be parsed before the AM/PM designator (%a, %A), |
---|
63 | /// otherwise the AM/PM designator will be ignored. |
---|
64 | /// |
---|
65 | /// See the DateTimeFormatter class for a list of supported format specifiers. |
---|
66 | /// In addition to the format specifiers supported by DateTimeFormatter, an |
---|
67 | /// additional specifier is supported: %r will parse a year given by either |
---|
68 | /// two or four digits. Years 69-00 are interpreted in the 20th century |
---|
69 | /// (1969-2000), years 01-68 in the 21th century (2001-2068). |
---|
70 | { |
---|
71 | public: |
---|
72 | static void parse(const std::string& fmt, const std::string& str, DateTime& dateTime, int& timeZoneDifferential); |
---|
73 | /// Parses a date and time in the given format from the given string. |
---|
74 | /// Throws a SyntaxException if the string cannot be successfully parsed. |
---|
75 | /// Please see DateTimeFormatter::format() for a description of the format string. |
---|
76 | /// Class DateTimeFormat defines format strings for various standard date/time formats. |
---|
77 | |
---|
78 | static DateTime parse(const std::string& fmt, const std::string& str, int& timeZoneDifferential); |
---|
79 | /// Parses a date and time in the given format from the given string. |
---|
80 | /// Throws a SyntaxException if the string cannot be successfully parsed. |
---|
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 | |
---|
84 | static bool tryParse(const std::string& fmt, const std::string& str, DateTime& dateTime, int& timeZoneDifferential); |
---|
85 | /// Parses a date and time in the given format from the given string. |
---|
86 | /// Returns true if the string has been successfully parsed, false otherwise. |
---|
87 | /// Please see DateTimeFormatter::format() for a description of the format string. |
---|
88 | /// Class DateTimeFormat defines format strings for various standard date/time formats. |
---|
89 | |
---|
90 | static void parse(const std::string& str, DateTime& dateTime, int& timeZoneDifferential); |
---|
91 | /// Parses a date and time from the given dateTime string. Before parsing, the method |
---|
92 | /// examines the dateTime string for a known date/time format. |
---|
93 | /// Throws a SyntaxException if the string cannot be successfully parsed. |
---|
94 | /// Please see DateTimeFormatter::format() for a description of the format string. |
---|
95 | /// Class DateTimeFormat defines format strings for various standard date/time formats. |
---|
96 | |
---|
97 | static DateTime parse(const std::string& str, int& timeZoneDifferential); |
---|
98 | /// Parses a date and time from the given dateTime string. Before parsing, the method |
---|
99 | /// examines the dateTime string for a known date/time format. |
---|
100 | /// Please see DateTimeFormatter::format() for a description of the format string. |
---|
101 | /// Class DateTimeFormat defines format strings for various standard date/time formats. |
---|
102 | |
---|
103 | static bool tryParse(const std::string& str, DateTime& dateTime, int& timeZoneDifferential); |
---|
104 | /// Parses a date and time from the given dateTime string. Before parsing, the method |
---|
105 | /// examines the dateTime string for a known date/time format. |
---|
106 | /// Please see DateTimeFormatter::format() for a description of the format string. |
---|
107 | /// Class DateTimeFormat defines format strings for various standard date/time formats. |
---|
108 | |
---|
109 | static int parseMonth(std::string::const_iterator& it, const std::string::const_iterator& end); |
---|
110 | /// Tries to interpret the given range as a month name. The range must be at least |
---|
111 | /// three characters long. |
---|
112 | /// Returns the month number (1 .. 12) if the month name is valid. Otherwise throws |
---|
113 | /// a SyntaxException. |
---|
114 | |
---|
115 | static int parseDayOfWeek(std::string::const_iterator& it, const std::string::const_iterator& end); |
---|
116 | /// Tries to interpret the given range as a weekday name. The range must be at least |
---|
117 | /// three characters long. |
---|
118 | /// Returns the weekday number (0 .. 6, where 0 = Synday, 1 = Monday, etc.) if the |
---|
119 | /// weekday name is valid. Otherwise throws a SyntaxException. |
---|
120 | |
---|
121 | protected: |
---|
122 | static int parseTZD(std::string::const_iterator& it, const std::string::const_iterator& end); |
---|
123 | static int parseAMPM(std::string::const_iterator& it, const std::string::const_iterator& end, int hour); |
---|
124 | }; |
---|
125 | |
---|
126 | |
---|
127 | } // namespace Poco |
---|
128 | |
---|
129 | |
---|
130 | #endif // Foundation_DateTimeParser_INCLUDED |
---|