在C++中,time(0),time(1)有什么区别?

在C++中,time(0),time(1)有什么区别?,第1张

在c++中,time(0)指函数返回当前时间,如果发生错误返回0。time(1)指函数返回当前时间,如果发生错误返回1

time(0)或者time(1)指c++中的一种函数。其作用是返回一特定时间的小数值

time(0)指函数返回当前时间,如果发生错误返回零。

time(1)指函数返回当前时间,如果发生错误返回一。

扩展资料

TIME 函数语法具有以下参数:

1Hour 必需。0(零)到 32767 之间的数值,代表小时。任何大于 23 的数值将除以 24,其余数将视为小时。例如,TIME(27,0,0) = TIME(3,0,0) = 125 或 3:00 AM。

2Minute 必需。0 到 32767 之间的数值,代表分钟。任何大于 59 的数值将被转换为小时和分钟。例如,TIME(0,750,0) = TIME(12,30,0) = 520833 或 12:30 PM。

3Second 必需。0 到 32767 之间的数值,代表秒。任何大于 59 的数值将被转换为小时、分钟和秒。例如,TIME(0,0,2000) = TIME(0,33,22) = 023148 或 12:33:20 AM。

4注解:Microsoft Excel for Windows 和 Microsoft Excel for the Macintosh 使用不同的默认日期系统。时间值为日期值的一部分,并用小数来表示(例如 12:00 PM 可表示为 05,因为此时是一天的一半)。

参考资料:

:time函数

1、C语言中读取系统时间的函数为time(),其函数原型为:

#include <timeh>

time_t time( time_t ) ;

time_t就是long,函数返回从1970年1月1日(MFC是1899年12月31日)0时0分0秒,到现在的的秒数。

2、C语言还提供了将秒数转换成相应的时间格式的函数:

char ctime(const time_t timer); //将日历时间转换成本地时间,返回转换后的字符串指针 可定义字符串或是字符指针来接收返回值

struct tm gmtime(const time_t timer); //将日历时间转化为世界标准时间(即格林尼治时间),返回结构体指针 可定义struct tm 变量来接收结果

struct tm localtime(const time_t timer); //将日历时间转化为本地时间,返回结构体指针 可定义struct tm 变量来接收结果

3、例程:

#include <timeh>

void main()

{

time_t t;

struct tm pt ;

char pc ;

time(&t);

pc=ctime(&t) ; printf("ctime:%s", pc );

pt=localtime(&t) ; printf("year=%d", pt->tm_year+1900 );

}

//时间结构体struct tm 说明:

struct tm {

int tm_sec; / 秒 – 取值区间为[0,59] /

int tm_min; / 分 - 取值区间为[0,59] /

int tm_hour; / 时 - 取值区间为[0,23] /

int tm_mday; / 一个月中的日期 - 取值区间为[1,31] /

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

int tm_year; / 年份,其值等于实际年份减去1900 /

int tm_wday; / 星期 – 取值区间为[0,6],其中0代表星期天,1代表星期一,以此类推 /

int tm_yday; / 从每年的1月1日开始的天数 – 取值区间为[0,365],其中0代表1月1日,1代表1月2日,以此类推 /

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

};

标准库的timeh里有时间函数

time_t time (time_t timer)

计算从1970年1月1日到当前系统时间,并把结果返回给timer变量,

函数本身返回的也是这个结果time_t这个类型其实就是一个int

另有:

double difftime ( time_t timer2, time_t timer1 )

把返回time2和time1所储存的时间的差

