网站首页 > 文章中心 > 其它

c语言时间函数库

作者:小编 更新时间:2023-09-22 19:16:28 浏览量:92人看过

c语言时间处理函数

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

c语言时间函数库-图1

在头文件中定义了三种类型: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

定义函数

char * asctime(const struct tm * timeptr);

函数说明

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语言时间函数:

①.、获得日历时间函数:

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

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

struct tm * gmtime(const time_t *timer);

struct tm * localtime(const time_t * timer);

C语言函数库 time函数

~~~~~~~~~~~~~~~~~~~~~~~~~~~

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

C语言的时间函数

C/C++中的日期和时间

摘要:

本文从介绍基础概念入手,探讨了在C/C++中对日期和时间操作所用到的数据结构和函数,并对计时、时间的获取、时间的计算和显示格式等方面进行了阐述.本文还通过大量的实例向你展示了time.h头文件中声明的各种函数和数据结构的详细使用方法.

关键字:

UTC(世界标准时间),Calendar Time(日历时间),epoch(时间点),clock tick(时钟计时单元)

①..概念

在C/C++中,对字符串的操作有很多值得注意的问题,同样,C/C++对时间的操作也有许多值得大家注意的地方.最近,在技术群中有很多网友也多次问到过C++语言中对时间的操作、获取和显示等等的问题.下面,在这篇文章中,笔者将主要介绍在C/C++中时间和日期的使用方法.

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

Calendar Time:日历时间,是用"从一个标准时间点到此时的时间经过的秒数"来表示的时间.这个标准时间点对不同的编译器来说会有所不同,但对一个编译系统来说,这个标准时间点是不变的,该编译系统中的时间对应的日历时间都通过该标准时间点来衡量,所以可以说日历时间是"相对时间",但是无论你在哪一个时区,在同一时刻对同一个标准时间点来说,日历时间都是一样的.

epoch:时间点.时间点在标准C/C++中是一个整数,它用此时的时间和标准时间点相差的秒数(即日历时间)来表示.

clock tick:时钟计时单元(而不把它叫做时钟滴答次数),一个时钟计时单元的时间长短是由CPU控制的.一个clock tick不是CPU的一个时钟周期,而是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.\n",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\n", 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

#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 * ctime(const time_t *timer);

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

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

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

int main(void)

struct tm *ptr;

time_t lt;

lt =time(NUL);

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

return 0;

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

struct tm *local;

time_t t;

t=time(NUL);

local=localtime(t);

printf("Local hour is: %d\n",local-tm_hour);

local=gmtime(t);

printf("UTC hour is: %d\n",local-tm_hour);

运行结果是:

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

星期几 月份 日期 时:分:秒 年\n\0

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

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

printf(ctime(t));

等价于:

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 时区名称,如果不能得到时区名称则返回空字符.

%% 百分号

lt=time(NUL);

ptr=localtime();

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

printf(str);

其运行结果为:

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

#include stdio.h

#include time.h

void main( void )

struct tm *newtime;

time_t lt1;

time( 1 );

newtime=localtime(1);

printf(tmpbuf);

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

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

time_t start,end;

start = time(NUL);

end = time(NUL);

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

运行结果为:

请按任意键继续. . .

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

其运行结果是一样的.

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

struct tm t;

time_t t_of_day;

t.tm_mday=1;

t.tm_hour=0;

t.tm_min=0;

t.tm_sec=1;

t.tm_isdst=0;

t_of_day=mktime(t);

printf(ctime(t_of_day));

今天小编给大家带来得是标准C/C++中的有关日期和时间的概念,并通过各种实例讲述了这些函数和数据结构的使用方法.笔者认为,和时间相关的一些概念是相当重要的,理解这些概念是理解各种时间格式的转换的基础,更是应用这些函数和数据结构的基础.

C语言时间函数time_t

①.、time_t // 时间类型(time.h 定义)?

struct tm { // 时间结构,time.h 定义如下:?

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;?

}?

localtime ( rawtime ); //转为当地时间,tm 时间结构?

asctime() // 转为标准ASCII时间格式:?

#include?stdio.h?

#include?time.h?

int?main()

{?

time_t?rawtime;?

struct?tm?*?timeinfo;?

time?(?rawtime?);?

timeinfo?=?localtime?(?rawtime?);?

printf?(?"The?current?date/time?is:?%s",?asctime?(timeinfo)?);?

return?0;

用c语言如何获取系统当前时间的函数?

方法一,#includetime.h

int main()

printf("%d\n",p-tm_sec); /*获取当前秒*/

printf("%d\n",p-tm_min); /*获取当前分*/

printf("%d\n",1+p-tm_mon);/*获取当前月份,范围是0-11,所以要加1*/

方法二.#include?stdio.h

#include?time.h

int?main?()

time_t?t

struct?tm?*?lt;?time?(t);//获取Unix时间戳.

lt?=?localtime?(t);//转为时间结构.

lt-tm_hour,?lt-tm_min,?lt-tm_sec);//输出结果

return?0;}

扩展资料

①.、CTimeSpan类

如果想计算两段时间的差值,可以使用CTimeSpan类,具体使用方法如下:

CTime t = CTime::GetCurrentTime();

CTimeSpan span=t-t1; //计算当前系统时间与时间t1的间隔

int iDay=span.GetDays(); //获取这段时间间隔共有多少天

int iHour=span.GetTotalHours(); //获取总共有多少小时

int iMin=span.GetTotalMinutes();//获取总共有多少分钟

int iSec=span.GetTotalSeconds();//获取总共有多少秒

_timeb定义在SYS\TIMEB.H,有四个fields

dstflag

millitm

time

timezone

void _ftime( struct _timeb *timeptr );

struct _timeb timebuffer;

_ftime( timebuffer );

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

版权声明:倡导尊重与保护知识产权。未经许可,任何人不得复制、转载、或以其他方式使用本站《原创》内容,违者将追究其法律责任。本站文章内容,部分图片来源于网络,如有侵权,请联系我们修改或者删除处理。

编辑推荐

热门文章