欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Linux下常用的时间函数,及windows c获取系统时间方法

程序员文章站 2024-01-21 21:30:58
...

一、参考地址

https://blog.csdn.net/IMBeGooD/article/details/76222922

二、Linux下常用的四种时间类型

2.1 time_t  #include <time.h>

1. 定义

typedef long time_t;

表示从UTC(coordinated universal time)时间1970年1月1日00时00分00秒(也称为Linux系统的Epoch时间)到当前时刻的秒数。

2.2 struct tm

  1. 定义
#ifndef _TM_DEFINED  

struct tm{  

    int tm_sec;  /*秒 - 取值区间为[0, 59]*/  

    int tm_min;  /*分 - 取值区间为[0, 59]*/  

    int tm_hour;  /*时 - 取值区间为[0, 23]*/  

    int tm_mday;  /*日 - 取值区间为[1, 31]*/  

    int tm_mon;  /*月份 - 取值区间为[0, 11]*/  

    int tm_year;  /*年份 - 其值为1900年至今年数*/  

    int tm_wday;  /*星期 - 取值区间[0, 6],0代表星期天,1代表星期1,以此类推*/  

    int tm_yday;  /*从每年的1月1日开始的天数-取值区间为[0, 365],0代表1月1日*/  

    int tm_isdst;  /*夏令时标识符,使用夏令时,tm_isdst为正,不使用夏令时,tm_isdst为0,不了解情况时,tm_isdst为负*/  

};  

#define _TM_DEFINED  

#endif  

 

2.3 struct timeval

  1. 定义
Struct tmieval{  

    time_t tv_sec;  /*秒s*/  

    suseconds_t tv_usec;  /*微秒us*/  

}; 

 

