当前位置:首页 > 正文

c语言时间函数对照表,c语言_c语言 时间函数

更新时间:2025-01-21 03:58 阅读量:85394

C语言的比较两个时间的函数

①.、方法一:若时间为结构体变量,比较两个时间的大小,而且不能改变时间的值,可以是:

{

if(date1- year==date1- year)

else

}

time_t start, end;

double diff;

memset(tm1, 0, sizeof(tm1));

strptime(t1, "%Y%m%d", tm1);

start = mktime(tm1);

diff = difftime(start, end);

调用:

(1)、获得日历时间函数:

可以通过time()函数来获得日历时间(Calendar Time),其原型为:time_t time(time_t * timer);

①.月1日0时0分0秒)到现在此时的秒数.如果参数为空(NUL),函数将只通过返回值返回现在的日历时间,比如下面这个例子用来显示当前的日历时间:

其中可以使用的函数是gmtime()和localtime(),这两个函数的原型为:

struct tm * gmtime(const time_t *timer);

struct tm * localtime(const time_t * timer);

其中gmtime()函数是将日历时间转化为世界标准时间(即格林尼治时间),并返回一个tm结构体来保存这个时间,而localtime()函数是将

C语言中时间的函数

一.概念

在C/C++中,通过学习许多C/C++库,你可以有很多操作、使用时间的方法.但在这之前你需要了解一些"时间"和"日期"的概念,主要有以下几个:

我们可以使用ANSI标准库中的time.h头文件.这个头文件中定义的时间和日期所使用的方法,无论是在结构定义,还是命名,都具有明显的C语言风格.下面,我将说明在C/C++中怎样使用日期的时间功能.

二. 介绍

① 计时

C/C++中的计时函数是clock(),而与其相关的数据类型是clock_t.在MSDN中,查得对clock函数定义如下:

clock_t clock( void );

这个函数返回从"开启这个程序进程"到"程序中调用clock()函数"时之间的CPU时钟计时单元(clock tick)数,在MSDN中称之为挂钟时间(wal-clock).其中clock_t是用来保存时间的数据类型,在time.h文件中,我们可以找到对它的定义:

#ifndef _CLOCK_T_DEFINED

typedef long clock_t;

#define _CLOCK_T_DEFINED

#endif

很明显,clock_t是一个长整形数.在time.h文件中,还定义了一个常量CLOCKS_PER_SEC,它用来表示一秒钟会有多少个时钟计时单元,其定义如下:

#define CLOCKS_PER_SEC ((clock_t)1000)

可以看到每过千分之一秒(1毫秒),调用clock()函数返回的值就加1.下面举个例子,你可以使用公式clock()/CLOCKS_PER_SEC来计算一个进程自身的运行时间:

void elapsed_time()

printf("Elapsed time:%u secs. ",clock()/CLOCKS_PER_SEC);

当然,你也可以用clock函数来计算你的机器运行一个循环或者处理其它事件到底花了多少时间:

/* 测量一个事件持续的时间*/

#include "stdio.h"

#include "stdlib.h"

#include "time.h"

int main( void )

long i = 10000000L;

clock_t start, finish;

double duration;

printf( "Time to do %ld empty loops is ", i );

start = clock();

while( i-- ) ;

finish = clock();

duration = (double)(finish - start) / CLOCKS_PER_SEC;

printf( "%f seconds ", duration );

system("pause");

在笔者的机器上,运行结果如下:

上面我们看到时钟计时单元的长度为1毫秒,那么计时的精度也为1毫秒,那么我们可不可以通过改变CLOCKS_PER_SEC的定义,通过把它定义的大一些,从而使计时精度更高呢?通过尝试,你会发现这样是不行的.在标准C/C++中,最小的计时单位是一毫秒.

在标准C/C++中,我们可通过tm结构来获得日期和时间,tm结构在time.h中的定义如下:

#ifndef _TM_DEFINED

struct tm {

int tm_mon; /* 月份(从一月开始,0代表一月) - 取值区间为[0,11] */

int tm_isdst; /* 夏令时标识符,实行夏令时的时候,tm_isdst为正.不实行夏令时的进候,tm_isdst为0;不了解情况时,tm_isdst()为负.*/

};

#define _TM_DEFINED

ANSI C标准称使用tm结构的这种时间表示为分解时间(broken-down time).

#ifndef _TIME_T_DEFINED

typedef long time_t; /* 时间值 */

#define _TIME_T_DEFINED /* 避免重复定义 time_t */

