Updated Examples and Recipes (markdown)

Howard Hinnant
2016-09-05 14:46:55 -04:00
parent c076ae6fc1
commit 5f03382cee

@@ -24,6 +24,7 @@ This page contains examples and recipes contributed by community members. Feel f
- [How many timezones are using daylight saving?](#tz_daylight)
- [What is the epoch difference between Unix Time and GPS time?](#unix_gps_epoch_diff)
- [How to convert to/from C++ Builder's TDate and TDateTime](#TDate)
- [How to convert to/from QDate](#QDate)
***
@@ -1215,6 +1216,30 @@ which outputs:
-1.25
35065
<a name="QDate"></a>
### How to convert to/from QDate
(by [Howard Hinnant](https://github.com/HowardHinnant))
Here are functions you can use to convert between [`QDate`](http://doc.qt.io/qt-5/qdate.html) and `sys_days`:
date::sys_days
to_sys_days(QDate qd)
{
using namespace date;
return sys_days{days{qd.toJulianDay()} -
(sys_days{1970_y/jan/1} - sys_days{year{-4713}/nov/24})};
}
QDate
to_QDate(date::sys_days sd)
{
using namespace date;
return QDate::fromJulianDay((sd.time_since_epoch() +
(sys_days{1970_y/jan/1} - sys_days{year{-4713}/nov/24})).count());
}
These work by simply adjusting the epoch of these two types.
***
![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/)._