43. Meeting time for multiple time zones

To work with time zones, you must use the tz.h header of the date library. However, this needs the IANA Time Zone Database to be downloaded and uncompressed on your machine.

This is how to prepare the time zone database for the date library:

  • Download the latest version of the database from https://www.iana.org/time-zones. Currently, the latest version is called tzdata2017c.tar.gz.
  • Uncompress this to any location on your machine, in a subdirectory called tzdata. Let's suppose the parent directory is c:workchallengeslibsdate (on a Windows machine); this will have a subdirectory called tzdata.
  • For Windows, you need to download a file called windowsZones.xml, containing mappings of Windows time zones to IANA time zones. This is available at https://unicode.org/repos/cldr/trunk/common/supplemental/windowsZones.xml. The file must be stored in the same tzdata subdirectory created earlier.
  • In your project settings, define a preprocessor macro called INSTALL that indicates the parent directory for the tzdata subdirectory. For the example given here, you should have INSTALL=c:\work\challenges\libs\date. (Note that the double backslash is necessary because the macro is used to create a file path using stringification and concatenation, and would otherwise result in an incorrect path.)

To solve this problem, we will consider a user structure with minimal information, such as name and time zone. The time zone is created using the date::locate_zone() function:

struct user
{
std::string Name;
date::time_zone const * Zone;

explicit user(std::string_view name, std::string_view zone)
: Name{name.data()}, Zone(date::locate_zone(zone.data()))
{}
};

A function that displays a list of users and their local time for the start of a meeting should transform the given time from a reference zone to the time in their own zone. To do that, we can use a conversion constructor of the date::zoned_time class:

template <class Duration, class TimeZonePtr>
void print_meeting_times(
date::zoned_time<Duration, TimeZonePtr> const & time,
std::vector<user> const & users)
{
std::cout
<< std::left << std::setw(15) << std::setfill(' ')
<< "Local time: "
<< time << std::endl;

for (auto const & user : users)
{
std::cout
<< std::left << std::setw(15) << std::setfill(' ')
<< user.Name
<< date::zoned_time<Duration, TimeZonePtr>(user.Zone, time)
<< std::endl;
}
}

This function can be used as follows, where the given time (hour and minute) is represented in the current time zone:

int main()
{
std::vector<user> users{
user{ "Ildiko", "Europe/Budapest" },
user{ "Jens", "Europe/Berlin" },
user{ "Jane", "America/New_York" }
};

unsigned int h, m;
std::cout << "Hour:"; std::cin >> h;
std::cout << "Minutes:"; std::cin >> m;

date::year_month_day today =
date::floor<date::days>(ch::system_clock::now());

auto localtime = date::zoned_time<std::chrono::minutes>(
date::current_zone(),
static_cast<date::local_days>(today)+ch::hours{h}+ch::minutes{m});

print_meeting_times(localtime, users);
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset