BDE 4.14.0 Production release
|
Provide a representation of a date and time with time zone offset.
This component provides a single value-semantic class, bdlt::DatetimeTz
, that represents a datetime value in a particular time zone. Each bdlt::DatetimeTz
object contains a time zone offset from UTC (in minutes) and a bdlt::Datetime
value in that time zone. For logical consistency, the datetime value and offset should correspond to a geographically valid time zone, but such consistency is the user's responsibility. This component does not enforce logical constraints on any values.
A bdlt::DatetimeTz
value is intended to be interpreted as a value in a local time zone, along with the offset of that value from UTC. However, there are some problems with this simple interpretation. First of all, the offset value may not correspond to any time zone that has ever existed. For example, the offset value could be set to one minute, or to 1,234 minutes. The meaning of the resulting "local datetime" value is always clear, but the local datetime might not correspond to any geographical or historical time zone.
The second problem is more subtle. A given offset from UTC might be "valid" in that it corresponds to a real time zone, but the actual datetime value might not exist in that time zone. To make matters worse, a "valid" offset may not (indeed, rarely will) specify one time zone uniquely. Moreover, the datetime value might be valid in one time zone corresponding to a given offset, and not in another time zone.
For these reasons (and others), this component cannot and does not perform any validation relating to time zones or offsets. The user must take care to honor the "local datetime" contract of this component.
A common standard text representation of a date and time value is described by ISO 8601. BDE provides the bdlt_iso8601util component for conversion to and from the standard ISO8601 format.
This section illustrates intended use of this component.
This example demonstrates how to create and use a bdlt::DatetimeTz
object.
First, create an object dt1
having the default value, and then verify that it contains an offset of 0, implying that the object represents a date and time in the UTC time zone, and the value of the datetime is the same as that of a default constructed bdlt::Datetime
object:
Then, set dt1
to the value 12:00 noon (12:00:00.000) on 12/31/2005 in the EST time zone (UTC-5):
Next, create dt2
as a copy of dt1
:
Now, create a third object, dt3
, representing the time 10:33:25.000 on 01/01/2001 in the PST time zone (UTC-8):
Finally, stream the values of dt1
, dt2
, and dt3
to stdout
:
The streaming operator produces the following output on stdout
:
Let us suppose that we are implementing a delivery estimation system for a shipping company. The system provides estimated delivery dates and times of client shipments. This information is provided in the local time zone and is represented as a bdlt::DatetimeTz
object. Below is the definition for a struct that returns the estimated delivery date.
All the relevant data used for delivery estimation is stored in a lookup table as shown below:
And here are the function definitions:
When we print out the delivery times:
We get the following results: