tes*_*der 10 c datetime string-conversion android-ndk
我需要将st_mtime转换为字符串格式以将其传递给java层,我尝试使用此示例http://www.cplusplus.com/forum/unices/10342/但编译器产生错误
从'long unsigned int*'到'const time_t*{aka long int const*}'的无效转换
初始化'tm*localtime(const time_t*)'[-fpermissive]的参数1
我做错了,如何在字符串表示中使用stat函数获取文件的时间.
请帮忙.
Bas*_*tch 14
根据stat(2)手册页,该st_mtime字段是a time_t(即在读取时间(7)手册页之后,自unix Epoch以来的秒数).
您需要localtime(3)将其转换time_t为struct tm本地时间,然后strftime(3)将其转换为char*字符串.
所以你可以编写类似的东西:
time_t t = mystat.st_mtime;
struct tm lt;
localtime_r(&t, <);
char timbuf[80];
strftime(timbuf, sizeof(timbuf), "%c", <);
Run Code Online (Sandbox Code Playgroud)
然后使用timbuf也许通过strdup它.
NB.我正在使用,localtime_r因为它更友好.
使用手册页中strftime()的一个例子:
struct tm *tm;
char buf[200];
/* convert time_t to broken-down time representation */
tm = localtime(&t);
/* format time days.month.year hour:minute:seconds */
strftime(buf, sizeof(buf), "%d.%m.%Y %H:%M:%S", tm);
printf("%s\n", buf);
Run Code Online (Sandbox Code Playgroud)
会打印输出:
"24.11.2012 17:04:33"
Run Code Online (Sandbox Code Playgroud)