BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bdlt_datetzformatter.h
Go to the documentation of this file.
1/// @file bdlt_datetzformatter.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bdlt_datetzformatter.h -*-C++-*-
8#ifndef INCLUDED_BDLT_DATETZFORMATTER
9#define INCLUDED_BDLT_DATETZFORMATTER
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bdlt_datetzformatter bdlt_datetzformatter
15/// @brief Provide `bsl::formatter` specialization for `bdlt::DateTz`.
16/// @addtogroup bdl
17/// @{
18/// @addtogroup bdlt
19/// @{
20/// @addtogroup bdlt_datetzformatter
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bdlt_datetzformatter-purpose"> Purpose</a>
25/// * <a href="#bdlt_datetzformatter-classes"> Classes </a>
26/// * <a href="#bdlt_datetzformatter-description"> Description </a>
27///
28/// # Purpose {#bdlt_datetzformatter-purpose}
29/// Provide `bsl::formatter` specialization for `bdlt::DateTz`.
30///
31/// # Classes {#bdlt_datetzformatter-classes}
32///
33/// - bdlt::DateTzFormatter: datetz formatter for `bslfmt` framework
34/// - bsl::formatter<bdlt::DateTz, t_CHAR>: specialization
35///
36/// @see bdlt_datetz, bdlt_dateformatter, bslfmt_formatter
37///
38/// # Description {#bdlt_datetzformatter-description}
39/// This component provides `bdlt::DateTzFormatter` and a
40/// specialization of `bsl::formatter` that allow `bsl::format` to output
41/// values of `bdlt::DateTz`.
42///
43/// The formatter interprets the following modifiers:
44/// - 'Z' - if the offset is zero, output the time zone as 'Z', otherwise output
45/// it normally.
46/// - ':' (colon) - always print a colon between hours and minutes
47/// - '_' (underscore) - never print a colon between hours and minutes
48/// Note that it is an error for both ':' and '_' to be specified at the same
49/// time.
50///
51/// The formatter supports various `Date` format specifiers including:
52/// - Year: 'Y' (4-digit), 'y' (2-digit), 'C' (century)
53/// - Month: 'm' (numeric), 'b'/'h' (abbreviated name)
54/// - Day: 'd' (zero-padded), 'e' (space-padded)
55/// - Day of week: 'a' (abbreviated name), 'u'/'w' (numeric)
56/// - Day of year: 'j' (numeric)
57/// all of the above will yield results for `bdlt::DateTz::localDate()`.
58/// - Time Zone: 'z' (2 digit hour, 2 digit minute)
59/// - Compound: 'D' (default local date format), 'F' (ISO 8601 local date
60/// format) "{}" (default local date format with time zone), 'i' (ISO 8601
61/// local date format with time zone).
62/// @}
63/** @} */
64/** @} */
65
66/** @addtogroup bdl
67 * @{
68 */
69/** @addtogroup bdlt
70 * @{
71 */
72/** @addtogroup bdlt_datetzformatter
73 * @{
74 */
75
76#include <bdlscm_version.h>
77
78#include <bdlt_date.h>
79#include <bdlt_dateformatter.h>
80#include <bdlt_datetz.h>
81#include <bdlt_formatter.h>
83
84#include <bsl_iosfwd.h>
85#include <bsl_string_view.h>
86
87
88namespace bdlt {
89
90class DateTzFormatter_Cache;
91
92 // =====================
93 // class DateTzFormatter
94 // =====================
95
96/// This `class` provides a specifier formatter for printing `DateTz` objects.
97///
98/// See @ref bdlt_datetzformatter
99template <class t_CHAR>
101 typedef DateFormatter<t_CHAR> Date_Formatter;
102 typedef TimeZoneFormatter<t_CHAR> TimeZone_Formatter;
105
106 // DATA
107 Date_Formatter d_dateFormatter;
108 // Formatter for date specifiers.
109
110 TimeZone_Formatter d_timeZoneFormatter;
111 // Formatter for time zone specifiers.
112
113 public:
114 // CREATORS
115
116 /// Create an object in its default initial state.
119
120 // MANIPULATORS
121
122 /// Parse a date tz that will be formatted in default mode.
124
125 /// Parse a date tz that will be formatted in Iso8601 mode.
127
128 /// Examine the first character of the specified `*specInOut` and if it is
129 /// recognized as a modifier by this object, update this object's state to
130 /// reflect it and pop it off the front of `*specInOut` and return `true`,
131 /// and if not, return `false` with no modification to `*specInOut`.
133
134 /// If the first character of the specified `*specInOut` is recognized by
135 /// this specifier formatter, parse it, remove it from `*specInOut`, and
136 /// return `true`, otherwise return `false` with no modification to `*specInOut`.
137 ///
138 /// \pre The behavior is undefined if `*specInOut` is empty.
140 bool parseNextSpecifier(StringView *specInOut);
141
142 /// Read post-processed fields from the specified `spec` that are relevant
143 /// to this specifier formatter.
145
146 // ACCESSORS
147
148 /// Return the `bslfmt::FormatSpecificationParser::Sections` flags that
149 /// apply to this value type.
151
152 /// Return the anticipated width of output given all the `parse*` calls
153 /// that have been happened thus far and the specified `value`.
154 int totalWidth(const FormatCache& value) const;
155
156 /// If the first character of the specified `*specInOut` is recognized by
157 /// this specifier formatter, use it to format the specified `value` to
158 /// `*outIt`, remove the character from `*specInOut`, and return `true`,
159 /// otherwise return `false` with no modification to `*specInOut`.
160 template <class t_ITERATOR>
161 bool formatNextSpecifier(StringView *specInOut,
162 t_ITERATOR *outIt,
163 const FormatCache& value) const;
164
165 /// Format the specified `value` to the specified `out` using the default
166 /// format and return `out`.
167 template <class t_ITERATOR>
168 t_ITERATOR formatDefault(t_ITERATOR out, const FormatCache& value) const;
169
170 /// Format the specified `value` to the specified `out` using the Iso8601
171 /// format and return `out`.
172 template <class t_ITERATOR>
173 t_ITERATOR formatIso8601(t_ITERATOR out, const FormatCache& value) const;
174};
175
176 // ===========================
177 // class DateTzFormatter_Cache
178 // ===========================
179
180/// This `class` facilitates faster access to a `DateTz` object during printing
181/// by batching access to several fields in a single call during construction
182/// and caching them for quick access later.
183///
184/// See @ref bdlt_datetzformatter
186 // DATA
187 DateFormatter_Cache d_dateFormatCache;
188 int d_offset;
189
190 public:
191 // CREATORS
192
193 /// Cache a copy of the specified `date` and `offset`.
195
196 // ACCESSORS
197
198 /// Return the `DateFormatter_Cache` held by this object
199 const DateFormatter_Cache& date() const;
200
201 /// Return the offset.
202 int offset() const;
203};
204
205// ============================================================================
206// INLINE FUNCTION DEFINITIONS
207// ============================================================================
208
209 // ---------------------
210 // class DateTzFormatter
211 // ---------------------
212
213// CREATORS
214template <class t_CHAR>
217: d_dateFormatter()
218, d_timeZoneFormatter()
219{}
220
221// MANIPULATORS
222template <class t_CHAR>
225{
226 d_dateFormatter. parseDefault();
227 d_timeZoneFormatter.parseDefault();
228}
229
230template <class t_CHAR>
233{
234 d_dateFormatter. parseIso8601();
235 d_timeZoneFormatter.parseIso8601();
236}
237
238template <class t_CHAR>
241{
242 return d_dateFormatter. parseNextModifier(specInOut) ||
243 d_timeZoneFormatter.parseNextModifier(specInOut);
244}
245
246template <class t_CHAR>
249{
250 BSLS_ASSERT(!specInOut->empty());
251
252 if (t_CHAR('i') == specInOut->front()) {
253 // Intercept iso format
254
255 d_dateFormatter. parseIso8601();
256 d_timeZoneFormatter.parseIso8601();
257 specInOut->remove_prefix(1);
258
259 return true; // RETURN
260 }
261
262 return d_dateFormatter. parseNextSpecifier(specInOut) ||
263 d_timeZoneFormatter.parseNextSpecifier(specInOut); // RETURN
264}
265
266template <class t_CHAR>
267inline
270{
271 d_dateFormatter. postprocess(spec);
272 d_timeZoneFormatter.postprocess(spec);
273}
274
275// ACCESSORS
276template <class t_CHAR>
279{
280 return d_dateFormatter.extraSections() |
281 d_timeZoneFormatter.extraSections();
282}
283
284template <class t_CHAR>
285inline
287{
288 return d_dateFormatter. totalWidth(value.date()) +
289 d_timeZoneFormatter.totalWidth(value.offset());
290}
291
292template <class t_CHAR>
293template <class t_ITERATOR>
294inline
296 StringView *specInOut,
297 t_ITERATOR *outIt,
298 const FormatCache& value) const
299{
300 BSLS_ASSERT_SAFE(!specInOut->empty());
301
302 if (t_CHAR('i') == specInOut->front()) {
303 *outIt = formatIso8601(*outIt, value);
304 specInOut->remove_prefix(1);
305 return true; // RETURN
306 }
307
308 return d_dateFormatter. formatNextSpecifier(specInOut,
309 outIt,
310 value.date()) ||
311 d_timeZoneFormatter.formatNextSpecifier(specInOut,
312 outIt,
313 value.offset());
314}
315
316template <class t_CHAR>
317template <class t_ITERATOR>
318inline
320 t_ITERATOR out, const FormatCache& value) const
321{
322 out = d_dateFormatter. formatDefault(out, value.date());
323 out = d_timeZoneFormatter.formatDefault(out, value.offset());
324
325 return out;
326}
327
328template <class t_CHAR>
329template <class t_ITERATOR>
330inline
332 t_ITERATOR out, const FormatCache& value) const
333{
334 out = d_dateFormatter. formatIso8601(out, value.date());
335 out = d_timeZoneFormatter.formatIso8601(out, value.offset());
336
337 return out;
338}
339
340 // ---------------------
341 // DateTzFormatter_Cache
342 // ---------------------
343
344// CREATORS
345inline
347: d_dateFormatCache(date)
348, d_offset(offset)
349{}
350
351// ACCESSORS
352inline
354{
355 return d_dateFormatCache;
356}
357
358inline
360{
361 return d_offset;
362}
363
364} // close package namespace
365
366
367namespace bsl {
368
369/// This type implements the formatter logic specific for `DateTz` objects.
370template <class t_CHAR>
371class formatter<BloombergLP::bdlt::DateTz, t_CHAR> {
372 // PRIVATE TYPES
373 typedef BloombergLP::bdlt::DateTz DateTz;
374 typedef BloombergLP::bdlt::DateTzFormatter_Cache FormatCache;
375 typedef BloombergLP::bdlt::Formatter<
376 BloombergLP::bdlt::DateTzFormatter, t_CHAR> Formatter;
377
378 // DATA
379 Formatter d_formatter;
380
381 public:
382 /// Parse and validate the specification string stored in the specified
383 /// `parseContext`. Return an end iterator of the parsed range. Throw
384 /// `bsl::format_error`, in the event of failure.
385 template <class t_PARSE_CONTEXT>
386 BSLS_KEYWORD_CONSTEXPR_CPP20 typename t_PARSE_CONTEXT::iterator parse(
387 t_PARSE_CONTEXT& context)
388 {
389 return d_formatter.parse(context);
390 }
391
392 /// Format the value in the specified `value` parameter according to the
393 /// specification stored as a result of a previous call to the `parse`
394 /// method, and write the result to the iterator accessed by calling the
395 /// `out()` method on the specified `formatContext` parameter. Return an
396 /// end iterator of the output range.
397 template <class t_FORMAT_CONTEXT>
398 typename t_FORMAT_CONTEXT::iterator format(
399 const DateTz& value,
400 t_FORMAT_CONTEXT& formatContext) const
401 {
402 const FormatCache formatCache(value.localDate(), value.offset());
403 return d_formatter.format(formatCache, formatContext);
404 }
405};
406
407} // close namespace bsl
408
409#endif
410
411// ----------------------------------------------------------------------------
412// Copyright 2026 Bloomberg Finance L.P.
413//
414// Licensed under the Apache License, Version 2.0 (the "License");
415// you may not use this file except in compliance with the License.
416// You may obtain a copy of the License at
417//
418// http://www.apache.org/licenses/LICENSE-2.0
419//
420// Unless required by applicable law or agreed to in writing, software
421// distributed under the License is distributed on an "AS IS" BASIS,
422// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
423// See the License for the specific language governing permissions and
424// limitations under the License.
425// ----------------------------- END-OF-FILE ----------------------------------
426
427/** @} */
428/** @} */
429/** @} */
Definition bdlt_dateformatter.h:224
Definition bdlt_dateformatter.h:94
Definition bdlt_datetzformatter.h:185
const DateFormatter_Cache & date() const
Return the DateFormatter_Cache held by this object.
Definition bdlt_datetzformatter.h:353
DateTzFormatter_Cache(const Date &date, int offset)
Cache a copy of the specified date and offset.
Definition bdlt_datetzformatter.h:346
int offset() const
Return the offset.
Definition bdlt_datetzformatter.h:359
Definition bdlt_datetzformatter.h:100
BSLS_KEYWORD_CONSTEXPR_CPP20 void parseDefault()
Parse a date tz that will be formatted in default mode.
Definition bdlt_datetzformatter.h:224
BSLS_KEYWORD_CONSTEXPR_CPP20 bool parseNextModifier(StringView *specInOut)
Definition bdlt_datetzformatter.h:240
BSLS_KEYWORD_CONSTEXPR_CPP20 void parseIso8601()
Parse a date tz that will be formatted in Iso8601 mode.
Definition bdlt_datetzformatter.h:232
BSLS_KEYWORD_CONSTEXPR_CPP20 bool parseNextSpecifier(StringView *specInOut)
Definition bdlt_datetzformatter.h:248
t_ITERATOR formatIso8601(t_ITERATOR out, const FormatCache &value) const
Definition bdlt_datetzformatter.h:331
t_ITERATOR formatDefault(t_ITERATOR out, const FormatCache &value) const
Definition bdlt_datetzformatter.h:319
bool formatNextSpecifier(StringView *specInOut, t_ITERATOR *outIt, const FormatCache &value) const
Definition bdlt_datetzformatter.h:295
int totalWidth(const FormatCache &value) const
Definition bdlt_datetzformatter.h:286
BSLS_KEYWORD_CONSTEXPR_CPP20 DateTzFormatter()
Create an object in its default initial state.
Definition bdlt_datetzformatter.h:216
void postprocess(const bslfmt::FormatSpecificationParser< t_CHAR > &spec)
Definition bdlt_datetzformatter.h:268
BSLS_KEYWORD_CONSTEXPR_CPP20 int extraSections() const
Definition bdlt_datetzformatter.h:278
Definition bdlt_datetz.h:161
Definition bdlt_date.h:294
Definition bdlt_timezoneformatter.h:89
Definition bslstl_stringview.h:471
BSLS_KEYWORD_CONSTEXPR_CPP14 const_reference front() const
Definition bslstl_stringview.h:1966
BSLS_KEYWORD_CONSTEXPR_CPP14 void remove_prefix(size_type numChars)
Definition bslstl_stringview.h:1800
BSLS_KEYWORD_CONSTEXPR bool empty() const BSLS_KEYWORD_NOEXCEPT
Return true if this view has length 0, and false otherwise.
Definition bslstl_stringview.h:1931
BSLS_KEYWORD_CONSTEXPR_CPP20 t_PARSE_CONTEXT::iterator parse(t_PARSE_CONTEXT &context)
Definition bdlt_datetzformatter.h:386
t_FORMAT_CONTEXT::iterator format(const DateTz &value, t_FORMAT_CONTEXT &formatContext) const
Definition bdlt_datetzformatter.h:398
Definition bslfmt_formatspecificationparser.h:151
#define BSLS_ASSERT(X)
Definition bsls_assert.h:1976
#define BSLS_ASSERT_SAFE(X)
Definition bsls_assert.h:1917
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
#define BSLS_KEYWORD_CONSTEXPR_CPP20
Definition bsls_keyword.h:645
Definition bbldc_basicisma30360.h:112
Definition bdlat_valuetypefunctions.h:939
Definition bslfmt_formatterbase.h:426