我们如何将C中的Unix时间戳转换为day:month:date:year?例如.如果我的unix时间戳是1230728833(int),我们如何将此值转换为 - > 2008年8月21日星期四?
谢谢,
Per @ H2CO3的正确使用建议strftime(3),这是一个示例程序.
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
static const time_t default_time = 1230728833;
static const char default_format[] = "%a %b %d %Y";
int
main(int argc, char *argv[])
{
time_t t = default_time;
const char *format = default_format;
struct tm lt;
char res[32];
if (argc >= 2) {
t = (time_t) atoi(argv[1]);
}
if (argc >= 3) {
format = argv[2];
}
(void) localtime_r(&t, <);
if (strftime(res, sizeof(res), format, <) == 0) {
(void) fprintf(stderr, "strftime(3): cannot format supplied "
"date/time into buffer of size %u "
"using: '%s'\n",
sizeof(res), format);
return 1;
}
(void) printf("%u -> '%s'\n", (unsigned) t, res);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
21911 次 |
| 最近记录: |