在time.h头文件中,我们还可以看到一些函数,它们都是以time_t为参数类型或返回值类型的函数:

double difftime(time_t time1, time_t time0);

time_t mktime(struct tm * timeptr);

time_t time(time_t * timer);

char * asctime(const struct tm * timeptr);

char * ctime(const time_t *timer);

此外,time.h还提供了两种不同的函数将日历时间(一个用time_t表示的整数)转换为我们平时看到的把年月日时分秒分开显示的时间格式tm:

在本节,我将向大家展示怎样利用time.h中声明的函数对时间进行操作.这些操作包括取当前时间、计算时间间隔、以不同的形式显示时间等内容.

我们可以通过time()函数来获得日历时间(Calendar Time),其原型为:

运行的结果与当时的时间有关,我当时运行的'结果是:

/* Author: Eman Lee */

int main(void)

time_t lt;

lt =time(NULL);

printf("The Calendar Time now is %d ",lt);

return 0;

//本地时间,世界标准时间

struct tm *local;

time_t t;

t=time(NULL);

local=localtime(t);

printf("Local hour is: %d:%d:%d ",local-tm_hour,local-tm_min,local-tm_sec);

local=gmtime(t);

printf("UTC hour is: %d:%d:%d ",local-tm_hour,local-tm_min,local-tm_sec);

运行结果是:

我们可以通过asctime()函数和ctime()函数将时间以固定的格式显示出来,两者的返回值都是char*型的字符串.返回的时间格式为:

星期几 月份 日期 时:分:秒 年

其中 是一个换行符,是一个空字符,表示字符串结束.下面是两个函数的原型:

其中asctime()函数是通过tm结构来生成具有固定格式的保存时间信息的字符串,而ctime()是通过日历时间来生成时间字符串.这样的话,asctime()函数只是把tm结构对象中的各个域填到时间字符串的相应位置就行了,而ctime()函数需要先参照本地的时间设置,把日历时间转化为本地时间,然后再生成格式化后的字符串.在下面,如果t是一个非空的time_t变量的话,那么:

printf(ctime(t));

等价于:

struct tm *ptr;

ptr=localtime(t);

printf(asctime(ptr));

那么,下面这个程序的两条printf语句输出的结果就是不同的了(除非你将本地时区设为世界标准时间所在的时区):

ptr=gmtime();

printf(ctime());

运行结果:

我们可以使用strftime()函数将时间格式化为我们想要的格式.它的原型如下:

size_t strftime(

char *strDest,

size_t maxsize,

const char *format,

const struct tm *timeptr

);

我们可以根据format指向字符串中格式命令把timeptr中保存的时间信息放在strDest指向的字符串中,最多向strDest中存放maxsize个字符.该函数返回向strDest指向的字符串中放置的字符数.

函数strftime()的操作有些类似于sprintf():识别以百分号(%)开始的格式命令集合,格式化输出结果放在一个字符串中.格式化命令说明串strDest中各种日期和时间信息的确切表示方法.格式串中的其他字符原样放进串中.格式命令列在下面,它们是区分大小写的.

%a 星期几的简写

%A 星期几的全称

%b 月分的简写

%B 月份的全称

%c 标准的日期的时间串

%C 年份的后两位数字

%d 十进制表示的每月的第几天

%D 月/天/年

%e 在两字符域中,十进制表示的每月的第几天

%F 年-月-日

%g 年份的后两位数字,使用基于周的年

%G 年分,使用基于周的年

%h 简写的月份名

%j 十进制表示的每年的第几天

%m 十进制表示的月份

%M 十时制表示的分钟数

%n 新行符

%p 本地的AM或PM的等价显示

%R 显示小时和分钟:hh:mm

%S 十进制的秒数

%t 水平制表符

%T 显示时分秒:hh:mm:ss

%V 每年的第几周,使用基于周的年

%x 标准的日期串

%X 标准的时间串

%Y 带世纪部分的十进制年份

%z,%Z 时区名称,如果不能得到时区名称则返回空字符.

%% 百分号

time_t localTime;

localTime=time(NULL);

ptr=localtime(localTime);

strftime(str,100,"It is now %I %p ",ptr);

printf(str);

其运行结果为:

而下面的程序则显示当前的完整日期:

//显示当前的完整日期

void main( void )

struct tm *newtime;

time_t localTime1;

time( localTime1 );

newtime=localtime(localTime1);

printf(tmpbuf);

