我正在尝试从当前时间生成一个日期字符串以放入HTTP响应头.它看起来像这样:
Date: Tue, 15 Nov 2010 08:12:31 GMT
Run Code Online (Sandbox Code Playgroud)
我只有默认的C库可以使用.我该怎么做呢?
pmg*_*pmg 19
使用strftime(),声明于<time.h>.
#include <stdio.h>
#include <time.h>
int main(void) {
char buf[1000];
time_t now = time(0);
struct tm tm = *gmtime(&now);
strftime(buf, sizeof buf, "%a, %d %b %Y %H:%M:%S %Z", &tm);
printf("Time is: [%s]\n", buf);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
请参阅键盘上的代码"running".