mirror of
https://github.com/HowardHinnant/date.git
synced 2025-08-06 22:24:27 +02:00
Added time_point to struct tm conversion function.
@@ -44,6 +44,38 @@ auto min = tod.minutes().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";
|
||||
|
||||
```
|
||||
***
|
||||
|
||||
 _This work is licensed under a [Creative Commons Attribution 4.0 International License](http://creativecommons.org/licenses/by/4.0/)._
|
Reference in New Issue
Block a user