From b78c13b23fb3fecda80afb0d731fc2c610b931ea Mon Sep 17 00:00:00 2001 From: Emile Cormier Date: Thu, 30 Jul 2015 16:02:17 -0300 Subject: [PATCH] Updated Examples and Recipes (markdown) --- Examples-and-Recipes.md | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/Examples-and-Recipes.md b/Examples-and-Recipes.md index 20f3f4b..efd3c9f 100644 --- a/Examples-and-Recipes.md +++ b/Examples-and-Recipes.md @@ -2,14 +2,34 @@ - [Obtaining a `time_point` from `y/m/d h:m:s` components](#time_point_to_components) - [Obtaining `y/m/d h:m:s` components from a `time_point`](#components_to_time_point) -=== +*** ### Obtaining a `time_point` from `y/m/d h:m:s` components +See http://stackoverflow.com/questions/31711782. -Write me +```c++ +using namespace std::chrono; +using namespace date; +// Component values (as obtained from an UI form, for example) +int y=2015, m=7, d=30, h=12, min=34, s=56; +system_clock::time_point = day_point(year(y)/m/d) + hours(h) + minutes(min) + seconds(s); +``` ### Obtaining `y/m/d h:m:s` components from a `time_point` -Write me +```c++ +using namespace date; +auto daypoint = floor(time); +auto ymd = year_month_day(daypoint); // calendar date +auto tod = make_time(time - daypoint); // Yields time_of_day type + +// Obtain individual components as integers +auto y = int(ymd.year()); +auto m = unsigned(ymd.month()); +auto d = unsigned(ymd.day()); +auto h = tod.hours().count(); +auto min = tod.minutes().count(); +auto s = tod.seconds().count(); +```