以前实际上用过,很想对C语言中的时间函数了解多一点,趁着这个寒假,查了些资料,大概把我现在能用到的关于时间的 *** 作在此记录下来。通过几个函数来熟悉C语言中对时间的 *** 作。(注:以下程序均在VS2010上编译通过。)①time()函数。可以通过time()函数来获得日历时间。其原型为: time_t time(time_t timer);一般参数为空,返回值类型time_t是一个长整型数,函数将返回现在的日历时间,即从一个时间点(所有不同版本的Visual C++都是从1970年1月1日0时0分0秒)到现在的经过的秒数。例子程序:#include<stdioh>#include<timeh>void main(){ time_t lt; lt=time(NULL); printf("1970年1月1日0时0分0秒到现在经历了%ld秒%A",lt);}运行结果(结果与程序运行的时间有关,贴出我此时运行出的结果):1970年1月1日0时0分0秒到现在经历了1326975564秒请按任意键继续 ②clock()函数。C语言中的计时函数。函数原型为: clock_t clock(void);clock()函数返回从“开启这个程序进程\”到“程序中调用clock()函数”时之间的CPU时钟计时单元数。返回值类型clock_t也是一个长整型数。在timeh头文件中定义了一个符号常量CLOCKS_PER_SEC,表示一秒钟会有多少个计时单元。所以通过简单的数学知识,可以知道在程序中用clock()/CLOCKS_PER_SEC来表示程序从开始到调用clock()函数时用了多少秒。例子程序:#include<stdioh>#include<timeh>void main(){ clock_t lt; for(int i=0;i<1000000000;i++); lt=clock(); printf("循环执行1000000000个空 *** 作需要%f秒%A",(double)lt/CLOCKS_PER_SEC);}运行结果(在不同的机器上运行的结果可能不一样,下面是在我自己的笔记本上运行的结果):循环执行1000000000个空 *** 作需要3484000秒请按任意键继续 ③使用C库函数来显示日期和时间。首先要介绍一下C语言中的一个日期的结构体类型,tm类型。其在timeh中的定义如下:#ifndef _TM_DEFINEDstruct 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; };#define _TM_DEFINED#endif然后可以介绍有关的函数了。timeh提供了两种不同的函数将日历时间(一个长整型数)转换成我们平时看到的把年月日时分秒分开的时间格式: struct tm gmtime(const time_t timer); struct tm localtime(const time_t timer);其中gmtime()函数是将日历时间转换为世界标准时间(即格林尼治时间),并返回一个tm结构体来保存这个时间,而localtime()函数是将日历时间转换为本地时间(在中国地区获得的本地时间会比世界标准时间晚8个小时)。例子程序:#include<stdioh>#include<timeh>void main(){ struct tm local; time_t t; t=time(NULL); local=localtime(&t); printf("现在北京时间是%d点%A",local->tm_hour); local=gmtime(&t); printf("世界标准时间是%d点%A",local->tm_hour);}运行结果(运行结果与运行的时间有关,我是在早上9点多钟运行这个程序的):现在北京时间是9点世界标准时间是1点请按任意键继续 这样子我们就可以完全才输出此刻的年月日时分秒了,当然需要逐个来输出。其实C库函数还提供了一个很有用的以固定的时间格式来输出年月日时分秒的函数。这两个函数原型如下: char asctime(const struct tm timeptr); char ctime(const time_t timer);asctime()函数是通过tm结构来生成具有固定格式的保存时间信息的字符串,而ctime()是通过日历时间来生成时间字符串。这样下面的例子程序就容易理解了:#include<stdioh>#include<timeh>void main(){ struct tm local; time_t t; t=time(NULL); local=localtime(&t); printf(asctime(local)); local=gmtime(&t); printf(asctime(local)); printf(ctime(&t));}运行结果(我是在早上9点多运行这个程序的):Fri Jan 20 09:55:56 2012Fri Jan 20 01:55:56 2012Fri Jan 20 09:55:56 2012请按任意键继续 C语言还可以以我们规定的各种形式来规定输出时间的格式。要用到时可以查阅相关的资料,限于篇幅,就介绍到这里。④这里介绍计算持续的时间长度的函数。之前已经介绍了使用clock()函数的例子,它可以精确到毫秒级。其实我们也可以使用difftime()函数,但它只精确到秒。该函数的定义如下: double difftime(time_t time1,time_t time0);例子程序:#include<stdioh>#include<timeh>#include<stdlibh>void main(){ time_t start,end; start=time(NULL); for(int i=0;i<1000000000;i++); end=time(NULL); printf("这个循-环用了%f秒%A",difftime(end,start));}运行结果:这个循环用了3000000秒请按任意键继续 ⑤最后介绍mktime()函数。原型如下: time_t mktime(struct tm timer);可以使用函数将用tm结构表示的时间转换为日历时间。其返回值就是转换后的日历时间。这样我们就可以先制定一个分解时间,然后对这个时间进行 *** 作。下面的例子用来计算2012年1月20日是星期几:#include<timeh>#include<stdioh>#include<stdlibh>int main(void){ struct tm t; time_t t_of_day; ttm_year=2012-1900; ttm_mon=0; ttm_mday=20; ttm_hour=0; ttm_min=12; ttm_sec=1; ttm_isdst=1; t_of_day=mktime(&t); printf(ctime(&t_of_day)); return 0;}运行结果:Fri Jan 20 00:12:01 2012请按任意键继续

  Timer()函数

  语法:Timer ( interval {, windowname } )

  参数:指定两次触发Timer事件之间的时间间隔,有效值在0到65之间。如果该参数的值指定为0,那么关闭定时器,不再触发指定窗口的Timer事件。windowname:窗口名,指定时间间隔到时要触发哪个窗口的Timer事件。省略该参数时,触发当前窗口的Timer事件返回值Integer。函数执行成功时返回1,发生错误时返回-1。如果任何参数的值为NULL,Timer()函数返回NULL。用法使用Timer()函数可以周期性地触发指定窗口的Timer事件,这样,每当时间间隔过去时,应用程序都可以完成一些周期性的工作,比如绘制简单动画等。将Timer()的interval参数设置为非0值时启动定时器并开始计时;将该函数的interval参数设置为0时关闭定时器,终止计时任务。需要注意的是,在Microsoft Windows系统中,该函数能够计时的最小时间间隔为0055秒(约1/18秒),如果把interval参数的值设置小于0055,那么该定时器将每隔0055秒触发一次窗口的Timer事件。Microsoft Windows 3x最多只支持系统中同时启动16个定时器。

  用法:

  启动定时器。

  启动定时器就需要使用CWnd类的成员函数SetTimer。CWnd::SetTimer的原型如下:

  UINT_PTR SetTimer(

  UINT_PTR nIDEvent,

  UINT nElapse,

  void (CALLBACK lpfnTimer)(

  HWND,

  UINT,

  UINT_PTR,

  DWORD

  )

  );

  

  参数nIDEvent指定一个非零的定时器ID;参数nElapse指定间隔时间,单位为毫秒;参数lpfnTimer指定一个回调函数的地址,如果该参数为NULL,则WM_TIMER消息被发送到应用程序的消息队列,并被CWnd对象处理。如果此函数成功则返回一个新的定时器的ID,我们可以使用此ID通过KillTimer成员函数来销毁该定时器,如果函数失败则返回0。

  通过SetTimer成员函数我们可以看出,处理定时事件可以有两种方式,一种是通过WM_TIMER消息的消息响应函数,一种是通过回调函数。

  如果要启动多个定时器就多次调用SetTimer成员函数。另外,在不同的CWnd中可以有ID相同的定时器,并不冲突。

  time函数

  返回某一特定时间的小数值。如果在输入函数前,单元格的格式为“常规”,则结果将设为日期格式。

  函数 TIME 返回的小数值为 0(零)到 099999999 之间的数值,代表从 0:00:00 (12:00:00 AM) 到 23:59:59 (11:59:59 PM) 之间的时间。

  语法:

  TIME(hour, minute, second)

  TIME 函数语法具有以下参数:

  Hour 必需。0(零)到 32767 之间的数值,代表小时。任何大于 23 的数值将除以 24,其余数将视为小时。例如,TIME(27,0,0) = TIME(3,0,0) = 125 或 3:00 AM。

  Minute 必需。0 到 32767 之间的数值,代表分钟。任何大于 59 的数值将被转换为小时和分钟。例如,TIME(0,750,0) = TIME(12,30,0) = 520833 或 12:30 PM。

  Second 必需。0 到 32767 之间的数值,代表秒。任何大于 59 的数值将被转换为小时、分钟和秒。例如,TIME(0,0,2000) = TIME(0,33,22) = 023148 或 12:33:20 AM。

  例子:

  <php$t=time();echo($t "<br />");echo(date("D F d Y",$t));>

  输出:

  1138618081Mon January 30 2006