设置时间函数settimeofday()与获取时间函数gettimeofday(0均使用该事件类型作为参数

2.4 struct timespec

  1. 定义
struct timespec{  

    time_t tv_sec;  /*秒s*/  

    long tv_nsec;  /*纳秒ns*/  

}; 

 

三、Linux下常用的时间函数

3.1 time()函数

头文件:#include <time.h>
函数定义:time_t time(time_t *timer)
功能描述:该函数返回从1970年1月1日00时00分00秒至今所经过的秒数。如果time_t *timer非空指针,函数也会将返回值存到timer指针指向的内存。
返回值:成功则返回秒数,失败则返回((time_t)-1)值,错误原因存于errno中。

例: 

time_t seconds;  

seconds = time((time_t *)NULL); 

3.2 ctime()函数

头文件:#include <time.h>
函数定义:char *ctime(const time_t *timep);
功能描述:ctime( )将参数timep指向的time_t时间信息转换成实际所使用的时间日期表示方法,并以字符串形式返回。字符串格式为:"Wed Jun 20 21:00:00 2012\n"。
例:

time_t timep;  

tmep = time(NULL);  

printf("%s\n", ctime(&timep));  

3.3 gmtime()函数

头文件:#include <time.h>
函数定义:struct tm *gmtime(const time_t *timep)
功能描述:gmtime( )将参数timep指向的time_t时间信息转换成以tm结构体表示的GMT时间信息,并以struct tm*指针返回。
GMT:GMT是*时区,北京在东8区,相差8个小时,所以北京时间=GMT时间+8小时。
例:

int main(void)  

{  

    char *wday[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};  

    time_t timep;  

    struct tm *p_tm;  

    timep = time(NULL);  

    p_tm = gmtime(&timep);  /*获取GMT时间*/  

    printf("%d-%d-%d ", (p_tm->tm_year+1900), (p_tm->mon+1), p_tm->tm_mday);  

    printf("%s %d:%d:%d\n", wday[p_tm->tm_wday], p_tm->tm_hour, p_tm->tm_min, p_tm->tm_sec);  

}  

3.4 localtime()函数

头文件:#include <time.h>
函数定义:struct tm *localtime(const time_t *timep);
功能描述:localtime( )将参数timep指向的time_t时间信息转换成以tm结构体表示的本地时区时间(如北京时间= GMT+小时)。
例:

int main(void)  

{  

    char *wday[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};  

    time_t timep;  

    struct tm *p_tm;  

    timep = time(NULL);  

    p_tm = localtime(&timep); /*获取本地时区时间*/  

    printf("%d-%d-%d ", (p_tm->tm_year+1900), (p_tm->mon+1), p_tm->tm_mday);  

    printf("%s %d:%d:%d\n", wday[p_tm->tm_wday], p_tm->tm_hour, p_tm->tm_min, p_tm->tm_sec);  

    return 0;  

}  

3.5 localtime_r()函数

头文件:#include <time.h>

函数定义:struct tm *localtime_r(const time_t *timep, struct tm * result)

功能描述:将参数timep指向的time_t时间信息转换成以tm结构体表示的本地时区时间,运行于Linux平台下

如果运行在Windows环境中,在使用gcc编译时需要在gcc后面加上:

-D_POSIX_C_SOURCE=199506L

例:

#include <stdio.h>

#include <time.h>

int main()

{    

time_t time_seconds = time(0);    

struct tm now_time;    

localtime_r(&time_seconds, &now_time);    

printf("%d-%d-%d %d:%d:%d/n", now_time.tm_year + 1900, now_time.tm_mon + 1,        now_time.tm_mday, now_time.tm_hour, now_time.tm_min, now_time.tm_sec);

}

3.6 localtime_s()函数

头文件:#include <time.h>

函数定义:struct tm *localtime_s( struct tm * result, const time_t *timep )

功能描述:将参数timep指向的time_t时间信息转换成以tm结构体表示的本地时区时间,运行于Windows平台下,参数顺序与localtime_r()函数相反

例:

#include <iostream>

#include <time.h>i

nt main()

{

time_t time_seconds = time(0);

struct tm now_time;

localtime_s(&now_time,&time_seconds);

printf("%d-%d-%d %d:%d:%d/n", now_time.tm_year + 1900, now_time.tm_mon + 1, now_time.tm_mday, now_time.tm_hour, now_time.tm_min, now_time.tm_sec);

}

3.7 mktime()函数

头文件:#include <time.h>
函数定义:time_t mktime(struct tm *p_tm);
功能描述:mktime( )将参数p_tm指向的tm结构体数据转换成从1970年1月1日00时00分00秒至今的GMT时间经过的秒数。
例:

int main(void)  

{  

    time_t timep:  

    struct tm *p_tm;  

    timep = time(NULL);  

    pintf("time( ):%d\n", timep);  

    p_tm = local(&timep);  

    timep = mktime(p_tm);  

    printf("time( )->localtime( )->mktime( ):%d\n", timep);  

    return 0;  

}  

3.8 asctime()函数

头文件:#include <time.h>
函数定义:char *asctime(const struct tm *p_tm);
功能描述:asctime( )将参数p_tm指向的tm结构体数据转换成实际使用的时间日期表示方法,并以字符串形式返回(与ctime函数相同)。字符串格式为:"Wed Jun 20 21:00:00 2012\n"。
例:

int main(void)  

{  

    time_t timep;  

    timep = time(NULL);  

    printf("%s\n", asctime(gmtime(&timep)));  

    return 0;  

}  

3.9 difftime()函数

头文件:#include <time.h>
函数定义:double difftime(time_t timep1, time_t timep2);
功能描述:difftime( )比较参数timep1和timep2时间是否相同,并返回之间相差秒数。
例:

int main(void)  

{  

    time_t timep1, timep2;  

    timep1 = time(NULL);  

    sleep(2);  

    timep2 = time(NULL);  

    printf("the difference is %f seconds\n", difftime(timep1, timep2));  

    return 0;  

}  

3.10 gettimeofday()函数

头文件:#include <sys/time.h>
              #include <unistd.h>
函数定义:int gettimeofday(struct timeval *tv, struct timezone *tz);
功能描述:gettimeofday( )把目前的时间信息存入tv指向的结构体,当地时区信息则放到tz指向的结构体。
struct timezone原型:

struct timezone{  

    int tz_minuteswest; /*miniutes west of Greenwich*/  

    int tz_dsttime; /*type of DST correction*/  

};  

例:

struct timeval tv;  

struct timeval tz;  

gettimeofday(&tv, &tz);  


附:
使用time函数族获取时间并输出指定格式字符串例子(strftime( )函数):

int main(void)  

{  

    char strtime[20] = {0};  

    time_t timep;  

    struct tm *p_tm;  

    timep = time(NULL);  

    p_tm = localtime(&timep);  

    strftime(strtime, sizeof(strtime), "%Y-%m-%d %H:%M:%S", p_tm);  

    return 0;  

}  

3.11 settimeofday()函数

头文件:#include <sys/time.h>
                #include <unistd.h>
函数定义:int settimeofday(const struct timeval *tv, const struct timezone *gz);
功能描述:settimeofday( )把当前时间设成由tv指向的结构体数据。当前地区信息则设成tz指向的结构体数据。
例:

int main(void)  

{  

    char t_string[] = "2012-04-28 22:30:00";  

    struct tm time_tm;  

    struct timeval time_tv;  

    time_t timep;  

    int ret = 0;  

    sscanf(t_string, "%d-%d-%d %d:%d:%d", &time_tm.tm_year, &time_tm.tm_mon, &time_tm.tm_mday, &time_tm.tm_hour, &time_tm.tm_min, &time_tm.tm_sec);  

    time_tm.tm_year -= 1900;  

    time_tm.tm_mon -= 1;  

    time_tm.tm_wday = 0;  

    time_tm.tm_yday = 0;  

    time_tm.tm_isdst = 0;  

    timep = mktime(&time_tm);  

    time_tv.tv_sec = timep;  

    time_tv.tv_usec = 0;  

    ret = settimeofday(&time_tv, NULL);  

    if(ret != 0)  

    {  
        fprintf(stderr, "settimeofday failed\n");  

        return -1;  
    }  

    return 0;  

}  

四、示例-Windows环境下获取当前时间

环境:Windows,使用mingw32进行编译

Eg1:

#include <stdio.h>

#include <time.h>


int main(void)

{

    time_t time_seconds = time(0);

    struct tm* now_time = localtime(&time_seconds);

    printf("%04d-%02d-%02d %02d:%02d:%02d\n", now_time->tm_year + 1900, now_time->tm_mon+1, now_time->tm_mday, now_time->tm_hour, now_time->tm_min, now_time->tm_sec);

    setvbuf(stdout,NULL,_IONBF,0);

    return 0;

}

Eg2:

#include <stdio.h>

#include <time.h>

int main(void)

{

    time_t time_seconds = time(0);

    struct tm now_time;

    localtime_s(&now_time, &time_seconds);

    printf("%04d-%02d-%02d %02d:%02d:%02d\n", now_time.tm_year + 1900, now_time.tm_mon+1, now_time.tm_mday, now_time.tm_hour, now_time.tm_min, now_time.tm_sec);

    setvbuf(stdout,NULL,_IONBF,0);

    return 0;

}

Eg3:

#include <stdio.h>

#include <time.h>

#include <sys/time.h>

int main(void)

{

    char szCurTime[30] = {0};

    struct timeval tCurTimeVal = {0};

    struct tm tTm = {0};

    gettimeofday(&tCurTimeVal, NULL);

    printf("tCurTimeVla: %d \n",tCurTimeVal.tv_sec);

    localtime_s( &tTm, &(tCurTimeVal.tv_sec));

    snprintf(szCurTime, sizeof(szCurTime) - 1, "%04d.%02d.%02d %02d:%02d:%02d", 1900 + tTm.tm_year, tTm.tm_mon, tTm.tm_mday, tTm.tm_hour, tTm.tm_min, tTm.tm_sec);

    printf(szCurTime);

    setvbuf(stdout,NULL,_IONBF,0);

    return 0;

}

Eg4:

#include <stdio.h>

#include <time.h>

#include <sys/time.h>

int main(void)

{
    char szCurTime[30] = {0};

    struct timeval tCurTimeVal = {0};

    struct tm tTm = {0};

    gettimeofday(&tCurTimeVal, NULL);

    printf("tCurTimeVla: %d \n",tCurTimeVal.tv_sec);

    localtime_r( &(tCurTimeVal.tv_sec), &tTm );

    snprintf(szCurTime, sizeof(szCurTime) - 1, "%04d.%02d.%02d %02d:%02d:%02d", 1900 +             tTm.tm_year, tTm.tm_mon, tTm.tm_mday, tTm.tm_hour, tTm.tm_min, tTm.tm_sec);

    printf(szCurTime);

    setvbuf(stdout,NULL,_IONBF,0);

    return 0;
}

与localtime_s的区别是其参数顺序相反,使用localtime_r()函数,在使用gcc编译时,需要加上-D_POSIX_C_SOURCE=199506L,设置编译使用的版本,否则会找不到localtime_r()函数,从而报错。