有时候在实际应用中要计算一个事件持续的时间长度,比如计算打字速度.在第1节计时部分中,我已经用clock函数举了一个例子.Clock()函数可以精确到毫秒级.同时,我们也可以使用difftime()函数,但它只能精确到秒.该函数的定义如下:

虽然该函数返回的以秒计算的时间间隔是double类型的,但这并不说明该时间具有同double一样的精确度,这是由它的参数觉得的(time_t是以秒为单位计算的).比如下面一段程序:

//计算持续时间的长度

time_t start,end;

start = time(NULL);

end = time(NULL);

printf("The pause used %f seconds. ",difftime(end,start));//-

运行结果为:

请按任意键继续. . .

printf("The pause used %f seconds. ",end-start);

其运行结果是一样的.

这里说的分解时间就是以年、月、日、时、分、秒等分量保存的时间结构,在C/C++中是tm结构.我们可以使用mktime()函数将用tm结构表示的时间转化为日历时间.其函数原型如下:

struct tm time;

time_t t_of_day;

time.tm_mday=1;

time.tm_hour=0;

time.tm_min=0;

time.tm_sec=1;

time.tm_isdst=0;

t_of_day=mktime(time);

printf(ctime(t_of_day));

c语言时间处理函数

C语言的标准库函数包括一系列日期和时间处理函数,它们都在头文件中说明.

在头文件中定义了三种类型:time_t,struct tm和clock_t.

下面列出了这些函数.

time_t time(time_t *timer);

struct tm *gmtime(const time_t *timer);

struct tm *localtime(const time_t *timer);

char *asctime(const struct tm *timeptr);

char *ctime(const time_t *timer);

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

time_t mktime(struct tm *timeptr);

clock_t clock(void);

【具体应用举例】

asctime(将时间和日期以字符串格式表示)

相关函数

time,ctime,gmtime,localtime

表头文件

#i nclude

定义函数

函数说明

asctime()将参数timeptr所指的tm结构中的信息转换成真实世界所使用的时间日期表示方法,然后将结果以字符串形态返回.

返回值

若再调用相关的时间日期函数,此字符串可能会被破坏.此函数与ctime不同处在于传入的参数是不同的结构.

附加说明

返回一字符串表示目前当地的时间日期.

范例

main()

time_t timep;

time (timep);

printf("%s",asctime(gmtime(timep)));

执行

ctime(将时间和日期以字符串格式表示)

time,asctime,gmtime,localtime

char *ctime(const time_t *timep);

ctime ()将参数timep所指的time_t结构中的信息转换成真实世界所使用的时间日期表示方法,然后将结果以字符串形态返回.

printf("%s",ctime(timep));

gettimeofday(取得目前的时间)

time,ctime,ftime,settimeofday

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

gettimeofday()会把目前的时间有tv所指的结构返回,当地时区的信息则放到tz所指的结构中.

timeval结构定义为:

