我有时间在Seconds,我想将此时间转换为Readable Time Sting as,
DAY-Date-Hour-Min-Sec-Month-Year.
如何在CPP或C中执行此操作。
答案 0 :(得分:2)
time()
,localtime()
和sprintf()
应该可以解决问题。
#ifdef __cplusplus
#include <cstdio>
#else
#include <stdio.h>.
#endif
#include <time.h>
static const char *Weekdays[] = {
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday"
};
int main() {
time_t curSec;
struct tm *curDate;
char dateString[32];
curSec = time(NULL);
curDate = localtime(&curSec);
sprintf(dateString,
"%s-%02d-%02d-%02d-%02d-%02d-%d",
Weekdays[curDate->tm_wday],
curDate->tm_mday,
curDate->tm_hour,
curDate->tm_min,
curDate->tm_sec,
curDate->tm_mon+1,
curDate->tm_year+1900);
printf("%s\n",dateString);
return 0;
}