#include <stdio.h> #include <stdlib.h> #include <time.h> int main(void) { time_t tt; struct tm * ttm; //获取本机时间 tt = time(NULL); printf("UTC time = %ld\n", tt); printf("current time is: %s", ctime(&tt)); //转换成本地时间 ttm = localtime(&tt); printf("local time is: %s", asctime(ttm)); printf("local time:\n" "\t%d-%d-%d : %d-%d-%d\n" "\tweekday %d\n" "\tyearday %d\n", ttm->tm_year+1900, ttm->tm_mon+1, ttm->tm_mday, ttm->tm_hour, ttm->tm_min, ttm->tm_sec, ttm->tm_wday, ttm->tm_yday); //格林威治时间 ttm = gmtime(&tt); printf("gm time is: %s", asctime(ttm)); printf("gm time:\n" "\t%d-%d-%d : %d-%d-%d\n" "\tweekday %d\n" "\tyearday %d\n", ttm->tm_year+1900, ttm->tm_mon+1, ttm->tm_mday, ttm->tm_hour, ttm->tm_min, ttm->tm_sec, ttm->tm_wday, ttm->tm_yday); return 0; }