C语言时间操作函数

一、获取日历时间
time_t是定义在time.h中的一个类型,表示一个日历时间,也就是从1970年1月1日0时0分0秒到此时的秒数,原型是:
typedef long time_t;        /* time value */
可以看出time_t其实是一个长整型,由于长整型能表示的数值有限,因此它能表示的最迟时间是2038年1月18日19时14分07秒。

函数time可以获取当前日历时间时间,time的定义:
time_t time(time_t *)

code:
#include <iostream>  
#include <time.h>  
using namespace std;  
int main(void)  
{  
 time_t nowtime;  
 nowtime = time(NULL); //获取当前时间  
 cout << nowtime << endl;  
   
 return 0;  
}

二、获取本地时间
time_t只是一个长整型,不符合我们的使用习惯,需要转换成本地时间,就要用到tm结构,time.h中结构tm的原型是:
struct tm
{
        int tm_sec;     /* seconds after the minute – [0,59] */
        int tm_min;     /* minutes after the hour – [0,59] */
        int tm_hour;    /* hours since midnight – [0,23] */
        int tm_mday;    /* day of the month – [1,31] */
        int tm_mon;     /* months since January – [0,11] */
        int tm_year;    /* years since 1900 */
        int tm_wday;    /* days since Sunday – [0,6] */
        int tm_yday;    /* days since January 1 – [0,365] */
        int tm_isdst;   /* daylight savings time flag */
};

可以看出,这个结构定义了年、月、日、时、分、秒、星期、当年中的某一天、夏令时。可以用这个结构很方便的显示时间。

用localtime获取当前系统时间,该函数将一个time_t时间转换成tm结构表示的时间,函数原型:
 struct tm * localtime(const time_t *)
使用gmtime函数获取格林尼治时间,函数原型:
 struct tm * gmtime(const time_t *)
 
code:
#include <iostream>  
#include <time.h>  
using namespace std;  
void dsptime(const struct tm *); //输出时间。  
 
int main(void)  
{  
        time_t nowtime;  
        nowtime = time(NULL); //获取日历时间  
        cout << nowtime << endl;  //输出nowtime  
        
        struct tm *local,*gm;  
        
        local = localtime(&nowtime);  //获取当前系统时间  
        dsptime(local);   
        
        gm=gmtime(&nowtime);  //获取格林尼治时间  
        dsptime(gm);  
          
        return 0;  
}  
void dsptime(const struct tm * ptm)  
{  
        char *pxq[]={“日”,”一”,”二”,”三”,”四”,”五”,”六”};  
        cout << ptm->tm_year+1900 << “年” << ptm->tm_mon+1 << “月” << ptm->tm_mday << “日 ” ;  
        cout << ptm->tm_hour << “:” << ptm->tm_min << “:” << ptm->tm_sec <<” ” ;  
        cout << ” 星期” <<pxq[ptm->tm_wday] << ” 当年的第” << ptm->tm_yday << “天 ” << endl;  
}

三、计算时间间隔
可以通过difftime来计算两个时间的间隔,可以精确到秒,函数原型:
 double difftime(time_t, time_t)
要想精确到毫秒,就要使用clock函数了,函数原型:
 clock_t clock(void)
从定义可以看出clock返回一个clock_t类型,这个类型也定义在time.h中,原型是:
 typedef long clock_t
clock_t也是一个长整型,表示的是从程序开始运行到执行clock函数时所经过的cpu时钟计时单元数。

code:
#include <iostream>  
#include <time.h>  
using namespace std;  
 
int main(void)  
{  
        time_t start, ends;  
        clock_t cstart,cends;  
        
        start = time(NULL);  
        cstart = clock();  
        
        system(“pause”);  
        
        ends = time(NULL);  
        cends = clock();  
        
        cout << “时间差:” << difftime(ends,start) << endl;  
        cout << “Clock时间差:” << cends-cstart << endl;  
        
        return 0;  
}  

四、获得当前精确时间(1970年1月1日到现在的时间)
gettimeofday是获得当前精确时间(1970年1月1日到现在的时间),或者为执行计时,可以使用gettimeofday()函数。函数原型:
int gettimeofday(struct  timeval *tv, struct  timezone *tz )
gettimeofday()会把目前的时间用tv 结构体返回,当地时区的信息则放到tz所指的结构中
函数执行成功后返回0,失败后返回-1

(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*/
}

code:
#include<stdio.h>
#include<sys/time.h>
#include<unistd.h>

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的数值

五、将时间结构转换为对应的秒数:
mktime()函数原型:
time_t mktime(struct tm * timeptr);
作用是,将时间转换为自1970年1月1日以来持续时间的秒数,发生错误时返回-1。

code:
#include
#include
main()
{
        time_t timep;
        strcut tm *p;
        
        time(&timep);
        printf(“time() : %d \n”,timep);
        
        p=localtime(&timep);
        timep = mktime(p);
        printf(“time()->localtime()->mktime():%d\n”,timep);
}


版权声明:本文为weixin_36198067原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。