I’ve written a function to convert datetimes from one format to another –
int convertDateTime(char* source_fmt,char* dest_fmt,char* source,char* dest)
{
struct tm tmpptr;
if (strptime(source,source_fmt,&tmpptr) == NULL)
{
strcpy(dest,"");
return -1;
}
strftime(dest,100,dest_fmt,&tmpptr);
return 0;
}
It works fine for most formats, But when I use format = “%y%j”, all I get is 10001; the julian day does not work.
I’m using gcc on solaris 10. Any idea what i need to change?
Update
The original answer (below) presumed that the “%y%j” format was used on the output (strftime) not the input (strptime). The mktime function will compute yday from valid info, but it doesn’t work the other way.
If you want to decode something like that you need to do it manually. Some example code follows. It has had minimal testing and almost certainly has bugs. You may need to alter ir, depending in the input format you want.
================================================
End Update
After you call strptime(), call mktime() which will populate any missing members of the struct. Also, you should zero out the struct before beginning.