time函数返回以格林尼治时间(GMT)为标准,从1970年1月1日00:00:00到现在的此时此刻所经过的秒数。

若time_t参数没有被忽略,则经过的秒数也会被填入该指针所指向的区域内。

数据类型time_t其实就是一个long类型,在timeh中这样定义

typedef

long

time_t;

#include<stdioh>

#include<timeh>

#include<stdlibh>

void

main()

{

time_t

start,end;

start

=

time(NULL);

system("pause");

end

=

time(NULL);

printf("The

pause

used

%f

seconds\n",difftime(end,start));//<-

}

这段代码就

是求时间差的,其中用这个函数difftime();

希望对你有所帮助

返回某一特定时间的小数值。如果在输入函数前,单元格的格式为“常规”,则结果将设为日期格式。函数 TIME 返回的小数值为 0(零)到 099999999 之间的数值,代表从 0:00:00 (12:00:00 AM) 到 23:59:59 (11:59:59 PM) 之间的时间。

Gets the system time

time_t time( time_t timer );

Routine Required Header Compatibility

time <timeh> ANSI, Win 95, Win NT

是定义在timeh中

The time function returns the number of seconds elapsed since midnight (00:00:00), January 1, 1970, coordinated universal time, according to the system clock The return value is stored in the location given by timer This parameter may be NULL, in which case the return value is not stored

Example

/ TIMESC illustrates various time and date functions including:

time _ftime ctime asctime

localtime gmtime mktime _tzset

_strtime _strdate strftime

Also the global variable:

_tzname

/

#include <timeh>

#include <stdioh>

#include <sys/typesh>

#include <sys/timebh>

#include <stringh>

void main()

{

char tmpbuf[128], ampm[] = "AM";

time_t ltime;

struct _timeb tstruct;

struct tm today, gmt, xmas = ;

/ Set time zone from TZ environment variable If TZ is not set,

the operating system is queried to obtain the default value

for the variable

/

_tzset();

/ Display operating system-style date and time /

_strtime( tmpbuf );

printf( "OS time:\t\t\t\t%s\n", tmpbuf );

_strdate( tmpbuf );

printf( "OS date:\t\t\t\t%s\n", tmpbuf );

/ Get UNIX-style time and display as number and string /

time( &ltime);

printf( "Time in seconds since UTC 1/1/70:\t%ld\n", ltime );

printf( "UNIX time and date:\t\t\t%s", ctime( <ime ) );

/ Display UTC /

gmt = gmtime(&ltime);

printf( "Coordinated universal time:\t\t%s", asctime( gmt ) );

/ Convert to time structure and adjust for PM if necessary /

today = localtime( &ltime );

if( today->tm_hour > 12 )

{

strcpy( ampm, "PM" );

today->tm_hour -= 12;

}

if( today->tm_hour == 0 ) / Adjust if midnight hour /

today->tm_hour = 12;

/ Note how pointer addition is used to skip the first 11

characters and printf is used to trim off terminating

characters

/

printf( "12-hour time:\t\t\t\t%8s %s\n",

asctime( today ) + 11, ampm );

/ Print additional time information /

_ftime( &tstruct );

printf( "Plus milliseconds:\t\t\t%u\n", tstructmillitm );

printf( "Zone difference in seconds from UTC:\t%u\n",

tstructtimezone );

printf( "Time zone name:\t\t\t\t%s\n", _tzname[0] );

printf( "Daylight savings:\t\t\t%s\n",

tstructdstflag "YES" : "NO" );

/ Make time for noon on Christmas, 1993 /

if( mktime( &xmas ) != (time_t)-1 )

printf( "Christmas\t\t\t\t%s\n", asctime( &xmas ) );

/ Use time structure to build a customized time string /

today = localtime( <ime );

/ Use strftime to build a customized time string /

strftime( tmpbuf, 128,

"Today is %A, day %d of %B in the year %Y\n", today );

printf( tmpbuf );

}

另外,团IDC网上有许多产品团购,便宜有口碑

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/11676525.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-17
下一篇 2023-05-17

发表评论

登录后才能评论

评论列表(0条)

保存