#include <stdio.h>
#include <time.h>
#include <sys/time.h>
int main(int argc, char * argv [ ])
{
// time_t time(time_t T)
// 返回1970年1月1日00:00:00,到现在的秒数,即UTC时间
// 参数不为NULL时,将T设为与返回值一样的值。
time_t T1,T2;
T1 = time(&T2);
printf("%d\n",T1);
printf("%d\n",T2);
///----------------------------------------------------------------------
unsigned long Time;
struct timeval tv;
struct timezone tz;
gettimeofday( &tv, &tz);
/// 本地时间:秒单位
Time = tv.tv_sec - tz.tz_minuteswest * 60;
printf("%lu\n",Time);
/// 本地时间:毫秒
Time = tv.tv_sec * 1000 - tz.tz_minuteswest * 60 * 1000 + tv.tv_usec / 1000;
printf("%lu\n",Time);
///----------------------------------------------------------------------
// struct tm *localtime(time_t *T)
// 将UTC时间到现在的秒数T,转换成本地时间的结构体struct tm
// 有时区转换,加时区
struct tm *ps_tm;
ps_tm = localtime(&T1);
printf("%d-%d-%d_%d:%d:%d\n",ps_tm->tm_year+1900,ps_tm->tm_mon+1,
ps_tm->tm_mday,ps_tm->tm_hour,ps_tm->tm_min,ps_tm->tm_sec);
// localtime_r(T,&s_tm);
// struct tm *localtime_r(const time_t *timer, struct tm *result);
// 功能与localtime一样,但是可重入,前者不可。
struct tm s_tm;
localtime_r(&T1,&s_tm);
printf("%d-%d-%d_%d:%d:%d\n",s_tm.tm_year+1900,s_tm.tm_mon+1,
s_tm.tm_mday,s_tm.tm_hour,s_tm.tm_min,s_tm.tm_sec);
///----------------------------------------------------------------------
// mktime***有时区转换,减时区
// time_t mktime(struct tm * timeptr);
// 将struct tm结构体转换成UTC时间到现在的秒数
time_t Tmktime = mktime(&s_tm);
printf("mktime:%d\n",Tmktime);
///----------------------------------------------------------------------
// gmtime***无时区转换
// struct tm *gmtime(const time_t *time);
// 将time转换成struct tm结构体
ps_tm = gmtime(&T1);
printf("%d-%d-%d_%d:%d:%d\n",ps_tm->tm_year+1900,ps_tm->tm_mon+1,
ps_tm->tm_mday,ps_tm->tm_hour,ps_tm->tm_min,ps_tm->tm_sec);
///----------------------------------------------------------------------
// ctime***有时区转换,UTC加时区
// char *ctime(const time_t *time);
// 将UTC时间的秒数,转换成本地时间的年月日字符串
char *Tstr=NULL;
Tstr = ctime(&T1);
printf("ctime:%s\n",Tstr);
///----------------------------------------------------------------------
// asctime***无时区转换
// char* asctime(const tm*)
// 把struct tm结构体中储存的时间转换为字符串格式返回
Tstr = asctime(&s_tm);
printf("asctime:%s\n",Tstr);
return 0;
}