#include <stdio.h>
#include <time.h>
#include <unistd.h>
int main(void)
{
time_t now, now1, now2;
struct tm ts;
char buf[80];
// Get current time
time(&now);
ts = *localtime(&now);
strftime(buf, sizeof(buf), "%a %Y-%m-%d %H:%M:%S", &ts);
printf("Local Time %s\n", buf);
//UTC time
now2 = now - 19800; //from local time to UTC time
ts = *localtime(&now2);
strftime(buf, sizeof(buf), "%a %Y-%m-%d %H:%M:%S", &ts);
printf("UTC time %s\n", buf);
//TAI time valid upto next Leap second added
now1 = now + 37; //from local time to TAI time
ts = *localtime(&now1);
strftime(buf, sizeof(buf), "%a %Y-%m-%d %H:%M:%S", &ts);
printf("TAI time %s\n", buf);
return 0;
}
2条答案
按热度按时间8i9zcol21#
根据@H2CO3关于使用
strftime(3)
的正确建议,下面是一个示例程序。r1zk6ea12#
这段代码帮助您将时间戳从系统时间转换为UTC和TAI人类可读格式。