#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
int
main(int argc, char *argv[])
{
char outstr[200];
time_t t;
struct tm *tmp;
t = time(NULL);
tmp = localtime(&t);
if (tmp == NULL) {
perror("localtime");
exit(EXIT_FAILURE);
}
strftime(outstr,sizeof(outstr),"%Y:%m:%d",tmp);
puts(outstr);
strcat(outstr,".xml");
puts(outstr);
} /* main */
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
int
main(int argc, char *argv[])
{
char outstr[200];
time_t t;
struct tm *tmp;
t = time(NULL);
tmp = localtime(&t);
if (tmp == NULL) {
perror("localtime");
exit(EXIT_FAILURE);
}
strftime(outstr,sizeof(outstr),"%Y:%m:%d",tmp);
puts(outstr);
strcat(outstr,".xml");
puts(outstr);
} /* main */
本文介绍了一个使用C语言进行时间格式化的简单示例,通过调用标准库函数如 time(), localtime() 和 strftime() 来获取并格式化当前时间。代码展示了如何将时间格式化为 年:月:日 的形式,并在其后附加 .xml 扩展名。

被折叠的 条评论
为什么被折叠?



