diff --git a/Examples-and-Recipes.md b/Examples-and-Recipes.md index d62208b..8aab121 100644 --- a/Examples-and-Recipes.md +++ b/Examples-and-Recipes.md @@ -30,6 +30,7 @@ This page contains examples and recipes contributed by community members. Feel f - [Parsing unambiguous date time inside daylight transition](#parse_daylight_transition) - [`microfortnights`?! Are you serious?](#microfortnights) - [Find and set UNIX download folder thanks to xdg-user-dir](#set_install_with_xdg_user_dir) +- [Re-using unzoned date/time code for zoned date/time values (and vice versa)](#zoned2unzoned) *** @@ -1927,4 +1928,31 @@ Do not forget to set `-DAUTO_DOWNLOAD=1` so that you download the tzdata in the *** -![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 + +### Re-using unzoned date/time code for zoned date/time values (and vice versa) +(by [Tai Meng](https://github.com/ta1meng)) + +The date/time libraries forbid implicit conversions between local_time and sys_time, and for good reason. If we type our variables using "auto" everywhere, sooner than later we'd run into a case where we'd be assigning a local_time to a sys_time or vice versa, and the compiler would catch this dangerous conversion. Thank you type safety! + +However at times, to re-use existing code, we actually want to convert between local_time and sys_time. If this is what you wish to achieve, feel free to skip the story below, and read the solution that follows it. + +\ + +Suppose you've written a layer of code to handle unzoned date/time addition with ISO durations of the form P1DT2H (a period of 1 day and 2 hours. Reference: https://en.wikipedia.org/wiki/ISO_8601#Durations). In your code, you would have used local_time everywhere as the date/time type. + +Now a new user requirement comes in. Users want zoned date/time addition with ISO durations. You look at the code and begin to frown. Some of your functions return local_time, so the compiler forbids you to overload these functions with the return type sys_time. Ok, you can copy paste existing functions and rename them so that they return sys_time. But now you'd need to maintain two parallel branches of code, which would be more susceptible to defects. Wouldn't it be nice if you could strip the zoned date/time down to an unzoned date/time, re-use existing code, then re-append the timezone information to the output date/time value? + +\ + +Here is how we can convert between local_time and sys_time: + +``` +const some_local_time unzonedTime{utcTime.time_since_epoch()}; +const some_sys_time utcTime{unzonedTime.time_since_epoch()}; +``` + +Where some_local_time and some_sys_time are template instantiations of local_time and sys_time. + +*** + +![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/)._