BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bdlt_datetimetz.h
Go to the documentation of this file.
1/// @file bdlt_datetimetz.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bdlt_datetimetz.h -*-C++-*-
8#ifndef INCLUDED_BDLT_DATETIMETZ
9#define INCLUDED_BDLT_DATETIMETZ
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bdlt_datetimetz bdlt_datetimetz
15/// @brief Provide a representation of a date and time with time zone offset.
16/// @addtogroup bdl
17/// @{
18/// @addtogroup bdlt
19/// @{
20/// @addtogroup bdlt_datetimetz
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bdlt_datetimetz-purpose"> Purpose</a>
25/// * <a href="#bdlt_datetimetz-classes"> Classes </a>
26/// * <a href="#bdlt_datetimetz-description"> Description </a>
27/// * <a href="#bdlt_datetimetz-caveats-on-time-zone-support"> Caveats on Time Zone Support </a>
28/// * <a href="#bdlt_datetimetz-iso-standard-text-representation"> ISO Standard Text Representation </a>
29/// * <a href="#bdlt_datetimetz-usage"> Usage </a>
30/// * <a href="#bdlt_datetimetz-example-1-basic-bdlt-datetimetz-usage"> Example 1: Basic bdlt::DatetimeTz Usage </a>
31/// * <a href="#bdlt_datetimetz-example-2-delivery-estimation-system"> Example 2: Delivery Estimation System </a>
32///
33/// # Purpose {#bdlt_datetimetz-purpose}
34/// Provide a representation of a date and time with time zone offset.
35///
36/// # Classes {#bdlt_datetimetz-classes}
37///
38/// - bdlt::DatetimeTz: local-datetime value with time zone offset from UTC
39///
40/// @see bdlt_datetime, bdlt_datetimetzformatter, bdlt_formatdoc
41///
42/// # Description {#bdlt_datetimetz-description}
43/// This component provides a single value-semantic class,
44/// `bdlt::DatetimeTz`, that represents a datetime value in a particular time
45/// zone. Each `bdlt::DatetimeTz` object contains a time zone offset from UTC
46/// (in minutes) and a `bdlt::Datetime` value in that time zone. For logical
47/// consistency, the datetime value and offset should correspond to a
48/// geographically valid time zone, but such consistency is the user's
49/// responsibility. This component does not enforce logical constraints on any
50/// values.
51///
52/// ## Caveats on Time Zone Support {#bdlt_datetimetz-caveats-on-time-zone-support}
53///
54///
55/// A `bdlt::DatetimeTz` value is intended to be interpreted as a value in a
56/// local time zone, along with the offset of that value from UTC. However,
57/// there are some problems with this simple interpretation. First of all, the
58/// offset value may not correspond to any time zone that has ever existed. For
59/// example, the offset value could be set to one minute, or to 1,234 minutes.
60/// The meaning of the resulting "local datetime" value is always clear, but the
61/// local datetime might not correspond to any geographical or historical time
62/// zone.
63///
64/// The second problem is more subtle. A given offset from UTC might be "valid"
65/// in that it corresponds to a real time zone, but the actual datetime value
66/// might not exist in that time zone. To make matters worse, a "valid" offset
67/// may not (indeed, rarely will) specify one time zone uniquely. Moreover, the
68/// datetime value might be valid in one time zone corresponding to a given
69/// offset, and not in another time zone.
70///
71/// For these reasons (and others), this component cannot and does not perform
72/// any validation relating to time zones or offsets. The user must take care
73/// to honor the "local datetime" contract of this component.
74///
75/// ## ISO Standard Text Representation {#bdlt_datetimetz-iso-standard-text-representation}
76///
77///
78/// A common standard text representation of a date and time value is described
79/// by ISO 8601. BDE provides the @ref bdlt_iso8601util component for conversion
80/// to and from the standard ISO8601 format.
81///
82/// ## Usage {#bdlt_datetimetz-usage}
83///
84///
85/// This section illustrates intended use of this component.
86///
87/// ### Example 1: Basic bdlt::DatetimeTz Usage {#bdlt_datetimetz-example-1-basic-bdlt-datetimetz-usage}
88///
89///
90/// This example demonstrates how to create and use a `bdlt::DatetimeTz` object.
91///
92/// First, create an object `dt1` having the default value, and then verify that
93/// it contains an offset of 0, implying that the object represents a date and
94/// time in the UTC time zone, and the value of the datetime is the same as that
95/// of a default constructed `bdlt::Datetime` object:
96/// @code
97/// bdlt::DatetimeTz dt1;
98/// assert(0 == dt1.offset());
99/// assert(bdlt::Datetime() == dt1.localDatetime());
100/// @endcode
101/// Then, set `dt1` to the value 12:00 noon (12:00:00.000) on 12/31/2005 in the
102/// EST time zone (UTC-5):
103/// @code
104/// bdlt::Datetime datetime1(2005, 12, 31, 12, 0, 0, 0);
105/// bdlt::Datetime datetime2(datetime1);
106/// int offset1 = -5 * 60;
107///
108/// dt1.setDatetimeTz(datetime1, offset1);
109/// assert(offset1 == dt1.offset());
110/// assert(dt1.localDatetime() != dt1.utcDatetime());
111/// assert(datetime1 == dt1.localDatetime());
112/// assert(datetime2 != dt1.utcDatetime());
113///
114/// datetime2.addMinutes(-offset1);
115/// assert(datetime2 == dt1.utcDatetime());
116/// @endcode
117/// Next, create `dt2` as a copy of `dt1`:
118/// @code
119/// bdlt::DatetimeTz dt2(dt1);
120/// assert(offset1 == dt2.offset());
121/// assert(datetime1 == dt2.localDatetime());
122/// assert(datetime2 == dt2.utcDatetime());
123/// @endcode
124/// Now, create a third object, `dt3`, representing the time 10:33:25.000 on
125/// 01/01/2001 in the PST time zone (UTC-8):
126/// @code
127/// bdlt::Datetime datetime3(2001, 1, 1, 10, 33, 25, 0);
128/// bdlt::Datetime datetime4(datetime3);
129/// int offset2 = -8 * 60;
130///
131/// bdlt::DatetimeTz dt3(datetime3, offset2);
132/// assert(offset2 == dt3.offset());
133/// assert(dt3.localDatetime() != dt3.utcDatetime());
134/// assert(datetime3 == dt3.localDatetime());
135/// assert(datetime4 != dt3.utcDatetime());
136///
137/// datetime4.addMinutes(-offset2);
138/// assert(datetime4 == dt3.utcDatetime());
139/// @endcode
140/// Finally, stream the values of `dt1`, `dt2`, and `dt3` to `stdout`:
141/// @code
142/// bsl::cout << dt1 << bsl::endl
143/// << dt2 << bsl::endl
144/// << dt3 << bsl::endl;
145/// @endcode
146/// The streaming operator produces the following output on `stdout`:
147/// @code
148/// 31DEC2005_12:00:00.000-0500
149/// 31DEC2005_12:00:00.000-0500
150/// 01JAN2001_10:33:25.000-0800
151/// @endcode
152///
153/// ### Example 2: Delivery Estimation System {#bdlt_datetimetz-example-2-delivery-estimation-system}
154///
155///
156/// Let us suppose that we are implementing a delivery estimation system for a
157/// shipping company. The system provides estimated delivery dates and times of
158/// client shipments. This information is provided in the local time zone and
159/// is represented as a `bdlt::DatetimeTz` object. Below is the definition for
160/// a struct that returns the estimated delivery date.
161/// @code
162/// // =====================
163/// // struct DeliverySystem
164/// // =====================
165///
166/// /// This struct provides a function that returns the estimated delivery
167/// /// date and time for a particular shipment.
168/// struct DeliverySystem {
169///
170/// // PRIVATE CLASS METHODS
171///
172/// /// Return the current UTC date and time.
173/// static bdlt::Datetime getCurrentUTCDatetime();
174///
175/// public:
176/// // TYPES
177///
178/// /// This enumeration provides an identifier for the various cities.
179/// enum City {
180///
181/// e_CHICAGO = 0,
182/// e_DUBAI,
183/// e_NEW_YORK,
184/// e_LONDON,
185/// e_LOS_ANGELES
186/// };
187///
188/// // CLASS METHODS
189///
190/// /// Return the estimated delivery date and time, in local time, for
191/// /// a shipment being sent to the specified `city`.
192/// static bdlt::DatetimeTz getEstimatedDeliveryDatetime(City city);
193/// };
194/// @endcode
195/// All the relevant data used for delivery estimation is stored in a lookup
196/// table as shown below:
197/// @code
198/// const int k_MINUTES_PER_HOUR = 60;
199///
200/// static const struct {
201/// int d_offset; // time zone offset from UTC (in minutes)
202/// int d_deliveryTime; // delivery time (in minutes)
203/// } DATA[] = {
204/// // Offset DeliveryTime
205/// // ======================= =======================
206/// { -6 * k_MINUTES_PER_HOUR, 10 * k_MINUTES_PER_HOUR }, // Chicago
207/// { 3 * k_MINUTES_PER_HOUR, 72 * k_MINUTES_PER_HOUR }, // Dubai
208/// { -5 * k_MINUTES_PER_HOUR, k_MINUTES_PER_HOUR }, // New York
209/// { k_MINUTES_PER_HOUR, 36 * k_MINUTES_PER_HOUR }, // London
210/// { -8 * k_MINUTES_PER_HOUR, 24 * k_MINUTES_PER_HOUR }, // Los Angeles
211/// };
212/// @endcode
213/// And here are the function definitions:
214/// @code
215/// // ---------------------
216/// // struct DeliverySystem
217/// // ---------------------
218///
219/// // PRIVATE CLASS METHODS
220/// bdlt::Datetime DeliverySystem::getCurrentUTCDatetime()
221/// {
222/// // Return a fixed datetime so that output is known a priori.
223/// return bdlt::Datetime(2014, 10, 17, 14, 48, 56);
224/// }
225///
226/// // CLASS METHODS
227/// bdlt::DatetimeTz DeliverySystem::getEstimatedDeliveryDatetime(City city)
228/// {
229/// bdlt::Datetime localDatetime(getCurrentUTCDatetime());
230/// localDatetime.addMinutes(DATA[city].d_offset
231/// + DATA[city].d_deliveryTime);
232/// return bdlt::DatetimeTz(localDatetime, DATA[city].d_offset);
233/// }
234/// @endcode
235/// When we print out the delivery times:
236/// @code
237/// bsl::cout << "Estimated Delivery Time in Chicago: "
238/// << DeliverySystem::getEstimatedDeliveryDatetime(
239/// DeliverySystem::e_CHICAGO)
240/// << bsl::endl;
241/// bsl::cout << "Estimated Delivery Time in Dubai: "
242/// << DeliverySystem::getEstimatedDeliveryDatetime(
243/// DeliverySystem::e_DUBAI)
244/// << bsl::endl;
245/// bsl::cout << "Estimated Delivery Time in New York: "
246/// << DeliverySystem::getEstimatedDeliveryDatetime(
247/// DeliverySystem::e_NEW_YORK)
248/// << bsl::endl;
249/// bsl::cout << "Estimated Delivery Time in London: "
250/// << DeliverySystem::getEstimatedDeliveryDatetime(
251/// DeliverySystem::e_LONDON)
252/// << bsl::endl;
253/// bsl::cout << "Estimated Delivery Time in Los Angeles: "
254/// << DeliverySystem::getEstimatedDeliveryDatetime(
255/// DeliverySystem::e_LOS_ANGELES)
256/// << bsl::endl;
257/// @endcode
258/// We get the following results:
259/// @code
260/// Estimated Delivery Time in Chicago: 17OCT2014_18:48:56.000-0600
261/// Estimated Delivery Time in Dubai: 20OCT2014_17:48:56.000+0300
262/// Estimated Delivery Time in New York: 17OCT2014_10:48:56.000-0500
263/// Estimated Delivery Time in London: 19OCT2014_03:48:56.000+0100
264/// Estimated Delivery Time in Los Angeles: 18OCT2014_06:48:56.000-0800
265/// @endcode
266/// @}
267/** @} */
268/** @} */
269
270/** @addtogroup bdl
271 * @{
272 */
273/** @addtogroup bdlt
274 * @{
275 */
276/** @addtogroup bdlt_datetimetz
277 * @{
278 */
279
280#include <bdlscm_version.h>
281
282#include <bdlt_datetime.h>
283#include <bdlt_datetz.h>
284#include <bdlt_time.h>
285#include <bdlt_timetz.h>
286
287#include <bslh_hash.h>
288
291
292#include <bsls_annotation.h>
293#include <bsls_assert.h>
294
295#include <bsl_iosfwd.h>
296
297
298namespace bdlt {
299
300 // ================
301 // class DatetimeTz
302 // ================
303
304/// This value-semantic class describes a datetime value in a particular
305/// time zone, which is indicated using an offset from UTC (in minutes).
306///
307/// See @ref bdlt_datetimetz
309 // PRIVATE TYPES
310
311 /// This enumeration specifies the minimum and maximum time zone offset
312 /// values.
313 enum ValidOffsetRange {
314 k_MAX_OFFSET = 1440,
315 k_MIN_OFFSET = -1440
316 };
317
318 // DATA
319 Datetime d_localDatetime; // datetime value in timezone specified by
320 // `d_offset`
321
322 int d_offset; // offset from UTC (in minutes)
323
324 public:
325 // CLASS METHODS
326
327 /// Return `true` if the specified `localDatetime` and the specified
328 /// time zone `offset` represent a valid `DatetimeTz` value, and `false`
329 /// otherwise. A `localDatetime` and `offset` represent a valid
330 /// `DatetimeTz` value if either `bdlt::Time() == localDatetime.time()`
331 /// and `0 == offset`, or `bdlt::Time() != localDatetime.time()` and `offset` is in the range `( -1440 .. 1440 )`.
332 ///
333 /// \note Note that a `true`
334 /// result from this function does not guarantee that `offset`
335 /// corresponds to any geographical or historical time zone. Also note
336 /// that a `true` result from this function does not guarantee that
337 /// `localDatetime` itself is a valid `Datetime` object.
338 static bool isValid(const Datetime& localDatetime, int offset);
339
340 // Aspects
341
342 /// Return the maximum valid BDEX format version, as indicated by the
343 /// specified `versionSelector`, to be passed to the `bdexStreamOut` method.
344 ///
345 /// \note Note that it is highly recommended that `versionSelector`
346 /// be formatted as "YYYYMMDD", a date representation. Also note that
347 /// `versionSelector` should be a *compile*-time-chosen value that
348 /// selects a format version supported by both externalizer and
349 /// unexternalizer. See the `bslx` package-level documentation for more
350 /// information on BDEX streaming of value-semantic types and
351 /// containers.
352 static int maxSupportedBdexVersion(int versionSelector);
353
354 // CREATORS
355
356 /// Create a `DatetimeTz` object having the (default) attribute values:
357 /// @code
358 /// localDatetime() == bdlt::Datetime()
359 /// offset() == 0
360 /// @endcode
361 DatetimeTz();
362
363 /// Create a `DatetimeTz` object having a local datetime value equal to
364 /// the specified `localDatetime` and a time zone offset value from UTC
365 /// equal to the specified `offset` (in minutes).
366 ///
367 /// \pre The behavior is undefined unless all of the specified values are within their valid ranges (see `isValid`).
368 ///
369 /// \note Note that this method provides no
370 /// validation, and it is the user's responsibility to ensure that
371 /// `offset` represents a valid time zone and that `localDatetime`
372 /// represents a valid datetime in that time zone.
374
375 /// Create a `DatetimeTz` object having the same value as the specified
376 /// `original` object.
377 DatetimeTz(const DatetimeTz& original);
378
379 /// Destroy this object.
380 ~DatetimeTz();
381
382 // MANIPULATORS
383
384 /// Assign to this object the value of the specified `rhs` object, and
385 /// return a reference providing modifiable access to this object.
386 DatetimeTz& operator=(const DatetimeTz& rhs);
387
388 /// Set the local datetime and the time zone offset of this object to
389 /// the specified `localDatetime` and `offset` values respectively.
390 ///
391 /// \pre The behavior is undefined unless all of the specified values are within their valid ranges (see `isValid`).
392 ///
393 /// \note Note that this method provides
394 /// no validation, and it is the user's responsibility to assure the
395 /// consistency of the resulting value.
396 void setDatetimeTz(const Datetime& localDatetime, int offset);
397
398 /// If the specified `localDatetime` and `offset` represent a valid
399 /// `DatetimeTz` value (see `isValid`), set the local datetime and the
400 /// time zone offset of this object to the `localDatetime` and `offset`
401 /// values respectively and return 0, leave this object unmodified and
402 /// return a non-zero value otherwise.
404
405 // Aspects
406
407 /// Assign to this object the value read from the specified input
408 /// `stream` using the specified `version` format, and return a
409 /// reference to `stream`. If `stream` is initially invalid, this
410 /// operation has no effect. If `version` is not supported, this object
411 /// is unaltered and `stream` is invalidated, but otherwise unmodified.
412 /// If `version` is supported but `stream` becomes invalid during this
413 /// operation, this object has an undefined, but valid, state.
414 ///
415 /// \note Note that no version is read from `stream`. See the `bslx` package-level
416 /// documentation for more information on BDEX streaming of
417 /// value-semantic types and containers.
418 template <class STREAM>
419 STREAM& bdexStreamIn(STREAM& stream, int version);
420
421 // ACCESSORS
422
423 /// Return a `DateTz` object having the value of the local date and
424 /// offset represented by this object.
425 DateTz dateTz() const;
426
427 /// Return a `Datetime` object having the value of the local datetime represented by this object.
428 ///
429 /// \note Note that the `Datetime` value returned
430 /// is the current value stored in this object and may be different from
431 /// the local datetime of the system.
432 Datetime localDatetime() const;
433
434 /// Return the time zone offset of this `DatetimeTz` object.
435 ///
436 /// \note Note that the offset is in minutes from UTC.
437 int offset() const;
438
439 /// Return a `TimeTz` object having the value of the local time and
440 /// offset represented by this object.
441 TimeTz timeTz() const;
442
443 /// Return a `Datetime` object having the value of the UTC datetime represented by this object.
444 ///
445 /// \note Note that if `0 != offset()`, the
446 /// returned value is equal to `localDatetime()` minus `offset()`
447 /// minutes, and `localDatetime()` otherwise.
448 Datetime utcDatetime() const;
449
450 // Aspects
451
452 /// Write the value of this object, using the specified `version`
453 /// format, to the specified output `stream`, and return a reference to
454 /// `stream`. If `stream` is initially invalid, this operation has no
455 /// effect. If `version` is not supported, `stream` is invalidated, but otherwise unmodified.
456 ///
457 /// \note Note that `version` is not written to
458 /// `stream`. See the `bslx` package-level documentation for more
459 /// information on BDEX streaming of value-semantic types and
460 /// containers.
461 template <class STREAM>
462 STREAM& bdexStreamOut(STREAM& stream, int version) const;
463
464 /// Write the value of this object to the specified output `stream` in a
465 /// human-readable format, and return a reference to `stream`.
466 /// Optionally specify an initial indentation `level`, whose absolute
467 /// value is incremented recursively for nested objects. If `level` is
468 /// specified, optionally specify `spacesPerLevel`, whose absolute value
469 /// indicates the number of spaces per indentation level for this and
470 /// all of its nested objects. If `level` is negative, suppress
471 /// indentation of the first line. If `spacesPerLevel` is negative,
472 /// format the entire output on one line, suppressing all but the
473 /// initial indentation (as governed by `level`). If `stream` is not valid on entry, this operation has no effect.
474 ///
475 /// \note Note that the format
476 /// is not fully specified, and can change without notice.
477 bsl::ostream& print(bsl::ostream& stream,
478 int level = 0,
479 int spacesPerLevel = 4) const;
480
481#ifndef BDE_OPENSOURCE_PUBLICATION // pending deprecation
482
483 // DEPRECATED METHODS
484
485 /// Return a `Datetime` object having the value of the UTC datetime represented by this object.
486 ///
487 /// \note Note that if `0 != offset()`, the
488 /// returned value is equal to `localDatetime()` minus `offset()`
489 /// minutes, and `localDatetime()` otherwise.
490 ///
491 /// @deprecated replaced by `utcDatetime.`
492 Datetime gmtDatetime() const;
493
494 /// Return the most current BDEX streaming version number supported by
495 /// this class.
496 ///
497 /// @deprecated Use @ref maxSupportedBdexVersion(int) instead.
498 static int maxSupportedBdexVersion();
499
500 /// If the specified `localDatetime` and `offset` represent a valid
501 /// `DatetimeTz` value (see `isValid`), set the local datetime and the
502 /// time zone offset of this object to the `localDatetime` and `offset`
503 /// values respectively and return 0, leave this object unmodified and
504 /// return a non-zero value otherwise.
505 ///
506 /// @deprecated replaced by `setDatetimeTzIfValid`.
508
509#endif // BDE_OPENSOURCE_PUBLICATION -- pending deprecation
510
511};
512
513// FREE OPERATORS
514
515/// Return `true` if the specified `lhs` and `rhs` `DatetimeTz` objects have
516/// the same value, and `false` otherwise. Two `DatetimeTz` objects have
517/// the same value if they have the same local datetime value and the same
518/// time zone offset value.
519bool operator==(const DatetimeTz& lhs, const DatetimeTz& rhs);
520
521/// Return `true` if the specified `lhs` and `rhs` `DatetimeTz` objects do
522/// not have the same value, and `false` otherwise. Two `DatetimeTz`
523/// objects do not have the same value if they do not have the same local
524/// datetime value or the same time zone offset value.
525bool operator!=(const DatetimeTz& lhs, const DatetimeTz& rhs);
526
527/// Write the value of the specified `rhs` object to the specified output
528/// `stream` in a single-line format, and return a reference providing
529/// modifiable access to `stream`. If `stream` is not valid on entry, this operation has no effect.
530///
531/// \note Note that this human-readable format is not
532/// fully specified and can change without notice. Also note that this
533/// method has the same behavior as `object.print(stream, 0, -1)`, but with
534/// the attribute names elided.
535bsl::ostream& operator<<(bsl::ostream& stream, const DatetimeTz& rhs);
536
537// FREE FUNCTIONS
538
539/// Pass the specified `object` to the specified `hashAlg`. This function
540/// integrates with the `bslh` modular hashing system and effectively provides a `bsl::hash` specialization for `DatetimeTz`.
541///
542/// \note Note that two
543/// objects which represent the same UTC time but have different offsets
544/// will not (necessarily) hash to the same value.
545template <class HASHALG>
546void hashAppend(HASHALG& hashAlg, const DatetimeTz& object);
547
548// ============================================================================
549// INLINE DEFINITIONS
550// ============================================================================
551
552 // ----------------
553 // class DatetimeTz
554 // ----------------
555
556// CLASS METHODS
557inline
558bool DatetimeTz::isValid(const Datetime& localDatetime, int offset)
559{
560 return k_MIN_OFFSET < offset
561 && k_MAX_OFFSET > offset
562 && (bdlt::Time() != localDatetime.time() || 0 == offset);
563}
564
565 // Aspects
566
567inline
569{
570 if (versionSelector >= 20170401) {
571 return 2; // RETURN
572 }
573 return 1;
574}
575
576// CREATORS
577inline
579: d_localDatetime()
580, d_offset(0)
581{
582}
583
584inline
585DatetimeTz::DatetimeTz(const Datetime& localDatetime, int offset)
586: d_localDatetime(localDatetime)
587, d_offset(offset)
588{
590}
591
592inline
594: d_localDatetime(original.d_localDatetime)
595, d_offset(original.d_offset)
596{
597}
598
599inline
601{
602 BSLS_ASSERT_SAFE(isValid(d_localDatetime, d_offset));
603}
604
605// MANIPULATORS
606inline
608{
609 d_localDatetime = rhs.d_localDatetime;
610 d_offset = rhs.d_offset;
611
612 return *this;
613}
614
615inline
616void DatetimeTz::setDatetimeTz(const Datetime& localDatetime, int offset)
617{
619
620 d_localDatetime = localDatetime;
621 d_offset = offset;
622}
623
624inline
625int DatetimeTz::setDatetimeTzIfValid(const Datetime& localDatetime, int offset)
626{
629 return 0; // RETURN
630 }
631 return -1;
632}
633
634 // Aspects
635
636template <class STREAM>
637STREAM& DatetimeTz::bdexStreamIn(STREAM& stream, int version)
638{
639 if (stream) {
640 switch (version) { // switch on the schema version
641 case 2:
643 case 1: {
645 localDatetime.bdexStreamIn(stream, version);
646
647 // Note that we have to initialize `offset` to a value in order to
648 // silence potential -Wmaybe-uninitialized.
649
650 int offset = 0;
651 stream.getInt32(offset);
652
653 if (stream && isValid(localDatetime, offset)) {
654 d_localDatetime = localDatetime;
655 d_offset = offset;
656 }
657 else {
658 stream.invalidate();
659 }
660 } break;
661 default: {
662 stream.invalidate(); // unrecognized version number
663 }
664 }
665 }
666 return stream;
667}
668
669// ACCESSORS
670inline
672{
673 return DateTz(d_localDatetime.date(), d_offset);
674}
675
676inline
678{
679 return d_localDatetime;
680}
681
682inline
684{
685 return d_offset;
686}
687
688inline
690{
691 return TimeTz(d_localDatetime.time(), d_offset);
692}
693
694inline
696{
697 Datetime utc(d_localDatetime);
698 if (d_offset) {
699 utc.addMinutes(-d_offset);
700 }
701 return utc;
702}
703
704 // Aspects
705
706template <class STREAM>
707STREAM& DatetimeTz::bdexStreamOut(STREAM& stream, int version) const
708{
709 if (stream) {
710 switch (version) { // switch on the schema version
711 case 2:
713 case 1: {
714 d_localDatetime.bdexStreamOut(stream, version);
715 stream.putInt32(d_offset);
716 } break;
717 default: {
718 stream.invalidate(); // unrecognized version number
719 }
720 }
721 }
722 return stream;
723}
724
725#ifndef BDE_OPENSOURCE_PUBLICATION // pending deprecation
726
727// DEPRECATED METHODS
728inline
730{
731 return utcDatetime();
732}
733
734inline
739
740inline
742 int offset)
743{
745}
746
747#endif // BDE_OPENSOURCE_PUBLICATION -- pending deprecation
748
749} // close package namespace
750
751// FREE OPERATORS
752inline
753bool bdlt::operator==(const DatetimeTz& lhs, const DatetimeTz& rhs)
754{
755 return lhs.localDatetime() == rhs.localDatetime()
756 && lhs.offset() == rhs.offset();
757}
758
759inline
760bool bdlt::operator!=(const DatetimeTz& lhs, const DatetimeTz& rhs)
761{
762 return !(lhs == rhs);
763}
764
765inline
766bsl::ostream& bdlt::operator<<(bsl::ostream& stream, const DatetimeTz& rhs)
767{
768 return rhs.print(stream, 0, -1);
769}
770
771// FREE FUNCTIONS
772template <class HASHALG>
773inline
774void bdlt::hashAppend(HASHALG& hashAlg, const DatetimeTz& object)
775{
776 using ::BloombergLP::bslh::hashAppend;
777 hashAppend(hashAlg, object.localDatetime());
778 hashAppend(hashAlg, object.offset());
779}
780
781namespace bslmf {
782
783// TRAITS
784
785/// This template specialization for `IsBitwiseCopyable` indicates that
786/// `bdlt::DatetimeTz` is a bitwise copyable type.
787template <>
788struct IsBitwiseCopyable<BloombergLP::bdlt::DatetimeTz> : bsl::true_type {
789};
790
791} // close namespace bslmf
792
793
794#endif
795
796// ----------------------------------------------------------------------------
797// Copyright 2016 Bloomberg Finance L.P.
798//
799// Licensed under the Apache License, Version 2.0 (the "License");
800// you may not use this file except in compliance with the License.
801// You may obtain a copy of the License at
802//
803// http://www.apache.org/licenses/LICENSE-2.0
804//
805// Unless required by applicable law or agreed to in writing, software
806// distributed under the License is distributed on an "AS IS" BASIS,
807// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
808// See the License for the specific language governing permissions and
809// limitations under the License.
810// ----------------------------- END-OF-FILE ----------------------------------
811
812/** @} */
813/** @} */
814/** @} */
Definition bdlt_datetz.h:161
Definition bdlt_datetimetz.h:308
Datetime localDatetime() const
Definition bdlt_datetimetz.h:677
~DatetimeTz()
Destroy this object.
Definition bdlt_datetimetz.h:600
STREAM & bdexStreamOut(STREAM &stream, int version) const
Definition bdlt_datetimetz.h:707
void setDatetimeTz(const Datetime &localDatetime, int offset)
Definition bdlt_datetimetz.h:616
DateTz dateTz() const
Definition bdlt_datetimetz.h:671
DatetimeTz()
Definition bdlt_datetimetz.h:578
STREAM & bdexStreamIn(STREAM &stream, int version)
Definition bdlt_datetimetz.h:637
Datetime gmtDatetime() const
Definition bdlt_datetimetz.h:729
TimeTz timeTz() const
Definition bdlt_datetimetz.h:689
int validateAndSetDatetimeTz(const Datetime &localDatetime, int offset)
Definition bdlt_datetimetz.h:741
static int maxSupportedBdexVersion()
Definition bdlt_datetimetz.h:735
static bool isValid(const Datetime &localDatetime, int offset)
Definition bdlt_datetimetz.h:558
DatetimeTz & operator=(const DatetimeTz &rhs)
Definition bdlt_datetimetz.h:607
int offset() const
Definition bdlt_datetimetz.h:683
Datetime utcDatetime() const
Definition bdlt_datetimetz.h:695
int setDatetimeTzIfValid(const Datetime &localDatetime, int offset)
Definition bdlt_datetimetz.h:625
bsl::ostream & print(bsl::ostream &stream, int level=0, int spacesPerLevel=4) const
Definition bdlt_datetime.h:330
Date date() const
Return the value of the "date" part of this object.
Definition bdlt_datetime.h:2234
Datetime & addMinutes(bsls::Types::Int64 minutes)
Definition bdlt_datetime.h:2062
Time time() const
Return the value of the "time" part of this object.
Definition bdlt_datetime.h:2345
STREAM & bdexStreamOut(STREAM &stream, int version) const
Definition bdlt_datetime.h:2367
STREAM & bdexStreamIn(STREAM &stream, int version)
Definition bdlt_datetime.h:2190
Definition bdlt_timetz.h:190
Definition bdlt_time.h:195
#define BSLS_ANNOTATION_FALLTHROUGH
Definition bsls_annotation.h:410
#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
void hashAppend(HASH_ALGORITHM &hashAlgorithm, const BigEndianInt16 &object)
Definition bbldc_basicisma30360.h:112
bool operator==(const Calendar &lhs, const Calendar &rhs)
bsl::ostream & operator<<(bsl::ostream &stream, const Calendar &calendar)
void hashAppend(HASHALG &hashAlg, const Calendar &object)
bool operator!=(const Calendar &lhs, const Calendar &rhs)
ALLOCATOR const STRING_VIEW_LIKE_TYPE & rhs
Definition bslstl_string.h:3918
ALLOCATOR & lhs
Definition bslstl_string.h:3917
Definition bdlbb_blob.h:579