struct timeval{

long tv_sec; /*秒*/

long tv_usec; /*微秒*/

timezone 结构定义为:

struct timezone{

int tz_minuteswest; /*和Greenwich 时间差了多少分钟*/

int tz_dsttime; /*日光节约时间的状态*/

上述两个结构都定义在/usr/include/sys/time.h.tz_dsttime 所代表的状态如下

DST_NONE /*不使用*/

DST_USA /*美国*/

DST_AUST /*澳洲*/

DST_WET /*西欧*/

DST_MET /*中欧*/

DST_EET /*东欧*/

DST_CAN /*加拿大*/

DST_GB /*大不列颠*/

DST_RUM /*罗马尼亚*/

DST_TUR /*土耳其*/

成功则返回0,失败返回-1,错误代码存于errno.附加说明EFAULT指针tv和tz所指的内存空间超出存取权限.

main(){

struct timeval tv;

struct timezone tz;

gettimeofday (tv , tz);

printf("tv_sec; %d\n", tv,.tv_sec) ;

printf("tv_usec; %d\n",tv.tv_usec);

printf("tz_minuteswest; %d\n", tz.tz_minuteswest);

printf("tz_dsttime, %d\n",tz.tz_dsttime);

tz_dsttime:0

gmtime(取得目前时间和日期)

time,asctime,ctime,localtime

struct tm*gmtime(const time_t*timep);

gmtime()将参数timep 所指的time_t 结构中的信息转换成真实世界所使用的时间日期表示方法,然后将结果由结构tm返回.

结构tm的定义为

struct tm

int tm_sec;

int tm_min;

int tm_hour;

int tm_mday;

int tm_mon;

int tm_year;

int tm_wday;

int tm_yday;

int tm_isdst;

int tm_mon 代表目前月份,从一月算起,范围从0-11

int tm_isdst 日光节约时间的旗标

此函数返回的时间日期未经时区转换,而是UTC时间.

返回结构tm代表目前UTC 时间

char *wday[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};

struct tm *p;

time(timep);

p=gmtime(timep);

printf("%s%d;%d;%d\n", wday[p-tm_wday], p-tm_hour, p-tm_min, p-tm_sec);

localtime(取得当地目前时间和日期)

time, asctime, ctime, gmtime

struct tm *localtime(const time_t * timep);

localtime()将参数timep所指的time_t结构中的信息转换成真实世界所使用的时间日期表示方法,然后将结果由结构tm返回.

结构tm的定义请参考gmtime().此函

数返回的时间日期已经转换成当地时区.

返回结构tm代表目前的当地时间.

p=localtime(timep); /*取得当地时间*/

printf("%s%d:%d:%d\n", wday[p-tm_wday],p-tm_hour, p-tm_min, p-tm_sec);

mktime(将时间结构数据转换成经过的秒数)

time_t mktime(strcut tm * timeptr);

返回经过的秒数.

/* 用time()取得时间(秒数),利用localtime()

转换成struct tm 再利用mktine()将struct tm转换成原来的秒数*/

strcut tm *p;

printf("time() : %d \n",timep);

p=localtime(timep);

timep = mktime(p);

printf("time()-localtime()-mktime():%d\n",timep);

settimeofday(设置目前时间)

time,ctime,ftime,gettimeofday

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

settimeofday()会把目前时间设成由tv所指的结构信息,当地时区信息则设成tz所指的结构.详细的说明请参考gettimeofday().

注意,只有root权限才能使用此函数修改时间.

成功则返回0,失败返回-1,错误代码存于errno.

错误代码

EPERM 并非由root权限调用settimeofday(),权限不够.

EINVAL 时区或某个数据是不正确的,无法正确设置时间.

time(取得目前的时间)

ctime,ftime,gettimeofday

time_t time(time_t *t);

此函数也会将返回值存到t指针所指的内存.

成功则返回秒数,失败则返回((time_t)-1)值,错误原因存于errno中.

mian()

int seconds= time((time_t*)NULL);

printf("%d\n",seconds);

Date and Time Functions: time.h

The header time.h declares types and functions for manipulating date and time. Some functions process local time,

which may differ from calendar time, for example because of time zone. clock_t and time_t are arithmetic types

representing times, and struct tm holds the components

of a calendar time:

int tm_mon; months since January (0,11)

int tm_isdst; Daylight Saving Time flag

tm_isdst is positive if Daylight Saving Time is in effect, zero if not, and negative if the information is not available.

clock_t clock(void)

clock returns the processor time used by the program since the beginning of execution, or -1 if unavailable.

clock()/CLK_PER_SEC is a time in

seconds.

time_t time(time_t *tp)

time returns the current calendar time or -1 if the time is not available. If tp is not NULL,

the return value is also assigned to *tp.

time_t mktime(struct tm *tp)

mktime converts the local time in the structure *tp into calendar time in the same representation used by time.

The components will have values

in the ranges shown. mktime returns the calendar time or -1 if it cannot be represented.

The next four functions return pointers to static objects that may be overwritten by other calls.

char *asctime(const struct tm *tp)

asctime*tp into a string of the form

char *ctime(const time_t *tp)

ctime converts the calendar time *tp to local time; it is equivalent to

asctime(localtime(tp))

struct tm *gmtime(const time_t *tp)

gmtime converts the calendar time *tp into Coordinated Universal Time (UTC). It returns NULL if UTC is not available.

The name gmtime has historical significance.

struct tm *localtime(const time_t *tp)

localtime converts the calendar time *tp into local time.

size_t strftime(char *s, size_t smax, const char *fmt, const struct tm *tp)

strftime formats date and time information from *tp into s according to fmt, which is analogous to a printf format.

Ordinary characters (including the terminating '\0') are copied into s. Each %c is replaced as described below,

using values appropriate for the local environment.

No more than smax characters are placed into s. strftime returns the number of characters, excluding the '\0',

or zero if more than smax characters were produced.

%a abbreviated weekday name.

%A full weekday name.

%b abbreviated month name.

%B full month name.

%c local date and time representation.

%p local equivalent of AM or PM.

%x local date representation.

%X local time representation.

%Y year with century.

%Z time zone name, if any.

%% %

以上就是思力小常识小编为大家整理的c语言时间函数对照表,c语言相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!