Question

So I'm writing a program to mimic the "find" command in linux. I have everything taken care of, but I cannot figure out how to format the string from ctime, mtime and atime.

I know the arguments are struct time_t, but I can't find to convert it to a formatable string.

Part of what I have is below. it won't work due to the error " error: cannot convert '__time_t' to 'const tm' for argument '4' to 'size_t strftime(char*, size_t, const char*, const tm*)'

Ideally, I want something like this:

cout << strftime(mBuf, 18, "%I:%M:%S-%m/%d/%y", (&sb.st_mtime));

where sb is the stat struct. It should print in the format: HH:MM:SS-MM/DD/YY

I'm sure the answer is something simple, but I cannot figure it out.

Was it helpful?

Solution

You have to do it in several steps:

struct tm *mytm = localtime(&sb.st_mtime);
strftime(mBuf, 18, "%I:%M:%S-%m/%d/%y", mytm);
cout << mBuf;

Of course, the call to localtime and strftime can be combined into a single call:

strftime(mBuf, 18, "%I:%M:%S-%m/%d/%y", localtime(&sb.st_mtime));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top