1 | // |
2 | // DateTimeFormat.cpp |
3 | // |
4 | // Library: Foundation |
5 | // Package: DateTime |
6 | // Module: DateTimeFormat |
7 | // |
8 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
9 | // and Contributors. |
10 | // |
11 | // SPDX-License-Identifier: BSL-1.0 |
12 | // |
13 | |
14 | |
15 | #include <Poco/Exception.h> |
16 | #include "Poco/DateTimeFormat.h" |
17 | #include "Poco/RegularExpression.h" |
18 | |
19 | |
20 | namespace Poco { |
21 | |
22 | |
23 | const std::string DateTimeFormat::ISO8601_FORMAT("%Y-%m-%dT%H:%M:%S%z" ); |
24 | const std::string DateTimeFormat::ISO8601_FRAC_FORMAT("%Y-%m-%dT%H:%M:%s%z" ); |
25 | const std::string DateTimeFormat::ISO8601_REGEX("([\\+-]?\\d{4}(?!\\d{2}\\b))" |
26 | "((-?)" |
27 | "((0[1-9]|1[0-2])(\\3([12]\\d|0[1-9]|3[01]))?|W([0-4]\\d|5[0-2])(-?[1-7])?|" |
28 | "(00[1-9]|0[1-9]\\d|[12]\\d{2}|3([0-5]\\d|6[1-6])))" |
29 | "([T\\s]" |
30 | "((([01]\\d|2[0-3])((:?)[0-5]\\d)?|24\\:?00)([\\.,]\\d+(?!:))?)?" |
31 | "(\\17[0-5]\\d([\\.,]\\d+)?)?([A-I]|[K-Z]|([\\+-])([01]\\d|2[0-3]):?([0-5]\\d)?)?)?)?" ); |
32 | |
33 | const std::string DateTimeFormat::RFC822_FORMAT("%w, %e %b %y %H:%M:%S %Z" ); |
34 | const std::string DateTimeFormat::RFC822_REGEX("(((Mon)|(Tue)|(Wed)|(Thu)|(Fri)|(Sat)|(Sun)), *)?" |
35 | "\\d\\d? +" |
36 | "((Jan)|(Feb)|(Mar)|(Apr)|(May)|(Jun)|(Jul)|(Aug)|(Sep)|(Oct)|(Nov)|(Dec)) +" |
37 | "\\d\\d(\\d\\d)? +" |
38 | "\\d\\d:\\d\\d(:\\d\\d)? +" |
39 | "(([+\\-]?\\d\\d\\d\\d)|(UT)|(GMT)|(EST)|(EDT)|(CST)|(CDT)|(MST)|(MDT)|(PST)|(PDT)|\\w)" ); |
40 | |
41 | const std::string DateTimeFormat::RFC1123_FORMAT("%w, %e %b %Y %H:%M:%S %Z" ); |
42 | const std::string DateTimeFormat::RFC1123_REGEX(DateTimeFormat::RFC822_REGEX); |
43 | |
44 | const std::string DateTimeFormat::HTTP_FORMAT("%w, %d %b %Y %H:%M:%S %Z" ); |
45 | const std::string DateTimeFormat::HTTP_REGEX("(((Mon)|(Tue)|(Wed)|(Thu)|(Fri)|(Sat)|(Sun)), *)?" |
46 | "\\d\\d? +" |
47 | "((Jan)|(Feb)|(Mar)|(Apr)|(May)|(Jun)|(Jul)|(Aug)|(Sep)|(Oct)|(Nov)|(Dec)) +" |
48 | "\\d\\d(\\d\\d)? +\\d\\d:\\d\\d(:\\d\\d)? " |
49 | "((UT)|(GMT)|(EST)|(EDT)|(CST)|(CDT)|(MST)|(MDT)|(PST)|(PDT)|)?+" |
50 | "(([+\\-]?\\d\\d\\d\\d)?|(UT)|(GMT)|(EST)|(EDT)|(CST)|(CDT)|(MST)|(MDT)|(PST)|(PDT)|\\w)" ); |
51 | |
52 | const std::string DateTimeFormat::RFC850_FORMAT("%W, %e-%b-%y %H:%M:%S %Z" ); |
53 | const std::string DateTimeFormat::RFC850_REGEX( |
54 | "(((Monday)|(Tuesday)|(Wednesday)|(Thursday)|(Friday)|(Saturday)|(Sunday)|" |
55 | "(Mon)|(Tue)|(Wed)|(Thu)|(Fri)|(Sat)|(Sun)), *)?" |
56 | "\\d\\d?-((Jan)|(Feb)|(Mar)|(Apr)|(May)|(Jun)|(Jul)|(Aug)|(Sep)|(Oct)|(Nov)|(Dec))-" |
57 | "\\d\\d(\\d\\d)? +\\d\\d:\\d\\d(:\\d\\d)? " |
58 | "((UT)|(GMT)|(EST)|(EDT)|(CST)|(CDT)|(MST)|(MDT)|(PST)|(PDT)|)?+" |
59 | "(([+\\-]?\\d\\d\\d\\d)?|(UT)|(GMT)|(EST)|(EDT)|(CST)|(CDT)|(MST)|(MDT)|(PST)|(PDT)|\\w)" ); |
60 | |
61 | const std::string DateTimeFormat::RFC1036_FORMAT("%W, %e %b %y %H:%M:%S %Z" ); |
62 | const std::string DateTimeFormat::RFC1036_REGEX( |
63 | "(((Monday)|(Tuesday)|(Wednesday)|(Thursday)|(Friday)|(Saturday)|(Sunday)), *)?" |
64 | "\\d\\d? +" |
65 | "((Jan)|(Feb)|(Mar)|(Apr)|(May)|(Jun)|(Jul)|(Aug)|(Sep)|(Oct)|(Nov)|(Dec)) +" |
66 | "\\d\\d(\\d\\d)? +\\d\\d:\\d\\d(:\\d\\d)? " |
67 | "((UT)|(GMT)|(EST)|(EDT)|(CST)|(CDT)|(MST)|(MDT)|(PST)|(PDT)|)?+" |
68 | "(([+\\-]?\\d\\d\\d\\d)?|(UT)|(GMT)|(EST)|(EDT)|(CST)|(CDT)|(MST)|(MDT)|(PST)|(PDT)|\\w)" ); |
69 | |
70 | const std::string DateTimeFormat::ASCTIME_FORMAT("%w %b %f %H:%M:%S %Y" ); |
71 | const std::string DateTimeFormat::ASCTIME_REGEX("((Mon)|(Tue)|(Wed)|(Thu)|(Fri)|(Sat)|(Sun)) +" |
72 | "((Jan)|(Feb)|(Mar)|(Apr)|(May)|(Jun)|(Jul)|(Aug)|(Sep)|(Oct)|(Nov)|(Dec)) +" |
73 | "\\d\\d? +\\d\\d:\\d\\d:\\d\\d +(\\d\\d\\d\\d)" ); |
74 | |
75 | const std::string DateTimeFormat::SORTABLE_FORMAT("%Y-%m-%d %H:%M:%S" ); |
76 | const std::string DateTimeFormat::SORTABLE_REGEX("(\\d\\d\\d\\d-\\d\\d-\\d\\d \\d\\d:\\d\\d:\\d\\d)" ); |
77 | |
78 | |
79 | DateTimeFormat::Formatlist DateTimeFormat::FORMAT_LIST( |
80 | { |
81 | DateTimeFormat::ISO8601_FORMAT, |
82 | DateTimeFormat::ISO8601_FRAC_FORMAT, |
83 | DateTimeFormat::RFC822_FORMAT, |
84 | DateTimeFormat::RFC1123_FORMAT, |
85 | DateTimeFormat::HTTP_FORMAT, |
86 | DateTimeFormat::RFC850_FORMAT, |
87 | DateTimeFormat::RFC1036_FORMAT, |
88 | DateTimeFormat::ASCTIME_FORMAT, |
89 | DateTimeFormat::SORTABLE_FORMAT |
90 | }); |
91 | |
92 | |
93 | const std::string DateTimeFormat::WEEKDAY_NAMES[] = |
94 | { |
95 | "Sunday" , |
96 | "Monday" , |
97 | "Tuesday" , |
98 | "Wednesday" , |
99 | "Thursday" , |
100 | "Friday" , |
101 | "Saturday" |
102 | }; |
103 | |
104 | |
105 | const std::string DateTimeFormat::MONTH_NAMES[] = |
106 | { |
107 | "January" , |
108 | "February" , |
109 | "March" , |
110 | "April" , |
111 | "May" , |
112 | "June" , |
113 | "July" , |
114 | "August" , |
115 | "September" , |
116 | "October" , |
117 | "November" , |
118 | "December" |
119 | }; |
120 | |
121 | |
122 | DateTimeFormat::RegexList DateTimeFormat::REGEX_LIST = |
123 | { |
124 | &DateTimeFormat::ISO8601_REGEX, |
125 | &DateTimeFormat::RFC822_REGEX, |
126 | &DateTimeFormat::RFC1123_REGEX, |
127 | &DateTimeFormat::HTTP_REGEX, |
128 | &DateTimeFormat::RFC850_REGEX, |
129 | &DateTimeFormat::RFC1036_REGEX, |
130 | &DateTimeFormat::ASCTIME_REGEX, |
131 | &DateTimeFormat::SORTABLE_REGEX |
132 | }; |
133 | |
134 | |
135 | bool DateTimeFormat::hasFormat(const std::string& fmt) |
136 | { |
137 | return FORMAT_LIST.find(fmt) != FORMAT_LIST.end(); |
138 | } |
139 | |
140 | |
141 | bool DateTimeFormat::isValid(const std::string& dateTime) |
142 | { |
143 | for (const auto& f : REGEX_LIST) |
144 | { |
145 | if (RegularExpression(*f).match(dateTime)) return true; |
146 | } |
147 | return false; |
148 | } |
149 | |
150 | |
151 | } // namespace Poco |
152 | |