如何st_mtime(从统计功能得到)转换为字符串或字符 [英] How to convert st_mtime (which get from stat function) to string or char

查看:2258
本文介绍了如何st_mtime(从统计功能得到)转换为字符串或字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要st_mtime转换为字符串格式为它传递到Java层,我尝试用这个例子的 http://www.cplusplus.com/forum/unices/10342/ 但是编译器产生误差

I need to convert st_mtime to string format for passing it to java layer, i try to use this example http://www.cplusplus.com/forum/unices/10342/ but compiler produce errors

这是'长无符号整数*'到'const的time_t的* {又名无效的转换
  长整型常量*}

invalid conversion from 'long unsigned int*' to 'const time_t* {aka long int const*}'

初​​始化的参数1'TM *本地时间(常量的time_t *)'
  [-fpermissive]

initializing argument 1 of 'tm* localtime(const time_t*)' [-fpermissive]

我做错了什么,如何在字符串presentation使用统计函数得到的时间创建的文件。

What i doing wrong, how to get time creation of file using stat function in string presentation.

请帮助。

推荐答案

据该的 STAT(2)手册页,在 st_mtime 字段是 time_t的(即后阅读时间(7)手册页,一些由于 Unix纪元)秒。

According to the stat(2) man page, the st_mtime field is a time_t (i.e. after reading the time(7) man page, a number of seconds since the unix Epoch).

您需要本地时间(3)以将其转换 time_t的结构TM 在当地的时间,那么,的的strftime(3)将其转换为一个的char * 字符串

You need localtime(3) to convert that time_t to a struct tm in local time, then, strftime(3) to convert it to a char* string.

所以,你可以code是这样的:

So you could code something like:

time_t t = mystat.st_mtime;
struct tm lt;
localtime_r(&t, &lt);
char timbuf[80];
strftime(timbuf, sizeof(timbuf), "%c", &lt);

然后用 timbuf 的strdup 也许-ing它。

NB。我使用则localtime_r ,因为它更线程友好。

NB. I am using localtime_r because it is more thread friendly.

这篇关于如何st_mtime(从统计功能得到)转换为字符串或字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