Biribu
Biribu

Reputation: 3823

Convert date in string to Date changing format in C

I need to parse a string like this:

Apr 3, 2014 10:03:51 AM

to something like this:

YYYY-MM-DD HH:MM:SS

And also, this long long:

1396682344000

To the same kind of string:

YYYY-MM-DD HH:MM:SS

Is there any library or function to do that? I am not very confortable writing C and I am not used to parse this kind of strings.

I tried with strptime with this code:

observationDate_message is the like first string (Apr 3, 2014 10:45:01 AM)

strptime(observationDate_message, "%G-%m-%d %r", &result);
    debugLog(DEB_INFO, "observationDateConverted: %d-%d-%d %d:%d:%d\n", result.tm_year, result.tm_mon, result.tm_mday, result.tm_hour, result.tm_min, result.tm_sec);

And what I get is:

0-52-0 36905376:32630:1497284224

Tutorial in; http://pic.dhe.ibm.com/infocenter/iseries/v7r1m0/index.jsp?topic=%2Frtref%2Fstrpti.htm

Upvotes: 2

Views: 6384

Answers (2)

Art
Art

Reputation: 20392

Check if your system has the function strptime. It's part of POSIX and will do the parsing of the string for you. To convert in the opposite direction there's the C standard function strftime.

Upvotes: 5

LearningC
LearningC

Reputation: 3162

you can parse

Apr 3, 2014 10:03:51 AM

this string using sscanf() and get the year month date and time information. if str contains the string,

sscanf(str,"%s %d, %d %d:%d:%d AM",month,&dd,&yy,&hh,&mm,&ss);

you can get the data from string. This is just a example you can extract formatted data from string as you want using sscanf()

Upvotes: 1

Related Questions