c library-time.h

本文详细介绍了C语言中关于时间的各种操作,包括获取时间、计算时间差、格式化时间等核心函数的使用方法。同时也解释了如何利用这些函数来处理日期和时间相关的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

时间操作:

clock_t clock ( void ); 返回自程序运行以来

double difftime ( time_t time2, time_t time1 );返回时间差秒,time2要大于time1

time_t mktime ( struct tm * timeptr );转换tm至time_t

time_t time ( time_t * timer );返回自1970后的秒数,timer可为NULL,为保存时间用


时间格式化:

char * asctime ( const struct tm * timeptr );按格式“Www Mmm dd hh:mm:ss yyyy”,末尾有\n,例如:“Sat May 20 15:21:51 2000”

char * ctime ( const time_t * timer );同上,只不过是time_t

struct tm * gmtime ( const time_t * timer );返回转换至UTC时间

struct tm * localtime ( const time_t * timer );返回转换至当前时间

size_t strftime ( char * ptr, size_t maxsize, const char * format, const struct tm * timeptr );

format列表:

%a Abbreviated weekday name * Thu 

%A Full weekday name *  Thursday 

%b Abbreviated month name * Aug 

%B Full month name * August 

%c Date and time representation * Thu Aug 23 14:55:02 2001 

%d Day of the month (01-31) 23 

%H Hour in 24h format (00-23) 14 

%I Hour in 12h format (01-12) 02 

%j Day of the year (001-366) 235 

%m Month as a decimal number (01-12) 08 

%M Minute (00-59) 55 

%p AM or PM designation PM 

%S Second (00-61) 02 

%U Week number with the first Sunday as the first day of week one (00-53) 33 

%w Weekday as a decimal number with Sunday as 0 (0-6) 4 

%W Week number with the first Monday as the first day of week one (00-53) 34 

%x Date representation * 08/23/01 

%X Time representation * 14:55:02 

%y Year, last two digits (00-99) 01 

%Y Year 2001 

%Z Timezone name or abbreviation CDT 

%% A % sign % 


宏:

CLOCKS_PER_SEC表示每秒有多少个clock tickets


类型:

clock_t 由clock函数返回,运行整数运算

time_t时间戳,1970后的秒数

struct tm日历时间,包括以下字段:

int tm_sec; 0-61*,某些系统上会超出

int tm_min; 0-59

int tm_hour; 0-23

int tm_mday; 1-31

int tm_mon; 0-11 

int tm_year; years since 1900

int tm_wday; 0-6

int tm_yday; 0-365

int tm_isdst;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值