diff --git a/Examples-and-Recipes.md b/Examples-and-Recipes.md index a18e093..0b80112 100644 --- a/Examples-and-Recipes.md +++ b/Examples-and-Recipes.md @@ -44,6 +44,38 @@ auto min = tod.minutes().count(); auto s = tod.seconds().count(); ``` +Alternatively, using `struct tm` to hold the components: + +```c++ +template +std::tm to_calendar_time(std::chrono::time_point tp) +{ + using namespace date; + auto date = floor(tp); + auto ymd = year_month_day(date); + auto weekday = year_month_weekday(date).weekday_indexed().weekday(); + auto tod = make_time(tp - date); + days daysSinceJan1 = date - day_point(ymd.year()/1/1); + + std::tm result; + std::memset(&result, 0, sizeof(result)); + result.tm_sec = tod.seconds().count(); + result.tm_min = tod.minutes().count(); + result.tm_hour = tod.hours().count(); + result.tm_mday = unsigned(ymd.day()); + result.tm_mon = unsigned(ymd.month()); + result.tm_year = int(ymd.year()) - 1900; + result.tm_wday = unsigned(weekday); + result.tm_yday = daysSinceJan1.count(); + result.tm_isdst = -1; // Information not available + return result; +} + +auto t = to_calendar_time(std::chrono::system_clock::now()); +std::cout << t.tm_year << "-" << t.tm_mon << "-" << t.tm_day << " "; + << t.tm_hour << ":" << t.tm_min << ":" << t.tm_sec << "\n"; + +``` *** ![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