资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

c语言时间函数的写法 C语言时间类型

c语言时间函数!!

time_t nowtime; -- 声明变量 nowtime(现在时间) 为 time_t 型

我们提供的服务有:做网站、成都网站建设、微信公众号开发、网站优化、网站认证、鄂托克ssl等。为上1000家企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的鄂托克网站制作公司

struct tm *timeinfo; -- 声明变量timeinfo(时间信息)为 tm 型 结构 指针。

time_t , tm 都是 time.h 头文件里定义 的 类型。

time( nowtime ); -- 调系统函数 time(), 获得 现在时间 (1970年起多少个“滴答”,世界标准时间)

timeinfo = localtime( nowtime ); -- 调系统函数, 获得 当地 现在时间 (例如 东8 区,北京时间)。时间数据是 tm 型 结构。

int hour; -- 声明变量 hour (小时),整型。

hour = timeinfo-tm_hour+1 ; -- 结构 timeinfo的成员tm_hour 是时间值,+1 得 hour(小时)。

tm_hour -- 数值范围 0-23。

c语言 时间函数

CLOCK()函数:

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

clock_t

clock(void)

;

这个函数返回从“开启这个程序进程”到“程序中调用clock()函数”时之间的CPU时钟计时单元(clock

tick)数,在MSDN中称之为挂钟时间(wal-clock);若挂钟时间不可取,则返回-1。其中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");

}

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

Time

to

do

10000000

empty

loops

is

0.03000

seconds

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

time_t

time(

time_t

*timer

);

返回值是1970年到现在的秒数

用long型接就可以了

参数也是同样意义

long

time_s

=

0;

time_s

=

time(

NULL

);

//

time_s就是1970年到现在的秒数

或者

long

*

time_s

=

NULL;

time(time_s);

//

*time_s就是1970年到现在的秒数

要计算前后一段时间的话之前取一次time,之后取一次相减就知道用了多少秒了

C语言时间函数time_t

1、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; 

time ( rawtime ); // 获取时间,以秒计,从1970年1月一日起算,存于rawtime 

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

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

//就是直接打印tm,tm_year 从1900年计算,所以要加1900,月tm_mon,从0计算,所以要加1

2、time函数使用示例

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

C语言的建时间函数是 mktime(),原型在 time.h 里

调用有点繁。

下面,用我的程序输入 年月日时分秒,调用mktime(), 就得 C语言 可直接使用的 时间, 存放在 t 里。

例如 输入年月日时分秒: 2008 8 16 9 55 25

time_t t; 里 就有了 各种时间信息,例如星期几...

#include stdio.h

#include time.h

void main(){

struct tm *target_time;

time_t rawtime, t;

int year,month,mday,hh,mm,ss;

time ( rawtime );

target_time = localtime ( rawtime );

printf("Please enter year month day hour minute second\n");

printf("For example: \n");

printf("2008 8 16 9 55 25\n");

scanf("%d %d %d %d %d %d", year, month, mday, hh,mm,ss);

target_time-tm_year = year - 1900;

target_time-tm_mon= month - 1;

target_time-tm_mday = mday ;

target_time-tm_hour = hh ;

target_time-tm_min = mm ;

target_time-tm_sec = ss ;

//

t = mktime (target_time);

// t is ready to use

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

}

如何用C语言编写一个显示时间的函数,要求时间显示精度到毫秒级别。

#include cstdio

#include ctime

using namespace std;

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

void printTime() {

struct tm t;   //tm结构指针

time_t now;  //声明time_t类型变量

time(now);      //获取系统日期和时间

localtime_s(t, now);   //获取当地日期和时间

   //格式化输出本地时间

printf("年-月-日-时-分-秒:%d-%d-%d %d:%d:%d\n", t.tm_year + 1900, t.tm_mon + 1, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec);

}

int main(int argc, char** argv) {

printTime();

}


网站名称:c语言时间函数的写法 C语言时间类型
文章地址:http://cdkjz.cn/article/hghijg.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220