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

ftime函数c语言

作者:小编 更新时间:2023-08-29 21:29:37 浏览量:100人看过

用C语言实现数据结构时需要哪些头文件?

①.、stdio.h,这个是用来输入输出.动态内存分配函数要用到的,所以要包含malloc.h,其他的还可能用到字符(串)处理函数(需包含string.h头文件)、数学函数(需包含math.h),包含什么头文件主要看i你想用这些数据结构实现什么功能,用到什么函数.

ALLOC.H 说明内存管理函数(分配、释放等).

ASSERT.H 定义 assert调试宏.

BIOS.H 说明调用IBM—PC ROM BIOS子程序的各个函数.

CONIO.H 说明调用DOS控制台I/O子程序的各个函数.

CTYPE.H 包含有关字符分类及转换的名类信息(如 isalpha和toascii等).

DIR.H 包含有关目录和路径的结构、宏定义和函数.

ftime函数c语言-图1

ERRON.H 定义错误代码的助记符.

FCNTL.H 定义在与open库子程序连接时的符号常量.

FLOAT.H 包含有关浮点运算的一些参数和函数.

GRAPHICS.H 说明有关图形功能的各个函数,图形错误代码的常量定义,正对不同驱动程序的各种颜色值,及函数用到的一些特殊结构.

IO.H 包含低级I/O子程序的结构和说明.

ftime函数c语言-图2

LIMIT.H 包含各环境参数、编译时间限制、数的范围等信息.

MATH.H 说明数学运算函数,还定了 HUGE VAL 宏, 说明了matherr和matherr子程序用到的特殊结构.

MEM.H 说明一些内存操作函数(其中大多数也在STRING.H中说明).

PROCESS.H 说明进程管理的各个函数,spawn...和EXEC ...函数的结构说明.

SETJMP.H 定义longjmp和setjmp函数用到的jmp buf类型,说明这两个函数.

SHARE.H 定义文件共享函数的参数.

SIGNAL.H 定义SIG[ZZ(Z] [ZZ)]IGN和SIG[ZZ(Z] [ZZ)]DFL常量,说明rajse和signal两个函数.

STDARG.H 定义读函数参数表的宏.(如vprintf,vscarf函数).

STDDEF.H 定义一些公共数据类型和宏.

STDIO.H 定义Kernighan和Ritchie在Unix System V 中定义的标准和扩展的类型和宏.还定义标准I/O 预定义流:stdin,stdout和stderr,说明 I/O流子程序.

STDLIB.H 说明一些常用的子程序:转换子程序、搜索/ 排序子程序等.

STRING.H 说明一些串操作和内存操作函数.

SYS\STAT.H 定义在打开和创建文件时用到的一些符号常量.

SYS\TYPES.H 说明ftime函数和timeb结构.

SYS\TIME.H 定义时间的类型time[ZZ(Z] [ZZ)]t.

TIME.H 定义时间转换子程序asctime、localtime和gmtime的结构,ctime、 difftime、 gmtime、 localtime和stime用到的类型,并提供这些函数的原型.

VALUE.H 定义一些重要常量,包括依赖于机器硬件的和为与Unix System V相兼容而说明的一些常量,包括浮点和双精度值的范围.

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

定义函数

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语言如何获取系统当前时间的函数?

方法一,#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 );

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

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

编辑推荐

热门文章