linux c语言获取时间

在程序中,经常需要输出系统的当前时间、计算程序的执行时间、使用计时器等。

一、时间的类型

1.格林威治标准时间

coordinated universal time(UTC)是世界标准时间,即常说的格林威治标准时间(greenwich mean time,GMT).

2.日历时间

日历时间(calendar time)是用"一个标准时间点(如1970年1月1日0点)到此时经过的秒数"来表示的时间.

二、时间函数的API

时间函数的API均属于系统调用函数.。

1.获取日历时间

  #include <time.h>

  time_t time(time_t *tloc)

  函数功能:获取日历时间,即从1970年1月1日0点到现在所经历的秒数.

  参数:通常设置为NULL

(time_t在time.h中定义:typedef long int time_t)

  例:

  #include <time.h>

  int main(int argc,char *argv[])

  {

     int seconds=0;

     seconds = time(NULL);

     printf("seconds=%d\n",seconds);

 }

执行结果:

[root@localhost Time]# ./time

seconds=1294908511

通常用户得到日历时间的秒数没有实际的意义,但可以为时间转化做一些铺垫性质的工作.为了更好的利用时间,用户需要将这些秒数转化为更容易接受的时间表示方式,这些表示时间的方式有格林威治时间、本地时间等.

2.将日历时间转换为格林威治标准时间

   struct tm *gmtime(const time_t *timep)

   函数功能:将日历时间转化为格林威治标准时间,并保存在tm结构

   参数:日历时间的返回值

3.将日历时间转化为本地时间

   struct tm* localtime(const time_t *timep)

   函数功能:将日历时间转化为本地时间,并保存至tm结构

   参数:日历时间的返回值

  由上面两个函数可以看出,这两个函数的返回值均存放在tm结构中,具体的tm结构如下:

   struct tm

   {

      int tm_sec;   //秒值

      int tm_min;   //分钟值

      int tm_hour;  //小时值

      int tm_mday;  //本月第几日

      int tm_mon;   //本年第几月

      int tm_year;  //tm_year+1900=哪一年

      int tm_wday;  //本周第几日

      int tm_yday;  //本年第几日

      int tm_isdst; //日光节约时间

   }

   建立time.c

   #include <stdio.h>

   #include <time.h>

   int main(int argc,char *argv[])

   {

 struct tm *local;

     time_t t;

     t = time(null);   //获取日历时间

     local = localtime(&t);  //将日历时间转化为本地时间,并保存在struct tm结构中

     printf("local hour is :%d\n",local->tm_hour);

     local = gmtime(&t);  //将日历时间转化为格林威治时间,并保存在struct tm结构中

     printf("utc hour is :%d\n",local->tm_hour);

     return 0;

   }

   执行结果:

[root@localhost Time]# gcc –o time time.c

[root@localhost Time]# ./time

Local hour is: 0

UTC hour is: 8

[root@localhost Time]# date

Thu Jan 13 00:52:44 PST 2011

 利用函数gmtime()、localtime()可以将日历时间转化为格林威治时间和本地时间,虽然用户可通过结构体tm来获取

这些时间值,但看起来还不方便,最好是将所有的信息,如年、月、日、星期、时、分、秒以字符串的形式显示出来,

这样就加直观.

4.时间显示

   char *asctime(const struct tm *tm)

   函数功能:将tm格式的时间转化为字符串

   参数:日历时间的返回值

   例如: SAT Jul 30 08:43:03 2005

该函数较ctime()使用起来更加的复杂.必须按照下面3个步骤来进行.

   <1>使用函数time()来获取日历时间

   <2>使用函数gmtime()将日历时间转化为格林威治标准时间

   <3>使用函数asctime()将tm格式的时间转化为字符串

   例程:

    #include <time.h>

    #include <stdio.h>

    int main(void)

   {

      struct tm *ptr;

      time_t lt;

      lt=time(null); 

      ptr=gmtime(&lt);

      printf(asctime(ptr));

      printf(ctime(&lt));

      return 0;

   }

   char *ctime(const time_t *timep)

   函数功能:将日历时间转化为本地时间的字符串形式

   参数:日历时间的返回值

   该函数较asctime()使用起来更加简单.必须按照下面2个步骤来进行.

   <1>使用函数time()来获取日历时间

   <2>使用函数ctime()将日历时间直接转化为字符串

  5.获取从今日凌晨到现在的时间差

   int gettimeofday(struct timeval *tv,struct timezone *tz)

   函数功能:获取从今日凌晨(0:0:0)到现在的时间差,常用于计算事件耗时

   参数1:存放从今日凌晨(0:0:0)到现在的时间差,时间差以秒或微秒为单位,以结构体形式存放

         struct timeval

        {

           int tv_sec;    //秒数

           int tv_usec;   //微秒数

        }

   参数2:常设置为null

   函数用法:可以在做某件事情之前调用gettimeofday(),在做完该件事情之后调用gettimeofday(),两个函数的参数1

的差就是做该事情所消耗的时间.

   例程:计算函数function()的耗时

    time.c

    #include <sys/time.h>

    #include <stdio.h>

    #include <stdlib.h>

    #include <math.h>

    void function()  

   {

      unsigned int i,j;

      double y;

      for(i=0;i<1000;i++)

        for(j=0;j<1000;j++)

         y++;

   }

   void main()

  {

      struct timeval tpstart,tpend;

      float timeuse;

      gettimeofday(&tpstart,null); // 开始时间

      function();

      gettimeofday(&tpend,null);   // 结束时间

  // 计算执行时间,以微秒为单位进行计算

      timeuse=1000000*(tpend.tv_sec-tpstart.tv_sec)+tpend.tv_usec-tpstart.tv_usec;

      timeuse/=1000000;

      printf("used time:%f\n",timeuse);

      exit(0);

  }

 执行结果:

 [root@localhost lishuai]# gcc time.c -o time -wall

 [root@localhost lishuai]# ./time

    use time:0.006288

6.延时函数

   <1>使程序睡眠seconds秒

      unsigned int sleep(unsigned int seconds)

   函数功能:使程序睡眠seconds秒

   参数:需要休眠的秒数

   <2>使程序睡眠usec微秒

      void usleep(unsigned long usec)

   函数功能:使程序睡眠usec微秒

   参数:需要休眠的秒数

posted @ 2013-05-10 15:06  Charles-Zhang  阅读(1205)  评论(0编辑  收藏  举报