#include
#include
#include
char*tzstr="TZ=PST8PDT";
int main()
{
struct timeval tbegin = {0}, tend = {0};
gettimeofday(&tbegin, NULL);
gettimeofday(&tend, NULL);
printf("new [%f], ", tend.tv_sec-tbegin.tv_sec + (tend.tv_usec - tbegin.tv_usec)/1000000.00);
/**********************************************************************/
int i=0;
time_t start, end;
time(&start);//得到系统当前的日历时间
printf("Today~s date and time: %s\n", ctime(&start));
sleep(5);//秒为单位
usleep(20);//单位是微秒
time(&end);
printf("Today~s date and time: %s\n", ctime(&end));
printf("The difference is: %f seconds\n", difftime(end,start));//得到两次机器时间差,单位为秒
time_t timer;
struct tm *tblock;
timer = time(NULL); tblock = localtime(&timer);//返回一个以tm结构表达的机器时间信息
printf("Local time is: %s", asctime(tblock));
time_t long_time;
struct tm logtime;
char current_time[30];
time( &long_time );//得到系统当前的日历时间
memcpy(&logtime ,localtime(&long_time),sizeof(logtime));
strftime(current_time,sizeof(current_time),"%Y%m%d %H:%M:%S \n",&logtime);
printf("current_tim=%s\n",current_time);
time_t t;
struct tm *gmt,*area;
putenv(tzstr);
tzset();
t = time(NULL);
area = localtime(&t);
printf("Local time is: %s\n", asctime(area));
gmt = gmtime(&t);
printf("GMT is: %s\n", asctime(gmt));
return 0;
}
unsigned int GetTickCount()
{
struct timeval CurrentTime;
gettimeofday(&CurrentTime, NULL);
return ((CurrentTime.tv_sec * 1000) + (CurrentTime.tv_usec / 1000));
}
/***********************************************
gettimeofday()函数的使用方法:
1.简介:
在C语言中可以使用函数gettimeofday()函数来得到时间。它的精度可以达到微妙
2.函数原型:
#include
int gettimeofday(struct timeval*tv,struct timezone *tz )
3.说明:
gettimeofday()会把目前的时间用tv 结构体返回,当地时区的信息则放到tz所指的结构中
4.结构体:
1>timeval
struct timeval{
long tv_sec;///秒
long tv_usec;///微妙
};
2>timezone 结构定义为:
struct timezone{
int tz_minuteswest;///和greenwich 时间差了多少分钟
int tz_dsttime;///type of DST correction
}
3>在gettimeofday()函数中tv或者tz都可以为空。如果为空则就不返回其对应的结构体。
4>函数执行成功后返回0,失败后返回-1,错误代码存于errno中。
5.程序实例:
#include
#include
#include
int main() { struct timeval tv; struct timezone tz; gettimeofday(&tv,&tz); printf(“tv_sec:%d\n”,tv.tv_sec); printf(“tv_usec:%d\n”,tv.tv_usec); printf(“tz_minuteswest:%d\n”,tz.tz_minuteswest); printf(“tz_dsttime:%d\n”,tz.tz_dsttime); } 说明:在使用gettimeofday()函数时,第二个参数一般都为空,因为我们一般都只是为了获得当前时间,而不用获得timezone的数值 * **********************************************/
输出结果:
[back@localhost ly]$ gcc time.c -o time1
[back@localhost ly]$ ./time1
Today~s date and time: Mon Nov 28 11:31:51 2016
Today~s date and time: Mon Nov 28 11:31:56 2016
The difference is: 5.000000 seconds
Local time is: Mon Nov 28 11:31:56 2016
current_tim=20161128 11:31:56
Local time is: Sun Nov 27 19:31:56 2016
GMT is: Mon Nov 28 03:31:56 2016
[back@localhost ly]$ ./time1
Today~s date and time: Mon Nov 28 11:31:51 2016
Today~s date and time: Mon Nov 28 11:31:56 2016
The difference is: 5.000000 seconds
Local time is: Mon Nov 28 11:31:56 2016
current_tim=20161128 11:31:56
Local time is: Sun Nov 27 19:31:56 2016
GMT is: Mon Nov 28 03:31:56 2016
百度百科:
http://baike.baidu.com/link?url=Vea0cZyPHlvqG0syUZp6NBz6PBaWwPOuAEoOYoiiuv-7PeDEUiOByQOhM5-8-WyYu3dxG5-IYCuxxg0C5F2jH_