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