Question Details

No question body available.

Tags

c++ c++20 c++-chrono

Answers (1)

June 15, 2026 Score: 6 Rep: 226,834 Quality: High Completeness: 100%

Not necessarily. C++20 introduces local time arithmetic which can be very useful in a surprising number of circumstances. Local time arithmetic can be contrasted with UTC arithmetic to make your code more robust in the face of daylight saving time. I'm not saying local time arithmetic is better than UTC arithmetic. It is not. I'm just saying that sometimes it is the right tool for the job.

To illustrate, I have an example below that models the nightly routine of a popular BBQ restaurant. The restaurant opens at 7 in the morning, and closes at 10pm (22:00) in the evening. At closing time, two things happen:

  1. A security guard comes to work to keep the BBQ secure while it cooks for the next day.

  2. The smoking of the next day's meat begins. The meat must cook for exactly 9 hours; not 8, not 10. It should be ready when the restaurant opens at 7am the next morning.

So on a normal evening:

  • At 10pm the security guard arrives, and the meat is put into the smoker.

  • At 7am the next morning, the security guard leaves and the meat is removed from the smoker.

The security guard must be paid for each hour worked. And the meat must be cooked for exactly 9 hours and ready at 7am sharp.

Twice a year in America/NewYork the politicians mess with this carefully crafted schedule by changing the local time discontinuously at 2am. The nightly routine must accommodate these daylight saving changes so that the guard is paid for each hour worked, and the meat is not over- or under-cooked.

#include 
#include 

void nightly
routine(std::chrono::localdays dt, std::chrono::timezone const* tz) { using namespace std; using namespace chrono;

// dt is the local midnight which starts a date

// security guard works from 10pm on the previous day to 7am local time zonedtime sgend{tz, dt + 7h}; // 7am local time

// Subtract 9 hours in local time to always get 10pm the previous evening zonedtime sgbeg{tz, sgend.getlocaltime() - 9h};

// Actual number of hours worked, usually 9h, but not always auto sg
hours = sgend.getsystime() - sgbeg.getsystime();

// bbq must cook for 9h and be ready at 7am local time zonedtime bbqend{tz, dt + 7h}; // 7am local time

// Subtract 9 hours in system time to reliably get 9h of cooking time // Usually this is 10pm the previous evening, but not always zonedtime bbqbeg{tz, bbqend.getsystime() - 9h};

// Actual number of hours cooked, always 9h auto bbq
hours = bbqend.getsystime() - bbqbeg.getsystime();

cout