APUE学习笔记--系统数据文件和信息--时间相关的函数(time、localtime、gmtime、mktime、strftime)

  • time

    • 函数定义
        #include <time.h>
        time_t time(time_t *t);
      
    • 功能描述
      time() returns the time since the Epoch (00:00:00 UTC, January 1, 1970), measured in seconds.
      If t is non-NULL, the return value is also stored in the memory pointed to by t.
    • 返回值
      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.
  • localtime

    • 函数定义
        #include <time.h>
        struct tm *localtime(const time_t *timep);
        struct tm *localtime_r(const time_t *timep, struct tm *result);
        
       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 */
       };
      
    • 功能描述
      • 通过UTC时间,得到存放在tm结构体中的程序员喜欢的时间格式。返回的是系统设置的时区的时间。
      • localtime通过timep指针输入一个日历时间(相对于UTC时间过去的秒数),返回一个tm结构体。数据存储在静态区,下一次调用会被覆盖。
      • localtime_r跟localtime一样,只是数据存放在result这个用户指定的在用户区中的位置,不再用担心被覆盖的问题。
      • 失败返回NULL。
  • gmtime

    • 函数定义
        #include <time.h>
        struct tm *gmtime(const time_t *timep);
        struct tm *gmtime_r(const time_t *timep, struct tm *result);
      
    • 功能描述
      • 与localtime几乎一样,只是它返回是UTC时间。根据时区之间的时差,localtime返回的时间与gmtime返回的时间之间可以互相转化。
      • 失败返回NULL。
  • mktime

    • 函数定义
        	#include <time.h>
        	time_t mktime(struct tm *tm);
      
    • 功能描述
      • 只需要在tm结构体中给出年月日时分秒,其他的三个部分,会被自动填充。其中的tm_isdst会被填充为正数或者0。
      • 返回一个初始时间过去的秒数值。
      • 只有这个函数的tm结构体没有const修饰。
      • 有自动纠错功能。比如6月31号,使用mktime后,自动转位7月1号。
      • 失败返回-1.
  • strftime

    • 函数定义
        #include <time.h>
        size_t strftime(char *s, size_t max, const char *format,
                   const struct tm *tm);
      
    • 功能描述
      • 把tm结构体中的时间通过format格式化输出到最大为max的s字符串中去。
      • 关于format的详细信息,使用man手册去查吧。
  • 例子(使用time、localtime、strftime)

/********************************************************
*	函数功能:每隔一秒,函数向输出文件中输出现在的系统时间,带行号。
*			 当下次再向这个输出文件输出时,依然能获知行号。
*********************************************************/

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

#define _GNU_SOURCE
#define FNAME "/tmp/out"

int main()
{
        time_t ptime;
        struct tm * tm;
        FILE *  fp;
        int count = 0;

        fp = fopen(FNAME,"a+");
        if(fp == NULL)
        {
                perror("fopen()");
                exit(0);
        }
        time(&ptime);
        tm = localtime(&ptime);
        
        while(getline(NULL,0,fp) != -1)//获取log文件的行数,方便分时多次输出,还能知道行号
                count++;
	
        while(1)
        {

                fprintf(fp,"%-4d%d-%d-%d %d:%d:%d\n",++count,tm->tm_year+1900 \
			,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec);
		
		fflush(fp);//fprintf是缓冲IO,必须要立即压缓冲区,否则不能及时看到文件中的信息。
		sleep(1);//睡眠一秒
	}
	fclose(fp);
	exit(0);
}

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

       int
       main(int argc, char *argv[])
       {
           char outstr[200];
           time_t t;
           struct tm *tmp;

           t = time(NULL);
           tmp = localtime(&t);
           if (tmp == NULL) {
               perror("localtime");
               exit(EXIT_FAILURE);
           }

           if (strftime(outstr, sizeof(outstr), argv[1], tmp) == 0) {
               fprintf(stderr, "strftime returned 0");
               exit(EXIT_FAILURE);
           }

           printf("Result string is \"%s\"\n", outstr);
           exit(EXIT_SUCCESS);
       } /* main */

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值