BDE 4.14.0 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
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
310 // PRIVATE TYPES
311
312 /// This enumeration specifies the minimum and maximum time zone offset
313 /// values.
314 enum ValidOffsetRange {
315 k_MAX_OFFSET = 1440,
316 k_MIN_OFFSET = -1440
317 };
318
319 // DATA
320 Datetime d_localDatetime; // datetime value in timezone specified by
321 // `d_offset`
322
323 int d_offset; // offset from UTC (in minutes)
324
325 public:
326 // CLASS METHODS
327
328 /// Return `true` if the specified `localDatetime` and the specified
329 /// time zone `offset` represent a valid `DatetimeTz` value, and `false`
330 /// otherwise. A `localDatetime` and `offset` represent a valid
331 /// `DatetimeTz` value if either `bdlt::Time() == localDatetime.time()`
332 /// and `0 == offset`, or `bdlt::Time() != localDatetime.time()` and
333 /// `offset` is in the range `( -1440 .. 1440 )`. 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`
344 /// method. Note that it is highly recommended that `versionSelector`
345 /// be formatted as "YYYYMMDD", a date representation. Also note that
346 /// `versionSelector` should be a *compile*-time-chosen value that
347 /// selects a format version supported by both externalizer and
348 /// unexternalizer. See the `bslx` package-level documentation for more
349 /// information on BDEX streaming of value-semantic types and
350 /// containers.
351 static int maxSupportedBdexVersion(int versionSelector);
352
353 // CREATORS
354
355 /// Create a `DatetimeTz` object having the (default) attribute values:
356 /// @code
357 /// localDatetime() == bdlt::Datetime()
358 /// offset() == 0
359 /// @endcode
360 DatetimeTz();
361
362 /// Create a `DatetimeTz` object having a local datetime value equal to
363 /// the specified `localDatetime` and a time zone offset value from UTC
364 /// equal to the specified `offset` (in minutes). The behavior is
365 /// undefined unless all of the specified values are within their valid
366 /// ranges (see `isValid`). Note that this method provides no
367 /// validation, and it is the user's responsibility to ensure that
368 /// `offset` represents a valid time zone and that `localDatetime`
369 /// represents a valid datetime in that time zone.
371
372 /// Create a `DatetimeTz` object having the same value as the specified
373 /// `original` object.
374 DatetimeTz(const DatetimeTz& original);
375
376 /// Destroy this object.
377 ~DatetimeTz();
378
379 // MANIPULATORS
380
381 /// Assign to this object the value of the specified `rhs` object, and
382 /// return a reference providing modifiable access to this object.
383 DatetimeTz& operator=(const DatetimeTz& rhs);
384
385 /// Set the local datetime and the time zone offset of this object to
386 /// the specified `localDatetime` and `offset` values respectively. The
387 /// behavior is undefined unless all of the specified values are within
388 /// their valid ranges (see `isValid`). Note that this method provides
389 /// no validation, and it is the user's responsibility to assure the
390 /// consistency of the resulting value.
391 void setDatetimeTz(const Datetime& localDatetime, int offset);
392
393 /// If the specified `localDatetime` and `offset` represent a valid
394 /// `DatetimeTz` value (see `isValid`), set the local datetime and the
395 /// time zone offset of this object to the `localDatetime` and `offset`
396 /// values respectively and return 0, leave this object unmodified and
397 /// return a non-zero value otherwise.
399
400 // Aspects
401
402 /// Assign to this object the value read from the specified input
403 /// `stream` using the specified `version` format, and return a
404 /// reference to `stream`. If `stream` is initially invalid, this
405 /// operation has no effect. If `version` is not supported, this object
406 /// is unaltered and `stream` is invalidated, but otherwise unmodified.
407 /// If `version` is supported but `stream` becomes invalid during this
408 /// operation, this object has an undefined, but valid, state. Note
409 /// that no version is read from `stream`. See the `bslx` package-level
410 /// documentation for more information on BDEX streaming of
411 /// value-semantic types and containers.
412 template <class STREAM>
413 STREAM& bdexStreamIn(STREAM& stream, int version);
414
415 // ACCESSORS
416
417 /// Return a `DateTz` object having the value of the local date and
418 /// offset represented by this object.
419 DateTz dateTz() const;
420
421 /// Return a `Datetime` object having the value of the local datetime
422 /// represented by this object. Note that the `Datetime` value returned
423 /// is the current value stored in this object and may be different from
424 /// the local datetime of the system.
425 Datetime localDatetime() const;
426
427 /// Return the time zone offset of this `DatetimeTz` object. Note that
428 /// the offset is in minutes from UTC.
429 int offset() const;
430
431 /// Return a `TimeTz` object having the value of the local time and
432 /// offset represented by this object.
433 TimeTz timeTz() const;
434
435 /// Return a `Datetime` object having the value of the UTC datetime
436 /// represented by this object. Note that if `0 != offset()`, the
437 /// returned value is equal to `localDatetime()` minus `offset()`
438 /// minutes, and `localDatetime()` otherwise.
439 Datetime utcDatetime() const;
440
441 // Aspects
442
443 /// Write the value of this object, using the specified `version`
444 /// format, to the specified output `stream`, and return a reference to
445 /// `stream`. If `stream` is initially invalid, this operation has no
446 /// effect. If `version` is not supported, `stream` is invalidated, but
447 /// otherwise unmodified. Note that `version` is not written to
448 /// `stream`. See the `bslx` package-level documentation for more
449 /// information on BDEX streaming of value-semantic types and
450 /// containers.
451 template <class STREAM>
452 STREAM& bdexStreamOut(STREAM& stream, int version) const;
453
454 /// Write the value of this object to the specified output `stream` in a
455 /// human-readable format, and return a reference to `stream`.
456 /// Optionally specify an initial indentation `level`, whose absolute
457 /// value is incremented recursively for nested objects. If `level` is
458 /// specified, optionally specify `spacesPerLevel`, whose absolute value
459 /// indicates the number of spaces per indentation level for this and
460 /// all of its nested objects. If `level` is negative, suppress
461 /// indentation of the first line. If `spacesPerLevel` is negative,
462 /// format the entire output on one line, suppressing all but the
463 /// initial indentation (as governed by `level`). If `stream` is not
464 /// valid on entry, this operation has no effect. Note that the format
465 /// is not fully specified, and can change without notice.
466 bsl::ostream& print(bsl::ostream& stream,
467 int level = 0,
468 int spacesPerLevel = 4) const;
469
470#ifndef BDE_OPENSOURCE_PUBLICATION // pending deprecation
471
472 // DEPRECATED METHODS
473
474 /// @deprecated replaced by `utcDatetime.`
475 ///
476 /// Return a `Datetime` object having the value of the UTC datetime
477 /// represented by this object. Note that if `0 != offset()`, the
478 /// returned value is equal to `localDatetime()` minus `offset()`
479 /// minutes, and `localDatetime()` otherwise.
480 Datetime gmtDatetime() const;
481
482 /// @deprecated Use @ref maxSupportedBdexVersion(int) instead.
483 ///
484 /// Return the most current BDEX streaming version number supported by
485 /// this class.
486 static int maxSupportedBdexVersion();
487
488 /// @deprecated replaced by `setDatetimeTzIfValid`.
489 ///
490 /// If the specified `localDatetime` and `offset` represent a valid
491 /// `DatetimeTz` value (see `isValid`), set the local datetime and the
492 /// time zone offset of this object to the `localDatetime` and `offset`
493 /// values respectively and return 0, leave this object unmodified and
494 /// return a non-zero value otherwise.
496
497#endif // BDE_OPENSOURCE_PUBLICATION -- pending deprecation
498
499};
500
501// FREE OPERATORS
502
503/// Return `true` if the specified `lhs` and `rhs` `DatetimeTz` objects have
504/// the same value, and `false` otherwise. Two `DatetimeTz` objects have
505/// the same value if they have the same local datetime value and the same
506/// time zone offset value.
507bool operator==(const DatetimeTz& lhs, const DatetimeTz& rhs);
508
509/// Return `true` if the specified `lhs` and `rhs` `DatetimeTz` objects do
510/// not have the same value, and `false` otherwise. Two `DatetimeTz`
511/// objects do not have the same value if they do not have the same local
512/// datetime value or the same time zone offset value.
513bool operator!=(const DatetimeTz& lhs, const DatetimeTz& rhs);
514
515/// Write the value of the specified `rhs` object to the specified output
516/// `stream` in a single-line format, and return a reference providing
517/// modifiable access to `stream`. If `stream` is not valid on entry, this
518/// operation has no effect. Note that this human-readable format is not
519/// fully specified and can change without notice. Also note that this
520/// method has the same behavior as `object.print(stream, 0, -1)`, but with
521/// the attribute names elided.
522bsl::ostream& operator<<(bsl::ostream& stream, const DatetimeTz& rhs);
523
524// FREE FUNCTIONS
525
526/// Pass the specified `object` to the specified `hashAlg`. This function
527/// integrates with the `bslh` modular hashing system and effectively
528/// provides a `bsl::hash` specialization for `DatetimeTz`. Note that two
529/// objects which represent the same UTC time but have different offsets
530/// will not (necessarily) hash to the same value.
531template <class HASHALG>
532void hashAppend(HASHALG& hashAlg, const DatetimeTz& object);
533
534// ============================================================================
535// INLINE DEFINITIONS
536// ============================================================================
537
538 // ----------------
539 // class DatetimeTz
540 // ----------------
541
542// CLASS METHODS
543inline
544bool DatetimeTz::isValid(const Datetime& localDatetime, int offset)
545{
546 return k_MIN_OFFSET < offset
547 && k_MAX_OFFSET > offset
548 && (bdlt::Time() != localDatetime.time() || 0 == offset);
549}
550
551 // Aspects
552
553inline
555{
556 if (versionSelector >= 20170401) {
557 return 2; // RETURN
558 }
559 return 1;
560}
561
562// CREATORS
563inline
565: d_localDatetime()
566, d_offset(0)
567{
568}
569
570inline
571DatetimeTz::DatetimeTz(const Datetime& localDatetime, int offset)
572: d_localDatetime(localDatetime)
573, d_offset(offset)
574{
576}
577
578inline
580: d_localDatetime(original.d_localDatetime)
581, d_offset(original.d_offset)
582{
583}
584
585inline
587{
588 BSLS_ASSERT_SAFE(isValid(d_localDatetime, d_offset));
589}
590
591// MANIPULATORS
592inline
594{
595 d_localDatetime = rhs.d_localDatetime;
596 d_offset = rhs.d_offset;
597
598 return *this;
599}
600
601inline
602void DatetimeTz::setDatetimeTz(const Datetime& localDatetime, int offset)
603{
605
606 d_localDatetime = localDatetime;
607 d_offset = offset;
608}
609
610inline
611int DatetimeTz::setDatetimeTzIfValid(const Datetime& localDatetime, int offset)
612{
615 return 0; // RETURN
616 }
617 return -1;
618}
619
620 // Aspects
621
622template <class STREAM>
623STREAM& DatetimeTz::bdexStreamIn(STREAM& stream, int version)
624{
625 if (stream) {
626 switch (version) { // switch on the schema version
627 case 2:
629 case 1: {
631 localDatetime.bdexStreamIn(stream, version);
632
633 int offset;
634 stream.getInt32(offset);
635
636 if (stream && isValid(localDatetime, offset)) {
637 d_localDatetime = localDatetime;
638 d_offset = offset;
639 }
640 else {
641 stream.invalidate();
642 }
643 } break;
644 default: {
645 stream.invalidate(); // unrecognized version number
646 }
647 }
648 }
649 return stream;
650}
651
652// ACCESSORS
653inline
655{
656 return DateTz(d_localDatetime.date(), d_offset);
657}
658
659inline
661{
662 return d_localDatetime;
663}
664
665inline
667{
668 return d_offset;
669}
670
671inline
673{
674 return TimeTz(d_localDatetime.time(), d_offset);
675}
676
677inline
679{
680 Datetime utc(d_localDatetime);
681 if (d_offset) {
682 utc.addMinutes(-d_offset);
683 }
684 return utc;
685}
686
687 // Aspects
688
689template <class STREAM>
690STREAM& DatetimeTz::bdexStreamOut(STREAM& stream, int version) const
691{
692 if (stream) {
693 switch (version) { // switch on the schema version
694 case 2:
696 case 1: {
697 d_localDatetime.bdexStreamOut(stream, version);
698 stream.putInt32(d_offset);
699 } break;
700 default: {
701 stream.invalidate(); // unrecognized version number
702 }
703 }
704 }
705 return stream;
706}
707
708#ifndef BDE_OPENSOURCE_PUBLICATION // pending deprecation
709
710// DEPRECATED METHODS
711inline
713{
714 return utcDatetime();
715}
716
717inline
722
723inline
725 int offset)
726{
728}
729
730#endif // BDE_OPENSOURCE_PUBLICATION -- pending deprecation
731
732} // close package namespace
733
734// FREE OPERATORS
735inline
736bool bdlt::operator==(const DatetimeTz& lhs, const DatetimeTz& rhs)
737{
738 return lhs.localDatetime() == rhs.localDatetime()
739 && lhs.offset() == rhs.offset();
740}
741
742inline
743bool bdlt::operator!=(const DatetimeTz& lhs, const DatetimeTz& rhs)
744{
745 return !(lhs == rhs);
746}
747
748inline
749bsl::ostream& bdlt::operator<<(bsl::ostream& stream, const DatetimeTz& rhs)
750{
751 return rhs.print(stream, 0, -1);
752}
753
754// FREE FUNCTIONS
755template <class HASHALG>
756inline
757void bdlt::hashAppend(HASHALG& hashAlg, const DatetimeTz& object)
758{
759 using ::BloombergLP::bslh::hashAppend;
760 hashAppend(hashAlg, object.localDatetime());
761 hashAppend(hashAlg, object.offset());
762}
763
764namespace bslmf {
765
766// TRAITS
767
768/// This template specialization for `IsBitwiseCopyable` indicates that
769/// `bdlt::DatetimeTz` is a bitwise copyable type.
770template <>
771struct IsBitwiseCopyable<BloombergLP::bdlt::DatetimeTz> : bsl::true_type {
772};
773
774} // close namespace bslmf
775
776
777#endif
778
779// ----------------------------------------------------------------------------
780// Copyright 2016 Bloomberg Finance L.P.
781//
782// Licensed under the Apache License, Version 2.0 (the "License");
783// you may not use this file except in compliance with the License.
784// You may obtain a copy of the License at
785//
786// http://www.apache.org/licenses/LICENSE-2.0
787//
788// Unless required by applicable law or agreed to in writing, software
789// distributed under the License is distributed on an "AS IS" BASIS,
790// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
791// See the License for the specific language governing permissions and
792// limitations under the License.
793// ----------------------------- END-OF-FILE ----------------------------------
794
795/** @} */
796/** @} */
797/** @} */
Definition bdlt_datetz.h:162
Definition bdlt_datetimetz.h:308
Datetime localDatetime() const
Definition bdlt_datetimetz.h:660
~DatetimeTz()
Destroy this object.
Definition bdlt_datetimetz.h:586
STREAM & bdexStreamOut(STREAM &stream, int version) const
Definition bdlt_datetimetz.h:690
void setDatetimeTz(const Datetime &localDatetime, int offset)
Definition bdlt_datetimetz.h:602
DateTz dateTz() const
Definition bdlt_datetimetz.h:654
DatetimeTz()
Definition bdlt_datetimetz.h:564
STREAM & bdexStreamIn(STREAM &stream, int version)
Definition bdlt_datetimetz.h:623
Datetime gmtDatetime() const
Definition bdlt_datetimetz.h:712
TimeTz timeTz() const
Definition bdlt_datetimetz.h:672
int validateAndSetDatetimeTz(const Datetime &localDatetime, int offset)
Definition bdlt_datetimetz.h:724
static int maxSupportedBdexVersion()
Definition bdlt_datetimetz.h:718
static bool isValid(const Datetime &localDatetime, int offset)
Definition bdlt_datetimetz.h:544
DatetimeTz & operator=(const DatetimeTz &rhs)
Definition bdlt_datetimetz.h:593
int offset() const
Definition bdlt_datetimetz.h:666
Datetime utcDatetime() const
Definition bdlt_datetimetz.h:678
int setDatetimeTzIfValid(const Datetime &localDatetime, int offset)
Definition bdlt_datetimetz.h:611
bsl::ostream & print(bsl::ostream &stream, int level=0, int spacesPerLevel=4) const
Definition bdlt_datetime.h:331
Date date() const
Return the value of the "date" part of this object.
Definition bdlt_datetime.h:2164
Datetime & addMinutes(bsls::Types::Int64 minutes)
Definition bdlt_datetime.h:1992
Time time() const
Return the value of the "time" part of this object.
Definition bdlt_datetime.h:2275
STREAM & bdexStreamOut(STREAM &stream, int version) const
Definition bdlt_datetime.h:2297
STREAM & bdexStreamIn(STREAM &stream, int version)
Definition bdlt_datetime.h:2120
Definition bdlt_timetz.h:190
Definition bdlt_time.h:196
#define BSLS_ANNOTATION_FALLTHROUGH
Definition bsls_annotation.h:412
#define BSLS_ASSERT_SAFE(X)
Definition bsls_assert.h:1762
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
void hashAppend(HASH_ALGORITHM &hashAlg, const baljsn::EncoderTestAddress &object)
Definition baljsn_encoder_testtypes.h:9236
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)
Definition bdlbb_blob.h:576