linux系统调用之时间函数

版权声明:转载请声明 https://blog.csdn.net/qq_40732350/article/details/82011571

1.常用的时间函数

man 2 中

       time_t time(time_t *t);

一个man 3 中

       #include <time.h>

       char *asctime(const struct tm *tm);
       char *asctime_r(const struct tm *tm, char *buf);

       char *ctime(const time_t *timep);
       char *ctime_r(const time_t *timep, char *buf);

       struct tm *gmtime(const time_t *timep);
       struct tm *gmtime_r(const time_t *timep, struct tm *result);

       struct tm *localtime(const time_t *timep);
       struct tm *localtime_r(const time_t *timep, struct tm *result);

       time_t mktime(struct tm *tm);


man 2 中

       size_t strftime(char *s, size_t max, const char *format, const struct tm *tm);

一个man 3 中

       #include <sys/time.h>

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

       int settimeofday(const struct timeval *tv, const struct timezone *tz);
 

2.time()

SYNOPSIS
       #include <time.h>

       time_t time(time_t *t);

DESCRIPTION
       time()  returns  the  time  as the number of seconds since the Epoch, 1970-01-01 00:00:00 +0000
       (UTC).

       If t is non-NULL, the return value is also stored in the memory pointed to by t.

RETURN VALUE
       On success, the value of time in seconds since the Epoch is returned.  On error,  ((time_t) -1)
       is returned, and errno is set appropriately.
/*******************   测试代码  ********************/

    time_t tNow = -1;

   time(&tNow);                // 指针做输出型参数

    if (tNow < 0)

    {

        perror("time");

        return -1;

    }

    printf("time()函数******************\n");
    printf("time: %ld\n\n", tNow);

返回值:为1970-01-01 00:00:00 +0000 (UTC).  到今天的秒数

结果:

3.ctime/asctime/gmtime/localtime/mktime 函数

 

       #include <time.h>

       char *asctime(          const struct tm *tm);  //获得这种Fri Aug 24 09:08:01 2018  的时间
       char *asctime_r(         const struct tm *tm,              char *buf);

       char *ctime(                     const time_t *timep); //获得这种Fri Aug 24 09:08:01 2018  的时间
       char *ctime_r(                 const time_t *timep,          char *buf);

       struct tm *gmtime(                const time_t *timep);  //获得国际时间
       struct tm *gmtime_r(               const time_t *timep,            struct tm *result);

       struct tm *localtime(                  const time_t *timep);//获得本地时间
       struct tm *localtime_r(                const time_t *timep,                struct tm *result);

       time_t mktime(struct tm *tm);  //把年月日时间变为秒时间,主要用来设置时间
结构体:

           struct tm {
               int tm_sec;         /* seconds */
               int tm_min;         /* minutes */
               int tm_hour;        /* hours */
               int tm_mday;        /* day of the month */
               int tm_mon;         /* month */
               int tm_year;        /* year */
               int tm_wday;        /* day of the week */
               int tm_yday;        /* day in the year */
               int tm_isdst;       /* daylight saving time */
           };
结构体参数格式:

       tm_sec    The  number of seconds after the minute, normally in the range 0 to 59, but can be up
                 to 60 to allow for leap seconds.

       tm_min    The number of minutes after the hour, in the range 0 to 59.

       tm_hour   The number of hours past midnight, in the range 0 to 23.

       tm_mday   The day of the month, in the range 1 to 31.

       tm_mon    The number of months since January, in the range 0 to 11.

       tm_year   The number of years since 1900.

       tm_wday   The number of days since Sunday, in the range 0 to 6.

       tm_yday   The number of days since January 1, in the range 0 to 365.

       tm_isdst  A flag that indicates  whether  daylight  saving  time  is  in  effect  at  the  time
                 described.  The value is positive if daylight saving time is in effect, zero if it is
                 not, and negative if the information is not available.
/*******************   测试代码  ********************/

    time_t tNow = -1;
    struct tm tmNow;

    time(&tNow);                // 指针做输出型参数

    if (tNow < 0)

    {

        perror("time");

        return -1;

    }

    // ctime
    printf("ctime()函数******************\n");
    printf("ctime: %s\n\n", ctime(&tNow));

    // gmtime_r 
    printf("gmtime_r()函数******************\n");
    memset(&tmNow, 0, sizeof(tmNow));
    gmtime_r(&tNow, &tmNow);
    printf("年%d月%d日%d时%d\n\n", tmNow.tm_year, tmNow.tm_mon, tmNow.tm_mday, tmNow.tm_hour);

    //localtime_r
    printf("localtime_r()函数******************\n");
    memset(&tmNow, 0, sizeof(tmNow));
    localtime_r(&tNow, &tmNow);
    printf("年%d月%d日%d时%d\n\n", tmNow.tm_year, tmNow.tm_mon, tmNow.tm_mday, tmNow.tm_hour);
  

   //mktime

    printf("mktime()函数******************\n");    
    memset(&tmNow, 0, sizeof(tmNow));
    localtime_r(&tNow, &tmNow);    
    printf("time: %ld\n\n", mktime(&tmNow));    
    
    // asctime
    printf("asctime()函数******************\n");        
    memset(&tmNow, 0, sizeof(tmNow));
    localtime_r(&tNow, &tmNow);
    printf("asctime:%s\n", asctime(&tmNow));

