Linux时间函数

常用时间类型

time_t

  time_t类型在time.h中定义:

#ifndef __TIME_T
#define __TIME_T

typedef long time_t;

#endif

struct tm

  tm结构在time.h中定义:

#ifndef _TM_DEFINED
#define _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日 */
    /* 夏令时标识符:
       1. 使用夏令时,tm_isdst为正。
       2. 不使用夏令时,tm_isdst为0。
       3. 不了解情况时,tm_isdst为负 */
    int tm_isdst;
};

#endif

struct timeval

  timeval结构体在time.h中定义:

struct tmieval {
    time_t tv_sec;       /* 秒s    */
    suseconds_t tv_usec; /* 微秒us */
};

struct timespec

  timespec结构体在time.h中定义:

struct timespec {
    time_t tv_sec; /* 秒s    */
    long tv_nsec;  /* 纳秒ns */
};

常用时间函数

time

  函数原型如下:

#include <time.h>
time_t time ( time_t *timer );

函数返回从197011000000秒至今所经过的秒数。
  如果time_t *timer为非空指针,函数也会将返回值存到timer指向的内存。

#include <time.h>
#include <stdio.h>

int main ( void ) {
    time_t seconds;
    seconds = time ( ( time_t * ) NULL );
    printf ( "seconds is %ld\n", seconds );
}

ctime

  函数原型如下:

#include <time.h>
char *ctime ( const time_t *timep );

函数将参数timep指向的time_t时间信息转换成实际所使用的时间日期表示方法,并以字符串形式返回。

#include <time.h>
#include <stdio.h>

int main ( void ) {
    time_t seconds;
    seconds = time ( NULL );
    printf ( "%s", ctime ( &seconds ) );
}

gmtime

  函数原型如下:

#include <time.h>
struct tm *gmtime ( const time_t *timep );

函数将参数timep指向的time_t时间信息转换成以tm结构体表示的GMT时间信息,并以struct tm *指针返回。
  GMT是中央时区,北京在东8区,相差8个小时,所以北京时间 = GMT时间 + 8小时

#include <time.h>
#include <stdio.h>

int main ( void ) {
    struct tm *local;
    time_t t;
    t = time ( NULL ); /* 获取日历时间 */
    local = localtime ( &t ); /* 将日历时间转化为本地时间 */
    printf ( "Local hour is: %d\n", local->tm_hour ); /* 打印当前的小时值 */
    local = gmtime ( &t ); /* 将日历时间转化为格林威治时间 */
    printf ( "UTC hour is: %d\n", local->tm_hour );
    return 0;
}

localtime

  函数原型如下:

#include <time.h>
struct tm *localtime ( const time_t *timep );

该函数将参数timep指向的time_t时间信息转换成以tm结构体表示的本地时区时间。

#include <time.h>
#include <stdio.h>

int main ( void ) {
    char *wday[] = {
        ( char* ) "Sun", ( char* ) "Mon", ( char* ) "Tue", ( char* ) "Wed",
        ( char* ) "Thu", ( char* ) "Fri", ( char* ) "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->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;
}

mktime

  函数原型如下:

#include <time.h>
time_t mktime ( struct tm *p_tm );

该函数将参数p_tm指向的tm结构体数据转换成从197011000000秒至今的GMT时间经过的秒数。

#include <time.h>
#include <stdio.h>

int main ( void ) {
    time_t timep;
    struct tm *p_tm;
    timep = time ( NULL );
    printf ( "time(): %ld\n", timep );
    p_tm = localtime ( &timep );
    timep = mktime ( p_tm );
    printf ( "time() -> localtime() -> mktime(): %ld\n", timep );
    return 0;
}

asctime

  函数原型如下:

#include <time.h>
char *asctime ( const struct tm *p_tm );

该函数将参数p_tm指向的tm结构体数据转换成实际使用的时间日期表示方法,并以字符串形式返回。

#include <time.h>
#include <stdio.h>

int main ( void ) {
    time_t timep;
    timep = time ( NULL );
    printf ( "%s", asctime ( gmtime ( &timep ) ) );
    return 0;
}

difftime

  函数原型如下:

#include <time.h>
double difftime ( time_t timep1, time_t timep2 );

该函数比较参数timep1timep2时间是否相同,并返回之间相差秒数。

#include <time.h>
#include <stdio.h>
#include <unistd.h>

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值