diff --git a/Examples-and-Recipes.md b/Examples-and-Recipes.md index d778bf1..c0ed211 100644 --- a/Examples-and-Recipes.md +++ b/Examples-and-Recipes.md @@ -11,6 +11,7 @@ This page contains examples and recipes contributed by community members. Feel f - [2Gs Birthday](#birthday2gs) - [Calculating Ordinal Dates](#year_day) - [Find all instances when a daylight savings shift is not 1 hour](#tz_search) +- [How many timezones are using daylight saving?](#tz_daylight) *** @@ -420,7 +421,44 @@ Sample output of this program: Africa/Accra has a daylight savings offset of 20min from 1920-09-01 00:00:00 UTC to 1920-12-30 23:40:00 UTC with the abbreviation GHST ... + +### How many timezones are using daylight saving? +(by [Howard Hinnant](https://github.com/HowardHinnant)) +Ever wonder how the global use of daylight saving time is trending with time? Here's one way to find out: + + #include "tz.h" + #include + + int + main() + { + using namespace date; + using namespace std::chrono_literals; + auto& db = get_tzdb(); + std::cout << db.version << '\n'; + for (auto y = 1850_y; y < 2020_y; ++y) + { + auto use_daylight = 0; + auto total = 0; + for (auto& z : db.zones) + { + ++total; + auto info1 = z.get_info(day_point{y/jan/15}, tz::utc); + auto info2 = z.get_info(day_point{y/jul/15}, tz::utc); + if (info1.save != 0min || info2.save != 0min) + ++use_daylight; + } + std::cout << y << " : " << use_daylight << '/' << total + << " = " << static_cast(use_daylight)/total << '\n'; + } + } + +This code loops over a wide range of years, and then for each year, loops over all timezones in the database, and for each timezone, detects whether it is switching back and forth between standard time and daylight time for that year. The switch detection is rather crude, but you can make this detection as elaborate as you want. Currently it picks two dates 6 months apart which are unlikely to both be using standard time if the zone is using daylight saving time that year. For each of those two dates, the `Info.save` member is checked. If either `save != 0min`, daylight saving is in use that year. + +The results of this program are plotted below (the plotting code is not part of the above program). + +![Plot of daylight saving use by year](https://github.com/HowardHinnant/HowardHinnant.github.io/blob/master/tz_year.jpg) *** ![CC BY Logo](http://mirrors.creativecommons.org/presskit/buttons/80x15/svg/by.svg) _This work is licensed under a [Creative Commons Attribution 4.0 International License](http://creativecommons.org/licenses/by/4.0/)._ \ No newline at end of file