Customizing time_zone


The os_time_zone class contains static functions for accessing the most common time zones. You can create a time zone using one of the following methods.

Constructing a Time Zone Without Daylight Savings

To create a time zone with no daylight savings rules, construct a time zone with two parameters: the offset relative to utc and the name of the time zone.

Constructing a Time Zone With Daylight Savings

Perform the following steps to create a time zone with one or more daylight savings rules.

  1. Construct a time zone with three parameters: offset relative to utc, name of the time zone, and name of the time zone when daylight savings rules are in effect.
  2. Create the time zone rules and add them to the time zone using insert().

There are three classes related to time zone rules.

os_time_zone_rule abstract class of all time zone rules. Unless you wish to create your own time zone rule, you do not need this class.

os_calendar_date specifies a particular day in the year in a flexible manner. For example, you can construct an os_calendar_date to represent "the third of August" or "the second Tuesday in the month of May."

os_simple_time_zone_rule day that is derived from os_time_zone_rule and adjusts all times between two calendar dates in a range of years.

For example, the following code creates a new time zone for "Recursion Software Time (ost)," which is two hours behind utc. In addition, "Recursion Software Daylight Savings Time (odt)" adds one hour to standard time between the third Monday of March and the 21st of July, between the years of 1800 and 2000.

Example <ospace/time/examples/tzone3.cpp>
#include <iostream>
#include <ospace/time.h>

void main()
  {
  os_time_toolkit initialize;

  os_time_and_date::default_format( "%B %d %y (%Z), %H:%M" );
  os_time_zone zone( 7200, "OST", "ODT" );
  cout << "zone = " << zone << endl;
  cout << "zone.offset() = " << zone.offset() << endl;
  os_simple_time_zone_rule rule
    (
    os_calendar_date( 0, os_date::march, os_third, os_date::monday ),
    os_calendar_date( 0, os_date::july, 21 ), // End.
    1800, // Start year.
    2000, // End year.
    os_time_period( 0, 1, 0, 0) // One hour.
    );
  zone.insert( rule );
  cout << "has dst = " << zone.dst_observed() << endl;
  os_time_and_date now = os_time_and_date::now();
  cout << "local time = " << now << endl;
  cout << "local time is dst = " << now.is_dst() << endl;
  cout << "local time zone = " << now.time_zone() << endl;
  now.time_zone( zone );
  cout << "time = " << now << endl;
  cout << "time is dst = " << now.is_dst() << endl;
  cout << "time zone = " << now.time_zone() << endl;
  }

zone = os_time_zone( OST2 )
zone.offset() = 7200 seconds
has dst = 1
local time = August 10 95 (CDT5), 11:45
local time is dst = 1
local time zone = os_time_zone( CST6, CDT5 )
time = August 10 95 (OST2), 14:45
time is dst = 0
time zone = os_time_zone( OST2, ODT1 )

The local time zone is six hours behind utc, and daylight savings time is in effect. The new time zone is two hours behind utc, and daylight savings time is not in effect. Therefore, the difference between the old time and the new time is three hours.


Copyright©1994-2026 Recursion Software LLC
All Rights Reserved - For use by licensed users only.