结果:

4.strftime()函数

       #include <time.h>    //它和printf()一样类似的,可以格式化输出,来达到与众不同的格式输出

       size_t strftime(char *s, size_t max, const char *format, const struct tm *tm);
格式符号:  这里不全,具体查man 手册

       %a     The abbreviated weekday name according to the current locale.

       %A     The full weekday name according to the current locale.

       %b     The abbreviated month name according to the current locale.

       %B     The full month name according to the current locale.

       %c     The preferred date and time representation for the current locale.

       %C     The century number (year/100) as a 2-digit integer. (SU)

/*******************   测试代码  ********************/

    printf("strftime()函数******************\n");        
    memset(&tmNow, 0, sizeof(tmNow));
    localtime_r(&tNow, &tmNow);
    memset(buf, 0, sizeof(buf));
    strftime(buf, sizeof(buf), "%Y * %m * %d, %H-%M-%S.", &tmNow);
    printf("时间为:[%s]\n", buf);

结果:

5.gettimeofday和settimeofday ()函数

gettimeofday返回的时间是由struct timeval和struct timezone这两个结构体来共同表示的,

其中timeval表示时间,而timezone表示时区。

settimeofday是用来设置当前的时间和时区的。

 

       #include <sys/time.h>

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

       int settimeofday(const struct timeval *tv, const struct timezone *tz);
结构体:

           struct timeval {
               time_t      tv_sec;     /* seconds */   //秒
               suseconds_t tv_usec;    /* microseconds */  //微秒
           };

           struct timezone {
               int tz_minuteswest;     /* minutes west of Greenwich */   //以国际时间的小时,时间以分钟为单位
               int tz_dsttime;         /* type of DST correction */  //不知道是啥
           };
/*******************   测试代码  ********************/

    printf("asctime()函数******************\n");        
    ret = gettimeofday(&tv, &tz);
    if (ret < 0)
    {
        perror("gettimeofday");
        return -1;
    }
    printf("seconde: %ld.\n", tv.tv_sec);
    printf("microseconds: %ld.\n", tv.tv_usec);    
    printf("timezone:%d.\n", tz.tz_minuteswest);    
    printf("timezone:%d.\n", tz.tz_dsttime);  

结果:

6.总的实例代码

#include <stdio.h>
#include <time.h>
#include <string.h>
#include <sys/time.h>
int main(void)
{
	time_t tNow = -1;
	struct tm tmNow;
	char buf[100];
	time(&tNow);				// 指针做输出型参数
	struct timeval tv;
	struct timezone tz;
	int ret;
	if (tNow < 0)
	{
		perror("time");
		return -1;
	}
	//time
	printf("time()函数******************\n");
	printf("time: %ld\n\n", tNow);

	// ctime
	printf("ctime()函数******************\n");
	printf("ctime: %s\n\n", ctime(&tNow));

	// gmtime_r 
	printf("gmtime_r()函数******************\n");
	memset(&tmNow, 0, sizeof(tmNow));
	gmtime_r(&tNow, &tmNow);
	printf("年%d月%d日%d时%d\n\n", tmNow.tm_year, tmNow.tm_mon, tmNow.tm_mday, tmNow.tm_hour);

	//localtime_r
	printf("localtime_r()函数******************\n");
	memset(&tmNow, 0, sizeof(tmNow));
	localtime_r(&tNow, &tmNow);
	printf("年%d月%d日%d时%d\n\n", tmNow.tm_year, tmNow.tm_mon, tmNow.tm_mday, tmNow.tm_hour);

	//mktime
	printf("mktime()函数******************\n");	
	memset(&tmNow, 0, sizeof(tmNow));
	localtime_r(&tNow, &tmNow);	
	printf("time: %ld\n\n", mktime(&tmNow));	
	
	// asctime
	printf("asctime()函数******************\n");		
	memset(&tmNow, 0, sizeof(tmNow));
	localtime_r(&tNow, &tmNow);
	printf("asctime:%s\n", asctime(&tmNow));

	// strftime
	printf("strftime()函数******************\n");		
	memset(&tmNow, 0, sizeof(tmNow));
	localtime_r(&tNow, &tmNow);
	memset(buf, 0, sizeof(buf));
	strftime(buf, sizeof(buf), "%Y * %m * %d, %H-%M-%S.", &tmNow);
	printf("时间为:[%s]\n\n", buf);

	// gettimeofday
	printf("asctime()函数******************\n");		
	ret = gettimeofday(&tv, &tz);
	if (ret < 0)
	{
		perror("gettimeofday");
		return -1;
	}
	printf("seconde: %ld.\n", tv.tv_sec);
	printf("microseconds: %ld.\n", tv.tv_usec);	
	printf("timezone:%d.\n", tz.tz_minuteswest);	
	printf("timezone:%d.\n", tz.tz_dsttime);
	return 0;
}

结果:

猜你喜欢

转载自blog.csdn.net/qq_40732350/article/details/82011571