3

I am receiving a datetime in YYYY-MM-DDThh:mm:ss[.S+][Z|+-hh:mm] this format. and i m trying to copy that value using strptime as shown below

struct tm time = {0};
char *pEnd = strptime(datetime, "%Y-%m-%dT%H:%M:%S%Z", &time);

But I can't copy the fraction of seconds as strptime doesn't support it in C++.

So what should i do?

I found a solution using gettimeofday(). but I am already getting date and time in 'datetime' so please help me to find soluntion for it...I can use poco also . but even their we take local time.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
user3913114
  • 105
  • 3
  • 11
  • You can count offset in char array and make a sub-string. – ST3 Aug 06 '14 at 07:10
  • 3
    Since `struct tm` does not have a field for [milliseconds]( http://www.cplusplus.com/reference/ctime/tm/) strptime will not support fractions of a second in C either. – Lev Landau Aug 06 '14 at 07:26

2 Answers2

0

In C++11 you can use the the high precision timing objects in <chrono>, see the answers to this question.

Community
  • 1
  • 1
Lev Landau
  • 788
  • 3
  • 16
  • Thank you for response. But my problem is i cant take system time or local time. I am already getting the datetime which i ve to represent and sendd it to *pEnd as i shown in above code – user3913114 Aug 06 '14 at 08:15
0

You can remove the "S" component from the string before parsing with strptime, then use it however you like later (it won't fit anywhere in a Standard struct tm - there's no sub-second fields). Just datetime.find('.') and go from there, or use a regexp if you prefer - both tedious but not rocket science.

Tony Delroy
  • 102,968
  • 15
  • 177
  • 252
  • thank you Tony.But my problem is i ll convert everything in &time to seconds since epoch using ->time_t t = mktime ( &time ). so if i remove S, how should i add it later?.pls help me – user3913114 Aug 06 '14 at 09:15
  • @user3913114 well, it depends what you want to do with your `time_t t`... if you want to get subsecond differences between such values, you can change to `double t = mktime(&time) + s;` or `uint64_t t_ms = mktime(&time) * 1000 + s;` or create a `struct T { time_t t; int ms; };` then you can compare and calculate deltas on such values. If you want to print the time later in some format, inject the microsecond back in at the appropriate place based on your output function. If you have some other need and still can't see how to meet it, let us know.... – Tony Delroy Aug 06 '14 at 09:37
  • With time_t t, i will return (t*1000000000L) to the function.100000000L is to convert it it nano seconds. – user3913114 Aug 06 '14 at 13:46
  • @user3913114: well, all simple then... just scale appropriately based on however-many digits S has.... – Tony Delroy Aug 07 '14 at 01:00
  • @user3913114 you're welcome - glad to hear it's sorted. Cheers. – Tony Delroy Aug 08 '14 at 05:03