1 <?xml version=
"1.0" encoding=
"utf-8"?>
2 <!DOCTYPE library PUBLIC
"-//Boost//DTD BoostBook XML V1.0//EN"
3 "../../../tools/boostbook/dtd/boostbook.dtd">
5 <!-- Copyright (c) 2001-2005 CrystalClear Software, Inc.
6 Subject to the Boost Software License, Version 1.0.
7 (See accompanying file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
10 <section id=
"date_time.local_time.local_date_time">
11 <title>Local Date Time
</title>
13 <link linkend=
"local_date_time_intro">Introduction
</link> --
14 <link linkend=
"local_date_time_header">Header
</link> --
15 <link linkend=
"local_date_time_clock_constr">Construct From Clock
</link> --
16 <link linkend=
"local_date_time_constr">Construction
</link> --
17 <link linkend=
"local_date_time_accessors">Accessors
</link> --
18 <link linkend=
"local_date_time_operators">Operators
</link> --
19 <link linkend=
"local_date_time_tm_func">Struct tm Functions
</link>
21 <anchor id=
"local_date_time_intro" />
22 <bridgehead renderas=
"sect3">Introduction
</bridgehead>
24 A local_date_time object is a point in time and an associated time zone. The time is represented internally as UTC.
27 <anchor id=
"local_date_time_header" />
28 <bridgehead renderas=
"sect3">Header
</bridgehead>
30 The inclusion of a single header will bring in all boost::local_time types,
31 functions, and IO operators.
33 #include
"boost/date_time/local_time/local_time.hpp"
37 <anchor id=
"local_date_time_clock_constr" />
38 <bridgehead renderas=
"sect3">Construct From Clock
</bridgehead>
40 Creation of a local_date_time object from clock is possible with either second, or sub second resolution.
43 <informaltable frame=
"all">
48 <entry>Example
</entry>
53 <entry valign=
"top"><screen>local_microsec_clock(...)
57 time_zone_ptr
</screen></entry>
58 <entry><screen>time_zone_ptr zone(
59 new posix_time_zone(
"MST-07")
62 local_microsec_clock::local_time(
67 <entry valign=
"top"><screen>local_sec_clock(...)
71 time_zone_ptr
</screen></entry>
72 <entry><screen>time_zone_ptr zone(
73 new posix_time_zone(
"MST-07")
76 local_sec_clock::local_time(zone);
</screen>
84 <anchor id=
"local_date_time_constr" />
85 <bridgehead renderas=
"sect3">Construction
</bridgehead>
87 Construction of a local_date_time object can be done with a ptime and a time_zone_ptr where the ptime represents UTC time. Construction with a wall-clock representation takes the form of a date, a time_duration, a time_zone_ptr, and a fourth parameter that addresses the following complication.
90 Construction from a wall-clock rep may result in differing shifts for a particular time zone, depending on daylight savings rules for that zone. This means it is also possible to create a local_date_time with a non-existent, or duplicated, UTC representation. These cases occur during the forward shift in time that is the transition into daylight savings and during the backward shift that is the transition out of daylight savings. The user has two options for handling these cases: a bool flag that states if the time is daylight savings, or an enum that states what to do when either of these cases are encountered.
93 The bool flag is ignored when the given time_zone has no daylight savings specification. When the daylight savings status of a given time label is calculated and it does not match the flag, a
<code>local_time::dst_not_valid
</code> exception is thrown. If a time label is invalid (does not exist), a
<code>local_time::time_label_invalid
</code> exception is thrown.
96 There are two elements in the
<code>local_date_time::DST_CALC_OPTIONS
</code> enum:
<code>EXCEPTION_ON_ERROR
</code> and
<code>NOT_DATE_TIME_ON_ERROR
</code>. The possible exceptions thrown are a
<code>local_time::ambiguous_result
</code> or a
<code>local_time::time_label_invalid
</code>. The
<code>NOT_DATE_TIME_ON_ERROR
</code> sets the time value to the special value
<code>local_time::not_a_date_time
</code> in the event of either a invalid or an ambiguous time label.
99 <informaltable frame=
"all">
103 <entry morerows=
"1">Syntax
</entry>
104 <entry>Description
</entry>
107 <entry>Example
</entry>
112 <entry valign=
"top" morerows=
"1"><screen>local_date_time(...)
115 time_zone_ptr
</screen></entry>
116 <entry>The given time is expected to be UTC. Therefore, the given time will be adjusted according to the offset described in the time zone.
</entry>
120 //
3am,
2004-Nov-
05 local time
121 ptime pt(date(
2004,Nov,
5),
124 new posix_time_zone(
"MST-07"));
125 local_date_time az(pt, zone);
</screen>
130 <entry valign=
"top" morerows=
"1"><screen>local_date_time(...)
135 bool
</screen></entry>
136 <entry>The passed time information understood to be in the passed tz. The DST flag must be passed to indicate whether the time is in daylight savings or not. May throw a
<code>dst_not_valid
</code> or
<code>time_label_invalid
</code> exception.
</entry>
139 <entry><screen>date d(
2004,Nov,
5);
140 time_duration td(
5,
0,
0,
0);
141 string z(
"PST-8PDT,M4.1.0,M10.1.0")
143 new posix_time_zone(z));
144 local_date_time nyc(d, td,
145 zone, false);
</screen>
150 <entry valign=
"top" morerows=
"1"><screen>local_date_time(...)
155 DST_CALC_OPTIONS
</screen></entry>
156 <entry>The passed time information understood to be in the passed tz. The DST flag is calculated according to the specified rule. May throw a
<code>ambiguous_result
</code> or
<code>time_label_invalid
</code> exception.
</entry>
159 <entry><screen>date d(
2004,Nov,
5);
160 time_duration td(
5,
0,
0,
0);
161 string z(
"PST-8PDT,M4.1.0,M10.1.0")
163 new posix_time_zone(z));
164 local_date_time nyc(d, td, zone,
165 NOT_DATE_TIME_ON_ERROR);
</screen>
170 <entry valign=
"top" morerows=
"1"><screen>local_date_time(local_date_time);
</screen></entry>
171 <entry>Copy Constructor.
</entry>
174 <entry><screen>local_date_time az_2(az);
</screen></entry>
178 <entry valign=
"top" morerows=
"1"><screen>local_date_time(...)
181 time_zone_ptr
</screen></entry>
182 <entry>Special Values constructor.
</entry>
185 <entry><screen>time_zone_ptr zone(
186 new posix_time_zone(
"MST-07")
188 local_date_time nadt(not_a_date_time,
190 // default NULL time_zone_ptr
191 local_date_time nadt(pos_infin);
</screen>
200 <anchor id=
"local_date_time_accessors" />
201 <bridgehead renderas=
"sect3">Accessors
</bridgehead>
203 <informaltable frame=
"all">
207 <entry valign=
"top" morerows=
"1">Syntax
</entry>
208 <entry>Description
</entry>
211 <entry>Example
</entry>
216 <entry valign=
"top" morerows=
"1"><screen>time_zone_ptr zone()
</screen></entry>
217 <entry>Returns associated time_zone object via a time_zone_ptr
</entry>
220 <entry><screen></screen></entry>
224 <entry valign=
"top" morerows=
"1"><screen>bool is_dst()
</screen></entry>
225 <entry>Determines if time value is in DST for associated zone.
</entry>
232 <entry valign=
"top" morerows=
"1"><screen>ptime utc_time()
</screen></entry>
233 <entry>Converts the local time value to a UTC value.
</entry>
236 <entry><screen>ptime pt(date(
2004,Nov,
5),
239 new posix_time_zone(
"MST-07"));
240 local_date_time az(pt, zone);
241 az.utc_time(); //
10am
2004-Nov-
5</screen>
246 <entry valign=
"top" morerows=
"1"><screen>ptime local_time()
</screen></entry>
247 <entry>Returns the local time for this object (Wall-clock).
</entry>
250 <entry><screen>ptime pt(date(
2004,Nov,
5),
253 new posix_time_zone(
"MST-07"));
254 local_date_time az(pt, zone);
255 az.utc_time(); //
10am
2004-Nov-
5
256 az.local_time(); //
3am
2004-Nov-
5</screen></entry>
260 <entry valign=
"top" morerows=
"1"><screen>local_time_in(...)
265 time_duration
</screen></entry>
266 <entry>Returns a local_date_time representing the same UTC time as calling object, plus optional time_duration, with given time zone.
</entry>
269 <entry><screen>local_date_time nyc = az.local_time_in(nyc_zone);
270 // nyc ==
7am
2004-Nov-
5</screen>
275 <entry valign="top" morerows="1"><screen>std::string zone_name()</screen></entry>
276 <entry>Returns full zone name from associated time zone or "Coordinated Universal Time" if time_zone_ptr is NULL.</entry>
284 <entry valign="top" morerows="1"><screen>std::string zone_abbrev()</screen></entry>
285 <entry>Returns zone abbreviation from associated time zone or "UTC" if time_zone_ptr is NULL.</entry>
293 <entry valign=
"top" morerows=
"1"><screen>bool is_infinity() const
</screen></entry>
294 <entry>Returns true if local_date_time is either positive or negative infinity
</entry>
297 <entry><screen>local_date_time ldt(pos_infin);
298 ldt.is_infinity(); // --> true
</screen></entry>
302 <entry valign=
"top" morerows=
"1"><screen>bool is_neg_infinity() const
</screen></entry>
303 <entry>Returns true if local_date_time is negative infinity
</entry>
306 <entry><screen>local_date_time ldt(neg_infin);
307 ldt.is_neg_infinity(); // --> true
</screen></entry>
311 <entry valign=
"top" morerows=
"1"><screen>bool is_pos_infinity() const
</screen></entry>
312 <entry>Returns true if local_date_time is positive infinity
</entry>
315 <entry><screen>local_date_time ldt(neg_infin);
316 ldt.is_pos_infinity(); // --> true
</screen></entry>
320 <entry valign=
"top" morerows=
"1"><screen>bool is_not_a_date_time() const
</screen></entry>
321 <entry>Returns true if value is not a date
</entry>
324 <entry><screen>local_date_time ldt(not_a_date_time);
325 ldt.is_not_a_date_time(); // --> true
</screen></entry>
329 <entry valign=
"top" morerows=
"1"><screen>bool is_special() const
</screen></entry>
330 <entry>Returns true if local_date_time is any
<code>special_value
</code></entry>
333 <entry><screen>local_date_time ldt(pos_infin);
334 local_date_time ldt2(not_a_date_time);
336 mst(new posix_time_zone(
"MST-07"));
338 ldt3(local_sec_clock::local_time(mst));
339 ldt.is_special(); // --> true
340 ldt2.is_special(); // --> true
341 ldt3.is_special(); // --> false
</screen></entry>
349 <anchor id=
"local_date_time_operators" />
350 <bridgehead renderas=
"sect3">Operators
</bridgehead>
351 <informaltable frame=
"all">
355 <entry valign=
"top" morerows=
"1">Syntax
</entry>
356 <entry>Description
</entry>
359 <entry>Example
</entry>
364 <entry valign=
"top" morerows=
"1"><screen>operator
<<</screen></entry>
365 <entry>Output streaming operator. This operator is part of the v1.33 IO addition to date_time. For complete details on this feature see
<link linkend=
"date_time.date_time_io">Date Time IO
</link>. The default output is shown in this example.
</entry>
369 time_zone_ptr zone(new posix_time_zone(
"MST-07");
370 local_date_time ldt(date(
2005,Jul,
4),
373 std::cout
<< ldt
<< std::endl;
374 //
"2005-Jul-04 20:00:00 MST"
379 <entry valign=
"top" morerows=
"1"><screen>operator
>></screen></entry>
380 <entry>Input streaming operator. This operator is part of the v1.33 IO addition to date_time. For complete details on this feature see
<link linkend=
"date_time.date_time_io">Date Time IO
</link>. At this time,
<code>local_date_time
</code> objects can only be streamed in with a Posix Time Zone string. A complete description of a Posix Time Zone string can be found in the documentation for the
<link linkend=
"posix_time_zone_intro">posix_time_zone
</link> class.
</entry>
383 <entry><screen>stringstream ss;
384 ss.str(
"2005-Jul-04 20:00:00 MST-07");
390 <entry valign=
"top" morerows=
"1"><screen>operator==, operator!=,
391 operator
>, operator
<,
392 operator
>=, operator
<=
</screen></entry>
393 <entry>A full complement of comparison operators
</entry>
396 <entry><screen>ldt1 == ldt2, etc
</screen></entry>
400 <entry valign=
"top" morerows=
"1"><screen>operator+, operator+=,
401 operator-, operator-=
</screen></entry>
402 <entry>Addition, subtraction, and shortcut operators for
<code>local_date_time
</code> and date duration types. These include:
<code>days
</code>,
<code>months
</code>, and
<code>years
</code>.
</entry>
405 <entry><screen>ldt + days(
5), etc
</screen></entry>
409 <entry valign=
"top" morerows=
"1"><screen>operator+, operator+=,
410 operator-, operator-=
</screen></entry>
411 <entry>Addition, subtraction, and shortcut operators for
<code>local_date_time
</code> and
<code>time_duration
</code>.
</entry>
414 <entry><screen>ldt + hours(
5), etc
</screen></entry>
420 <anchor id=
"local_date_time_tm_func" />
421 <bridgehead renderas=
"sect3">Struct tm Functions
</bridgehead>
422 <para>Function for converting a
<code>local_date_time
</code> object to a
<code>tm
</code> struct is provided.
</para>
423 <informaltable frame=
"all">
427 <entry valign=
"top" morerows=
"1">Syntax
</entry>
428 <entry>Description
</entry>
431 <entry>Example
</entry>
436 <entry valign=
"top" morerows=
"1"><screen>tm to_tm(local_date_time)
</screen></entry>
437 <entry>A function for converting a
<code>local_date_time
</code> object to a
<code>tm
</code> struct.
</entry>
441 //
6am,
2005-Jul-
05 local time
442 std::string z(
"EST-05EDT,M4.1.0,M10.1.0");
443 ptime pt(date(
2005,Jul,
5),
445 time_zone_ptr zone( new posix_time_zone(z));
446 local_date_time ldt(pt, zone);
447 tm ldt_tm = to_tm(ldt);
451 tm_wday =
> 2 (Tuesday)
456 tm_isdst =
> 1 */
</screen>