1844311 Members
2803 Online
110230 Solutions
New Discussion

strptime

 
Espen Ekeroth
Occasional Contributor

strptime

Having problem with %U format for the function strptime. It just don't work. Here is my sample code:

#include
#include

int main()
{
struct tm t;
if(strptime("2004:22","%Y:%U",&t) == NULL)
printf("NULL return\n");

printf("dato=%d%d%d\n",
t.tm_year+1900,t.tm_mon,t.tm_mday);
}

It looks like everything else I do try work, but to convert a year and week number to a date is just not working. (The NULL printf is not written, the value is just set to 0 ) I have also tested %W with the same result.

If you have an idea - please help

Espen

1 REPLY 1
Dennis Handly
Acclaimed Contributor

Re: strptime

It appears that strptime(3) doesn't process %U, unless %w or %a/%A is also there. I don't see such restriction in the Posix standard, so it appears this is a bug.

Also, your test program should clear out your struct tm before calling strptime(3). And your printf should increment tm_mon and have 2 digit day and month:
printf("dato=%d-%02d-%02d\n", t.tm_year+1900, t.tm_mon+1, t.tm_mday);