Added time_point to struct tm conversion function.

Emile Cormier
2015-07-31 18:52:33 -03:00
parent 9c0c369b36
commit 59ffa1042f

@@ -44,6 +44,38 @@ auto min = tod.minutes().count();
auto s = tod.seconds().count(); auto s = tod.seconds().count();
``` ```
Alternatively, using `struct tm` to hold the components:
```c++
template <typename Clock, typename Duration>
std::tm to_calendar_time(std::chrono::time_point<Clock, Duration> tp)
{
using namespace date;
auto date = floor<days>(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/)._ ![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/)._