linux time.h整理

/*
	 * time(&timer); returns the time since the Epoch (00:00:00 UTC, January 1, 1970), measured in seconds
	 * 如果timer非空,也会把值放入timer
	 */
	time_t timer = time(NULL); //获取具体的时间,以秒计
	
	/**
	 * clock()返回进程运行以来的clock tick数,linux下CLOCKS_PER_SEC=1,000,000
	 * 也就是说返回微妙数,不过从我观察看,其最低三位一般是0,也就是说它能做到毫秒级别的精度
	 * 多用于计算时间差
	 */
	clock_t t;
	int f;
	t = clock();
	printf ("Calculating...\n");
	f = frequency_of_primes (2500000);
	printf ("计算小于 2,500,000的素数的个数 is: %d\n",f);
	t = clock() - t;
	printf ("It took me %ld clicks (%f seconds) unit %ld.\n",t,((float)t)/CLOCKS_PER_SEC, CLOCKS_PER_SEC);
	
	/**
	 * double difftime (time_t end, time_t beginning);
	 * 返回end-beginning的秒数,保存为浮点型数据
	 */
	
	
	struct tm {
               int tm_sec;         /* seconds 0-59*/
               int tm_min;         /* minutes 0-59*/
               int tm_hour;        /* hours 0-23*/
               int tm_mday;        /* day of the month 1-31*/
               int tm_mon;         /* month 0-11*/
               int tm_year;        /* year 真实年份-1900*/
               int tm_wday;        /* day of the week */
               int tm_yday;        /* day in the year */
               int tm_isdst;       /* daylight saving time */
           };
	
	/**
	 * time_t mktime (struct tm * timeptr);
	 * 把tm结构体转换为time_t格式的时间,tm_wday与tm_yday的值被忽略,其他域值即使超出有效返回,也会被合理转换,如tm_mday超过31时,也会被转换为之后月份的某一天
	 * timeptr指向的值有可能会被改变:如果tm_wday与tm_yday越过其有效范围或者与其他域的值不符,则会被调整为正确的值
	 */
	
	/**
	 * struct tm * localtime (const time_t * timer);
	 * 把time_t时间转换为tm时间,非线程安全
	 * 其线程安全版本为:struct tm *localtime_r(const time_t *timep, struct tm *result);
	 */
	
	/**
	 * size_t strftime (char* ptr, size_t maxsize, const char* format, const struct tm* timeptr );
	 * maxsize 包含结束符'\0'的长度
	 * format: %F表示YYYY-MM-DD,等效于%Y-%m-%d;%T表示HH:MM:SS,等效于%H:%M:%S
	 */
	


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值