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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With