views:

95

answers:

4

Hi,

I have a series of date strings in the format: "30-05-2001"

string date1 = "30-05-2001";

I would like to parse the date into Day, Month, Year. Now an easy way of doing this would be just to call the function sscanf. But I'd like to explore other possiblilties and from searching the web the following function from time.h was recommended:

char *strptime(const char *buf, const char *format, struct tm *tm); 

Does any anyone have experience using this function? Some short example would be very helpful.

Thanks!

+1  A: 

Source:

#include <time.h>
...


struct tm tm;
time_t t;


if (strptime("6 Dec 2001 12:33:45", "%d %b %Y %H:%M:%S", &tm) == NULL)
    /* Handle error */;


printf("year: %d; month: %d; day: %d;\n",
        tm.tm_year, tm.tm_mon, tm.tm_mday);
printf("hour: %d; minute: %d; second: %d\n",
        tm.tm_hour, tm.tm_min, tm.tm_sec);
printf("week day: %d; year day: %d\n", tm.tm_wday, tm.tm_yday);


tm.tm_isdst = -1;      /* Not set by strptime(); tells mktime()
                          to determine whether daylight saving time
                          is in effect */
t = mktime(&tm);
if (t == -1)
    /* Handle error */;
printf("seconds since the Epoch: %ld\n", (long) t);"
KMan
`strptime()` is not part of C++.
Georg Fritzsche
@Georg: You're right; but this might help for windows equivalent http://stackoverflow.com/questions/321849/strptime-equivalent-on-windows/321877#321877
KMan
@Georg: Did you rather mean "not part of the ISO C++ standard library", which would be more accurate. It is not part of the ISO C standard library either for that matter. It is a fair point from a portability point of view, but since it was teh OP's suggestion, should have been made against his post perhaps? However if the target library provides it, it can be used. It would be overly restrictive to suggest that no code should use third-party libraries or standard library extensions. By that logic, the OS API would be out of bounds too!
Clifford
@cli: I meant not part of C++ as in *"standard C++"* as in the *"C++ standard"*. Ok, i overlooked that the OP did have that too... you seem to have corrected that. When posting answers however i think one should point out non-standard solutions and thats all there is to it - i didn't say *"don't post it"*.
Georg Fritzsche
@Georg: You seem to have misunderstood me. I understand your intent, but your statement was inaccurate. The language and the library are distinct entities. The language is intended by design to be extended through the use of libraries; their use does not make the code "non-standard" - it will compile in any compliant compiler for which the library is made available. As you say it is helpful to warn of non-portable usage, but it seems from your reiteration that you do not accept the distinction between language and library since you repeated the same inaccuracy.
Clifford
@cli: The C++ standard covers both the language and the standard library. Could we avoid the noise of such extensive discussions in comments though?
Georg Fritzsche
+1  A: 

See here for an example. Also good explanation how to use the conversion specifiers etc.

Edit: KMan was faster :)

Edit2: Link leads to a desctiption of

char *strptime(const char *restrict buf, const char *restrict format, struct tm *restrict tm);

and also a short example on how to use. The Site is provided by

The Open Group Base Specifications Issue 6 IEEE Std 1003.1, 2004 Edition

BaumS
Please provide at least a summary and not just a link.
Georg Fritzsche
+1  A: 

Hello Skaffman,

Just in case you want to understand how it works (no blackbox) or if you need the stuff to fly (no function calls) then here are a few tips for you:

//             0123456789
char date[] = "30-05-2001"; // DD-MM-YYYY
int day, month, year;

day = (date[0] - '0') * 10 + (date[1] - '0');
month = (date[3] - '0') * 10 + (date[4] - '0');
year = (date[6] - '0') * 1000 + (date[7] - '0') * 100 + (date[8] - '0') * 10 + (date[9] - '0');

This (untested) code is clother to the right way to do what you wanted to do as it only does what you need to do (not more, not less). Safe, small, simple, FAST.

If you are unsure of the date (user input) then you will have to check for its validity (or accept the idea to live with invalid dates in your application).

Pierre
+1  A: 
std::istringstream buffer("30-05-2001");
int day = 0;
int month = 0;
int year = 0;
char dummy = 0;

buffer >> day >> dummy >> month >> dummy >> year;
// TODO: validate values here

You can perform different reads on the data if you want to also validate that the dummy equals '-'.

utnapistim