1 | // |
---|
2 | // DateTimeFormatter.cpp |
---|
3 | // |
---|
4 | // $Id: //poco/1.3/Foundation/src/DateTimeFormatter.cpp#4 $ |
---|
5 | // |
---|
6 | // Library: Foundation |
---|
7 | // Package: DateTime |
---|
8 | // Module: DateTimeFormatter |
---|
9 | // |
---|
10 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
---|
11 | // and Contributors. |
---|
12 | // |
---|
13 | // Permission is hereby granted, free of charge, to any person or organization |
---|
14 | // obtaining a copy of the software and accompanying documentation covered by |
---|
15 | // this license (the "Software") to use, reproduce, display, distribute, |
---|
16 | // execute, and transmit the Software, and to prepare derivative works of the |
---|
17 | // Software, and to permit third-parties to whom the Software is furnished to |
---|
18 | // do so, all subject to the following: |
---|
19 | // |
---|
20 | // The copyright notices in the Software and this entire statement, including |
---|
21 | // the above license grant, this restriction and the following disclaimer, |
---|
22 | // must be included in all copies of the Software, in whole or in part, and |
---|
23 | // all derivative works of the Software, unless such copies or derivative |
---|
24 | // works are solely in the form of machine-executable object code generated by |
---|
25 | // a source language processor. |
---|
26 | // |
---|
27 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
28 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
29 | // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT |
---|
30 | // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE |
---|
31 | // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, |
---|
32 | // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
33 | // DEALINGS IN THE SOFTWARE. |
---|
34 | // |
---|
35 | |
---|
36 | |
---|
37 | #include "Poco/DateTimeFormatter.h" |
---|
38 | #include "Poco/DateTimeFormat.h" |
---|
39 | #include "Poco/Timestamp.h" |
---|
40 | #include "Poco/NumberFormatter.h" |
---|
41 | |
---|
42 | |
---|
43 | namespace Poco { |
---|
44 | |
---|
45 | |
---|
46 | void DateTimeFormatter::append(std::string& str, const DateTime& dateTime, const std::string& fmt, int timeZoneDifferential) |
---|
47 | { |
---|
48 | std::string::const_iterator it = fmt.begin(); |
---|
49 | std::string::const_iterator end = fmt.end(); |
---|
50 | while (it != end) |
---|
51 | { |
---|
52 | if (*it == '%') |
---|
53 | { |
---|
54 | if (++it != end) |
---|
55 | { |
---|
56 | switch (*it) |
---|
57 | { |
---|
58 | case 'w': str.append(DateTimeFormat::WEEKDAY_NAMES[dateTime.dayOfWeek()], 0, 3); break; |
---|
59 | case 'W': str.append(DateTimeFormat::WEEKDAY_NAMES[dateTime.dayOfWeek()]); break; |
---|
60 | case 'b': str.append(DateTimeFormat::MONTH_NAMES[dateTime.month() - 1], 0, 3); break; |
---|
61 | case 'B': str.append(DateTimeFormat::MONTH_NAMES[dateTime.month() - 1]); break; |
---|
62 | case 'd': NumberFormatter::append0(str, dateTime.day(), 2); break; |
---|
63 | case 'e': NumberFormatter::append(str, dateTime.day()); break; |
---|
64 | case 'f': NumberFormatter::append(str, dateTime.day(), 2); break; |
---|
65 | case 'm': NumberFormatter::append0(str, dateTime.month(), 2); break; |
---|
66 | case 'n': NumberFormatter::append(str, dateTime.month()); break; |
---|
67 | case 'o': NumberFormatter::append(str, dateTime.month(), 2); break; |
---|
68 | case 'y': NumberFormatter::append0(str, dateTime.year() % 100, 2); break; |
---|
69 | case 'Y': NumberFormatter::append0(str, dateTime.year(), 4); break; |
---|
70 | case 'H': NumberFormatter::append0(str, dateTime.hour(), 2); break; |
---|
71 | case 'h': NumberFormatter::append0(str, dateTime.hourAMPM(), 2); break; |
---|
72 | case 'a': str.append(dateTime.isAM() ? "am" : "pm"); break; |
---|
73 | case 'A': str.append(dateTime.isAM() ? "AM" : "PM"); break; |
---|
74 | case 'M': NumberFormatter::append0(str, dateTime.minute(), 2); break; |
---|
75 | case 'S': NumberFormatter::append0(str, dateTime.second(), 2); break; |
---|
76 | case 'i': NumberFormatter::append0(str, dateTime.millisecond(), 3); break; |
---|
77 | case 'c': NumberFormatter::append(str, dateTime.millisecond()/100); break; |
---|
78 | case 'F': NumberFormatter::append0(str, dateTime.millisecond()*1000 + dateTime.microsecond(), 6); break; |
---|
79 | case 'z': tzdISO(str, timeZoneDifferential); break; |
---|
80 | case 'Z': tzdRFC(str, timeZoneDifferential); break; |
---|
81 | default: str += *it; |
---|
82 | } |
---|
83 | ++it; |
---|
84 | } |
---|
85 | } |
---|
86 | else str += *it++; |
---|
87 | } |
---|
88 | } |
---|
89 | |
---|
90 | |
---|
91 | void DateTimeFormatter::append(std::string& str, const Timespan& timespan, const std::string& fmt) |
---|
92 | { |
---|
93 | std::string::const_iterator it = fmt.begin(); |
---|
94 | std::string::const_iterator end = fmt.end(); |
---|
95 | while (it != end) |
---|
96 | { |
---|
97 | if (*it == '%') |
---|
98 | { |
---|
99 | if (++it != end) |
---|
100 | { |
---|
101 | switch (*it) |
---|
102 | { |
---|
103 | case 'd': NumberFormatter::append(str, timespan.days()); break; |
---|
104 | case 'H': NumberFormatter::append0(str, timespan.hours(), 2); break; |
---|
105 | case 'h': NumberFormatter::append(str, timespan.totalHours()); break; |
---|
106 | case 'M': NumberFormatter::append0(str, timespan.minutes(), 2); break; |
---|
107 | case 'm': NumberFormatter::append(str, timespan.totalMinutes()); break; |
---|
108 | case 'S': NumberFormatter::append0(str, timespan.seconds(), 2); break; |
---|
109 | case 's': NumberFormatter::append(str, timespan.totalSeconds()); break; |
---|
110 | case 'i': NumberFormatter::append0(str, timespan.milliseconds(), 3); break; |
---|
111 | case 'c': NumberFormatter::append(str, timespan.milliseconds()/100); break; |
---|
112 | case 'F': NumberFormatter::append0(str, timespan.milliseconds()*1000 + timespan.microseconds(), 6); break; |
---|
113 | default: str += *it; |
---|
114 | } |
---|
115 | ++it; |
---|
116 | } |
---|
117 | } |
---|
118 | else str += *it++; |
---|
119 | } |
---|
120 | } |
---|
121 | |
---|
122 | |
---|
123 | void DateTimeFormatter::tzdISO(std::string& str, int timeZoneDifferential) |
---|
124 | { |
---|
125 | if (timeZoneDifferential != UTC) |
---|
126 | { |
---|
127 | if (timeZoneDifferential >= 0) |
---|
128 | { |
---|
129 | str += '+'; |
---|
130 | NumberFormatter::append0(str, timeZoneDifferential/3600, 2); |
---|
131 | str += ':'; |
---|
132 | NumberFormatter::append0(str, (timeZoneDifferential%3600)/60, 2); |
---|
133 | } |
---|
134 | else |
---|
135 | { |
---|
136 | str += '-'; |
---|
137 | NumberFormatter::append0(str, -timeZoneDifferential/3600, 2); |
---|
138 | str += ':'; |
---|
139 | NumberFormatter::append0(str, (-timeZoneDifferential%3600)/60, 2); |
---|
140 | } |
---|
141 | } |
---|
142 | else str += 'Z'; |
---|
143 | } |
---|
144 | |
---|
145 | |
---|
146 | void DateTimeFormatter::tzdRFC(std::string& str, int timeZoneDifferential) |
---|
147 | { |
---|
148 | if (timeZoneDifferential != UTC) |
---|
149 | { |
---|
150 | if (timeZoneDifferential >= 0) |
---|
151 | { |
---|
152 | str += '+'; |
---|
153 | NumberFormatter::append0(str, timeZoneDifferential/3600, 2); |
---|
154 | NumberFormatter::append0(str, (timeZoneDifferential%3600)/60, 2); |
---|
155 | } |
---|
156 | else |
---|
157 | { |
---|
158 | str += '-'; |
---|
159 | NumberFormatter::append0(str, -timeZoneDifferential/3600, 2); |
---|
160 | NumberFormatter::append0(str, (-timeZoneDifferential%3600)/60, 2); |
---|
161 | } |
---|
162 | } |
---|
163 | else str += "GMT"; |
---|
164 | } |
---|
165 | |
---|
166 | |
---|
167 | } // namespace Poco |
---|