# Posix Time Zone

Introduction -- Important Notes -- Header -- Construction -- Accessors

Introduction

A posix_time_zone object is a set of data and rules that provide information about a time zone. Information such as the offset from UTC, it's name and abbreviation, as well as daylight savings rules, called dst_calc_rules. These rules are stored as a boost::shared_ptr<dst_calc_rules>.

As a convenience, a typedef for shared_ptr<dst_calc_rules> is provided.

    typedef boost::shared_ptr<dst_calc_rules> local_time::dst_calc_rule_ptr;

A posix_time_zone is unique in that the object is created from a Posix time zone string (IEEE Std 1003.1). A POSIX time zone string takes the form of:

          "std offset dst [offset],start[/time],end[/time]" (w/no spaces).

'std' specifies the abbrev of the time zone. 'offset' is the offset from UTC. 'dst' specifies the abbrev of the time zone during daylight savings time. The second offset is how many hours changed during DST. 'start' and 'end' are the dates when DST goes into (and out of) effect. 'offset' takes the form of:

          [+|-]hh[:mm[:ss]] {h=0-23, m/s=0-59}

'time' and 'offset' take the same form. 'start' and 'end' can be one of three forms:

          Mm.w.d {month=1-12, week=1-5 (5 is always last), day=0-6}
          Jn {n=1-365 Feb29 is never counted}
          n {n=0-365 Feb29 is counted in leap years}

Exceptions will be thrown under the following conditions:

- An exception will be thrown for an invalid date spec (see date class).
- A boost::local_time::bad_offset exception will be thrown for:
- A DST start or end offset that is negative or more than 24 hours.
- A UTC zone that is greater than +14 or less than -12 hours.
- A boost::local_time::bad_adjustment exception will be thrown for a DST adjustment that is 24 hours or more (positive or negative)

As stated above, the 'offset' and '/time' portions of the string are not required. If they are not given they default to 01:00 for 'offset', and 02:00 for both occurrences of '/time'.

Some examples are:

          "PST-8PDT01:00:00,M4.1.0/02:00:00,M10.1.0/02:00:00"
          "PST-8PDT,M4.1.0,M10.1.0"

These two are actually the same specification (defaults were used in the second string). This zone lies eight hours west of GMT and makes a one hour shift forward during daylight savings time. Daylight savings for this zone starts on the first Sunday of April at 2am, and ends on the first Sunday of October at 2am.

          "MST-7"

This zone is as simple as it gets. This zone lies seven hours west of GMT and has no daylight savings.

          "EST10EDT,M10.5.0,M3.5.0/03"

This string describes the time zone for Sydney Australia. It lies ten hours east of GMT and makes a one hour shift forward during daylight savings. Being located in the southern hemisphere, daylight savings begins on the last Sunday in October at 2am and ends on the last Sunday in March at 3am.

          "FST+3FDT02:00,J60/00,J304/02"

This specification describes a fictitious zone that lies three hours east of GMT. It makes a two hour shift forward for daylight savings which begins on March 1st at midnight, and ends on October 31st at 2am. The 'J' designation in the start/end specs signifies that counting starts at one and February 29th is never counted.

          "FST+3FDT,59,304"

This specification is significant because of the '59'. The lack of 'J' for the start and end dates, indicates that the Julian day-count begins at zero and ends at 365. If you do the math, you'll see that allows for a total of 366 days. This is fine in leap years, but in non-leap years '59' (Feb-29) does not exist. This will construct a valid posix_time_zone object but an exception will be thrown if the date of '59' is accessed in a non-leap year. Ex:

    posix_time_zone leap_day(std::string("FST+3FDT,59,304"));
    leap_day.dst_local_start_time(2004); // ok
    leap_day.dst_local_start_time(2003); // Exception thrown

The posix_time_zone objects are used via a boost::shared_ptr<local_time::time_zone_base>. As a convenience, a typedef for boost::shared_ptr<local_time::time_zone_base> is provided:

    typedef boost::shared_ptr<time_zone_base> local_time::time_zone_ptr;

See Simple time zone for a side by side example of time_zone and posix_time_zone usage.

Important Notes

- posix_time_zone objects use the standard and daylight savings abbreviations in place of the full names (see Accessors below).
- 'Jn' and 'n' date specifications can not be mixed in a specification string. Ex: "FST+3FDT,59,J304"
- 'n' date specification of 59 represents Feb-29. Do not attempt to access in a non-leap year or an exception will be thrown.

Header

The inclusion of a single header will bring in all boost::local_time types, functions, and IO operators.

    #include "boost/date_time/local_time/local_time.hpp"

Construction

Syntax | Example
--- | ---

    posix_time_zone(std::string)

    std::string nyc("EST-5EDT,M4.1.0,M10.5.0");
    time_zone_ptr zone(new posix_time_zone(nyc));

Accessors

Syntax | Description
--- | ---
Example

    std::string dst_zone_abbrev()

Returns the daylight savings abbreviation for the represented time zone.

    nyc_zone_sh_ptr->dst_zone_abbrev(); // "EDT"

    std::string std_zone_abbrev()

Returns the standard abbreviation for the represented time zone.

    nyc_zone_sh_ptr->std_zone_abbrev(); // "EST"

    std::string dst_zone_name()

Returns the daylight savings ABBREVIATION for the represented time zone.

    nyc_zone_sh_ptr->dst_zone_name(); // "EDT"

    std::string std_zone_name()

Returns the standard ABBREVIATION for the represented time zone.

    nyc_zone_sh_ptr->std_zone_name(); // "EST"

    bool has_dst()

Returns true when time_zone's shared_ptr to dst_calc_rules is not NULL.

    nyc_zone_sh_ptr->has_dst(); // true
    phx_zone_sh_ptr->has_dst(); // false

    ptime dst_local_start_time(greg_year)

The date and time daylight savings time begins in given year. Returns not_a_date_time if this zone has no daylight savings.

    nyc_zone_sh_ptr->dst_local_start_time(2004);
    // 2004-Apr-04 02:00

    ptime dst_local_end_time(greg_year)

The date and time daylight savings time ends in given year. Returns not_a_date_time if this zone has no daylight savings.

    nyc_zone_sh_ptr->dst_local_end_time(2004);
    // 2004-Oct-31 02:00

    time_duration base_utc_offset()

The amount of time offset from UTC (typically in hours).

    nyc_zone_sh_ptr->base_utc_offset(); // -05:00

    posix_time::time_duration dst_offset()

The amount of time shifted during daylight savings.

    nyc_zone_sh_ptr->dst_offset(); // 01:00

    std::string to_posix_string()

Returns a posix time zone string representation of this time_zone_base object. Depending on how the time_zone object was created, the date-spec format of the string will be in either 'M' notation or 'n' notation. Every possible date-spec that can be represented in 'J' notation can also be represented in 'n' notation. The reverse is not true so only 'n' notation is used for these types of date-specs. For a detailed description of a posix time zone string see posix_time_zone.

    nyc_zone_sh_ptr->to_posix_string();
    // "EST-05EDT+01,M4.1.0/02:00,M10.5.0/02:00"
    phx_zone_sh_ptr->to_posix_string();
    // "MST-07"