Reputation: 93
My teacher said that i have to convert the file with lines like that:
Jan Kowalski 1997 4 3
to class person with pools like:
string name, surname;
std::chrono::system_clock::time_point dateofbirth;
How can i create time point from 3 integers? I suppose it is not the easiest way to store that kind of data.
"A reference to a specific point in time, like one's birthday, today's dawn, or when the next train passes. In this library, objects of the time_point class template express this by using a duration relative to an epoch (which is a fixed point in time common to all time_point objects using the same clock)."
But how do i make duration from those data?
I suppose i should start with something like:
using std::chrono::system_clock;
system_clock::time_point today = system_clock::now();
std::chrono::duration<int, std::ratio<60 * 60 * 24> > one_day;
Upvotes: 4
Views: 3433
Reputation: 1150
// create tm with 1/1/2000:
std::tm timeinfo = std::tm();
timeinfo.tm_year = 100; // year: 2000
timeinfo.tm_mon = 0; // month: january
timeinfo.tm_mday = 1; // day: 1st
std::time_t tt = std::mktime (&timeinfo);
system_clock::time_point tp = system_clock::from_time_t (tt);
Example is here to convert the std::tm to time_point: http://www.cplusplus.com/reference/chrono/system_clock/from_time_t/
Upvotes: 1
Reputation: 218750
Here are several low-level date algorithms which can be used to convert three integers into a std::chrono::system_clock::time_point
:
You can view this paper as a cookbook for how to write the algorithms portion of a date class.
Here is an example date class based on these algorithms. It contains a type-alias called days
that is quite similar to your one_day
.
Here is a time zone library built on top of date
that includes a parse
function that can be used to parse dates (and times) out of files:
As this is homework, you may just want to stick with the underlying algorithms paper.
Upvotes: 3
Reputation: 586
You can start by storing the birth date in std::tm (http://en.cppreference.com/w/cpp/chrono/c/tm), a structure that holds a date and time broken down into its components. For example:
std::tm tm;
tm.tm_mday = 3;
tm.tm_mon = 4;
tm.tm_year = 1977;
This std::tm structure can be converted to std::time_t (http://en.cppreference.com/w/c/chrono/time_t), which holds the number of seconds since epoch.
std::time_t tt = timegm(&tm);
Which in turn can be used to create the std::chrono::system_clock::time_point you are looking for:
std::chrono::system_clock::time_point dateofbirth = std::chrono::system_clock::from_time_t(tt);
Upvotes: 5