Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to efficiently parse date time (boost)

I have a char * with a date string I wish to parse. In this case a very simple format: 2010-10-28T16:23:31.428226 (common ISO format).

I know with Boost I can parse this, but at a horrible cost. I have to create a string-stream, possibly a string, and then copy data back and forth. Is there any way to parse the char * without allocating any additional memory. Stack objects are fine, so is reusing a heap object.

Any easy way would also be great! ;)

Edit: I need the result in microseconds since the epoch.

like image 631
edA-qa mort-ora-y Avatar asked Aug 05 '26 22:08

edA-qa mort-ora-y


1 Answers

Sometimes plain old C is simpler. You can almost do it with strptime(...):

struct tm parts = {0};
strptime("2010-10-28T16:23:31", "%Y-%m-%dT%H:%M:%S", &parts);

Unfortunately, you'd have to grab the fractional seconds separately. I suppose you could do it with sscanf(...) too:

unsigned int year, month, day, hour, min;
double sec;
int got = sscanf(
     "2010-10-28T16:23:31.428226", 
     "%u-%u-%uT%u:%u:%lf",
     &year, &month, &day, &hour, &min, &sec
);
assert(got == 6);
like image 119
xscott Avatar answered Aug 07 '26 13:08

xscott



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!