gpt4 book ai didi

c - 将时间戳格式化为文本

转载 作者:太空宇宙 更新时间:2023-11-04 07:27:02 32 4
gpt4 key购买 nike

我想获取文件的修改日期,然后将其格式化为人类可读的日期。我正在运行一个 C 程序,该程序获取有关特定文件上次修改时间的信息。我的 C 代码包含一个系统 cmd,其中包含许多由管道分隔的 egreps、awks、seds。使用 sed 或 awk 或类似的东西,我如何将 06 转换为六月(这可以是任何月份,所以需要一个数组或其他东西)我想要实现的是得到一个类似于以下的字符串:

我的 C 代码包含:

    char string1[100] = "";
#define MAXCHAR 100
FILE *fp;
char str[MAXCHAR], str2[MAXCHAR];
char* filename = "newfile";

/*
stat: run 'stat' on the dtlName file to display status information.
egrep: search for the pattern 'Modify' and print the lines containing it.
awk: Get columns 2 & 3
sed: replace the . with a space, leaving 3 columns of output
awk: only print cols 1 & 2 to newfile
sed: replace '-' with ' ' in newfile
awk: format output in newfile
*/
sprintf(string1, "/bin/stat %s \
| egrep Modify \
| /bin/awk '{print $2, $3}' \
| /bin/sed 's/\\./ /g' \
| /bin/awk '{print $1, $2}' \
| /bin/sed 's/-/ /g' \
| /bin/awk '{print $3,$2\", \"$1,\"at\",$4}' > newfile"
, dtlName);
system(string1);
fp = fopen(filename, "r");
while (fgets(str, MAXCHAR, fp) != NULL)
sprintf(str2,"%s", str);

/* Write information to file */
DisplayReportFile (report);
ReportEntry (report,L"Source file: %s, Created: %s\n\n",dtlName,str2);

最佳答案

这是同一程序的工作版本:

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char ** argv)
{
if (argc < 2) exit(1);

char *filename = argv[1];

struct stat st;
char s[1000];
if (stat(filename, &st))
exit(2);

struct tm *mdtime = localtime( &st.st_mtime );
strftime(s, sizeof(s), "%D", mdtime);

printf("%s\n", s);
}

参见 strftime更多格式。

关于c - 将时间戳格式化为文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17445789/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com