Input: strings with date and optional time. Different representations would be nice but necessary. The strings are user-supplied and can be malformed. Examples:
"2004-03-21 12:45:33"
(I consider this the default layout)"2004/03/21 12:45:33"
(optional layout)"23.09.2004 04:12:21"
(german format, optional)"2003-02-11"
(time may be missing)
Needed Output: Seconds since Epoch (1970/01/01 00:00:00) or some other fixed point.
Bonus: Also, reading the UTC-offset of the local system time would be great.
The input is assumed to be a local time on the machine in question. The output needs to be UTC. System is Linux only (Debian Lenny and Ubuntu needed).
I have tried to use boost/date_time
, but must admit I can't wrap my head around the documentation. The following works without the needed conversion from system local time to UTC:
std::string date = "2000-01-01";
boost::posix_time::ptime ptimedate = boost::posix_time::time_from_string(date);
ptimedate += boost::posix_time::hours(Hardcoded_UTC_Offset);// where to get from?
struct tm = boost::posix_time::to_tm(ptimedate);
int64_t ticks = mktime(&mTmTime);
I think boost::date_time
can provide the needed UTC offset, but I wouldn't know how.
Although I don't know how to format a single-digit month input in boost, I can do it after the two-digit edit:
note that the seconds-from-epoch output will be assuming the date was in UTC:
You could probably use
boost::posix_time::c_time::localtime()
from#include <boost/date_time/c_time.hpp>
to get this conversion done assuming the input is in the current time zone, but it is rather inconsistent: for me, for example, the result will be different between today and next month, when daylight saving ends.boost::gregorian
has some of the stuff you need without you doing any more work:There is an example on how to use UTC offsets with
boost::posix_time
here.You can provide generation of date and time from custom input string formats using
date_input_facet
andtime_input_facet
. There is an I/O tutorial on this page that should help you get going.the simplest, portable solution is to use
scanf
:Initialize a
struct tm
with theint
values and pass it tomktime
to get a calendar time astime_t
. For timezone conversions, please see information ongmtime
.If c-style is acceptable: strptime() is the way to go, because you can specify the format and it can take locale in account:
Different layouts will have to be checked with the return value (if possible). Timezone will have to be added to by checking the system clock (localtime_r() with time(), tm_zone)