C语言 如何将 st_mtime(从 stat 函数获得)转换为字符串或字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13542345/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 04:32:26  来源:igfitidea点击:

How to convert st_mtime (which get from stat function) to string or char

cdatetimeandroid-ndkstring-conversion

提问by testCoder

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

我需要将 st_mtime 转换为字符串格式以将其传递给 java 层,我尝试使用此示例http://www.cplusplus.com/forum/unices/10342/但编译器产生错误

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

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

从“long unsigned int*”到“const time_t* {aka long int const*}”的无效转换

初始化 'tm* localtime(const time_t*)' [-fpermissive] 的参数 1

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

我做错了什么,如何在字符串表示中使用 stat 函数获取文件的时间创建时间。

Help please.

请帮忙。

回答by Basile Starynkevitch

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

根据stat(2)手册页,该st_mtime字段是一个time_t(即在阅读time(7)手册页后,自unix Epoch以来的秒数)。

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

您需要localtime(3)将其转换time_tstruct tm本地时间,然后使用strftime(3)将其转换为char*字符串。

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);

then use timbufperhaps by strdup-ing it.

然后timbuf可能通过strdup-ing使用它。

NB. I am using localtime_rbecause it is more thread friendly.

注意。我正在使用,localtime_r因为它对线程更友好。

回答by iabdalkader

use strftime()there's an example in the man pagesomething like:

使用手册页中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);

Would print the output:

将打印输出:

"24.11.2012 17:04:33"

回答by Rohit

You can achieve this in an alternative way:

您可以通过另一种方式实现这一点:

  1. Declare a pointer to a tmstructure:

    struct tm *tm;
    
  2. Declare a character array of proper size, which can contain the time string you want:

    char file_modified_time[100];
    
  3. Break the st.st_mtime(where stis a structof type stat, i.e. struct stat st) into a local time using the function localtime():

    tm = localtime(&st.st_mtim);
    

    Note: st_mtimeis a macro (#define st_mtime st_mtim.tv_sec) in the man page of stat(2).

  4. Use sprintf()to get the desired time in string format or whatever the format you'd like:

    sprintf(file_modified_time, "%d_%d.%d.%d_%d:%d:%d", tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
    
  1. 声明一个指向tm结构的指针:

    struct tm *tm;
    
  2. 声明一个合适大小的字符数组,它可以包含你想要的时间字符串:

    char file_modified_time[100];
    
  3. 使用函数将st.st_mtime(where stis a structof type stat, ie struct stat st) 分解为本地时间localtime()

    tm = localtime(&st.st_mtim);
    

    注意:st_mtimestat(2)#define st_mtime st_mtim.tv_sec手册页中的宏 ( )

  4. 用于sprintf()以字符串格式或您喜欢的任何格式获取所需的时间:

    sprintf(file_modified_time, "%d_%d.%d.%d_%d:%d:%d", tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
    

NB: You should use

注意:你应该使用

memset(file_modified_time, '##代码##', strlen(file_modified_time));

before sprintf()to avoid the risk of any garbage which arises in multi-threading.

之前sprintf(),以避免出现在多线程任何垃圾的风险。