TimelineCpp20CoreLanguage

Time Zones: Details

Today, I present the time-zones functionality of the C++20 extension.

This post is the seventh in my detailed journey through the chrono extension in C++20:

Times Zones

First, a time zone is a region and its entire date history, such as daylight saving time or leap seconds.

Challenges

Dealing with time zones has a few inherent challenges.

  • Wintertime and summertime: Many European countries, such as Germany, use summertime (daylight saving time) and wintertime. The daylight saving
    time is one hour ahead of the wintertime in Germany.
  • More time zones: Countries like China or the United States have different time
    zones. For example, in the United States, between the Hawaii Standard Time
    (UTC-10) and the Easter Daylight Time (UTC-4) is a difference of six hours.
  • Time zone differences: Time zone differences are often fractions of hours, such
    as 30 or 45 minutes. The Australian Central Time is UTC+9:30, and the Australian
    Central Western Standard Time is UTC+8:45.
  • Time zone abbreviations are ambiguous: The time zone abbreviations are not
    unique. ADT can be Arabic Daylight Time (UTC+4) or Atlantic Daylight Time (UTC-3).

The time-zone library in C++20 is a complete parser of the IANA timezone database. The following table should give you a first idea of the new functionality.

The use of the time zone database requires an operating system. Consequently, using the time zone database on a freestanding system typically results in an exception. The time-zone database is updated during the operating system’s update, such as a reboot. When your system supports updating the IANA time-zone database without rebooting, you can use std::chrono::reload_tzdb(). The new database is atomically added to the front of the linked list. Calls such as std::chrono::get_tzdb_list() or std::chrono::get_tzdb() parse the front of the list. Consequently, the database queries get the updated database entries. std::chrono::get_tzdb().version returns the version of the used database.


The two elementary types for time zones are std::chrono::time_zone and std::chrono::zoned_time.


The possible time zones are predefined by the IANA time-zone database. The calls std::chrono::current_zone(), and std::chrono::locate_zone(name) return a pointer to the current or by name requested time zone. The call std::chrono::locate_zone(name) causes a search for the name in the database. If the search is unsuccessful, you get a std::runtime_error exception.


std::chrono::zoned_time() represents a time zone combined with a time point. You can use a system time point, or a local time point as time point. A system time point uses std::chrono::system_clock and a local time point uses the pseudo clock std::chrono::local_t.

My first example is straightforward. It displays the UTC time and the local time.

 

Rainer D 6 P2 500x500Modernes C++ Mentoring

Be part of my mentoring programs:

  • "Fundamentals for C++ Professionals" (open)
  • "Design Patterns and Architectural Patterns with C++" (open)
  • "C++20: Get the Details" (open)
  • "Concurrency with Modern C++" (starts March 2024)
  • Do you want to stay informed: Subscribe.

     

    UTC Time and Local Time

    The UTC or Coordinated Univeral Time is the primary time standard worldwide. A computer uses Unix time, which is a very close approximation of UTC. The UNIX time is the number of seconds since the Unix epoch. The Unix epoch is 00:00:00 UTC on 1 January 1970.

    std::chrono::system_clock::now() inline (1) returns in the following program localTime.cpp the Unix time.

    // localTime.cpp
    
    #include <chrono>
    #include <iostream>
    
    int main() {
    
        std::cout << '\n';
    
        using std::chrono::floor;
     
        std::cout << "UTC  time" << '\n';                             // (1)             
        auto utcTime = std::chrono::system_clock::now();
        std::cout << "  " << utcTime << '\n';
        std::cout << "  " << floor<std::chrono::seconds>(utcTime) << '\n';
    
        std::cout << '\n';
        
        std::cout << "Local time" << '\n';                           // (2)                
        auto localTime = std::chrono::zoned_time(std::chrono::current_zone(), utcTime);
    
        std::cout << "  " << localTime << '\n';
        std::cout << "  " << floor<std::chrono::seconds>(localTime.get_local_time()) 
                          << '\n';
    
        auto offset = localTime.get_info().offset;                 // (3)    
        std::cout << "  UTC offset: "  << offset << '\n';
    
        std::cout << '\n';
    
    }
    

    I have not added too much to the program. The code block beginning with line (1) gets the current time point, truncates it to seconds, and displays it. The call std::chrono::zoned_time creates  std::chrono::zoned_time localTime. The following call localTime.get_local_time() returns the stored time point as a local time. This time point is also truncated to seconds. localTime (line 3) can also be used to get information about the time zone. In this case, I’m interested in the offset to the UTC.

    What’s next?

    My next example answers a crucial question when I teach in a different time zone: When should I start my online class?

    Thanks a lot to my Patreon Supporters: Matt Braun, Roman Postanciuc, Tobias Zindl, G Prvulovic, Reinhold Dröge, Abernitzke, Frank Grimm, Sakib, Broeserl, António Pina, Sergey Agafyin, Андрей Бурмистров, Jake, GS, Lawton Shoemake, Jozo Leko, John Breland, Venkat Nandam, Jose Francisco, Douglas Tinkham, Kuchlong Kuchlong, Robert Blanch, Truels Wissneth, Mario Luoni, Friedrich Huber, lennonli, Pramod Tikare Muralidhara, Peter Ware, Daniel Hufschläger, Alessandro Pezzato, Bob Perry, Satish Vangipuram, Andi Ireland, Richard Ohnemus, Michael Dunsky, Leo Goodstadt, John Wiederhirn, Yacob Cohen-Arazi, Florian Tischler, Robin Furness, Michael Young, Holger Detering, Bernd Mühlhaus, Stephen Kelley, Kyle Dean, Tusar Palauri, Juan Dent, George Liao, Daniel Ceperley, Jon T Hess, Stephen Totten, Wolfgang Fütterer, Matthias Grün, Phillip Diekmann, Ben Atakora, Ann Shatoff, Rob North, Bhavith C Achar, Marco Parri Empoli, Philipp Lenk, Charles-Jianye Chen, Keith Jeffery,and Matt Godbolt.

    Thanks, in particular, to Jon Hess, Lakshman, Christian Wittenhorst, Sherhy Pyton, Dendi Suhubdy, Sudhakar Belagurusamy, Richard Sargeant, Rusty Fleming, John Nebel, Mipko, Alicja Kaminska, Slavko Radman, and David Poole.

    My special thanks to Embarcadero
    My special thanks to PVS-Studio
    My special thanks to Tipi.build 
    My special thanks to Take Up Code
    My special thanks to SHAVEDYAKS

    Seminars

    I’m happy to give online seminars or face-to-face seminars worldwide. Please call me if you have any questions.

    Standard Seminars (English/German)

    Here is a compilation of my standard seminars. These seminars are only meant to give you a first orientation.

    • C++ – The Core Language
    • C++ – The Standard Library
    • C++ – Compact
    • C++11 and C++14
    • Concurrency with Modern C++
    • Design Pattern and Architectural Pattern with C++
    • Embedded Programming with Modern C++
    • Generic Programming (Templates) with C++
    • Clean Code with Modern C++
    • C++20

    Online Seminars (German)

    Contact Me

    Modernes C++ Mentoring,

     

     

    0 replies

    Leave a Reply

    Want to join the discussion?
    Feel free to contribute!

    Leave a Reply

    Your email address will not be published. Required fields are marked *