c语言 时间函数

我在自学C语言 最近在时间函数上卡住了 怎么搞都搞不懂
有哪位大大能给我讲解一下C当中的时间函数
CLOCK()
CLOCKS_PER_SEC()
time()
这几个函数的功能与用法
最好能详细一点 容易点
在此献上本人所有的分数 虽然只有15分 谢谢

  c语言时间函数:
  1、获得日历时间函数:
  可以通过time()函数来获得日历时间(Calendar Time),其原型为:time_t time(time_t * timer);
  如果已经声明了参数timer,可以从参数timer返回现在的日历时间,同时也可以通过返回值返回现在的日历时间,即从一个时间点(例如:1970年1月1日0时0分0秒)到现在此时的秒数。如果参数为空(NUL),函数将只通过返回值返回现在的日历时间,比如下面这个例子用来显示当前的日历时间:
  2、获得日期和时间函数:
  这里说的日期和时间就是平时所说的年、月、日、时、分、秒等信息。从第2节我们已经知道这些信息都保存在一个名为tm的结构体中,那么如何将一个日历时间保存为一个tm结构的对象呢?
  其中可以使用的函数是gmtime()和localtime(),这两个函数的原型为:
  struct tm * gmtime(const time_t *timer);
  struct tm * localtime(const time_t * timer);
  其中gmtime()函数是将日历时间转化为世界标准时间(即格林尼治时间),并返回一个tm结构体来保存这个时间,而localtime()函数是将日历时间转化为本地时间。比如现在用gmtime()函数获得的世界标准时间是2005年7月30日7点18分20秒,那么用localtime()函数在中国地区获得的本地时间会比世界标准时间晚8个小时,即2005年7月30日15点18分20秒。
温馨提示:答案为网友推荐,仅供参考
第1个回答  2012-04-26
废话少说!对于CLOCKS_PER_SEC()简单地理解就是用来计算程序本身的执行时间
以下程序可以验证如:
#include
<stdio.h>
#include
<time.h>
#include
<windows.h>
void
main()
{
Sleep(1000);//让程序休眠一秒钟
printf("Elapsed
time:
%u
secs.\n",
clock()/CLOCKS_PER_SEC);
}
//很显然从本程序来说主函数体里用到了Sleep函数并且让它休眠了一秒钟,所以这个程序执行的时间就是1秒
第2个回答  2008-04-04
C语言的建时间函数是
mktime(),原型在

调用有点繁。
下面,用我的程序输入
年月日时分秒,调用mktime(),
就得
C语言
可直接使用的
时间,
存放在
t
里。
例如
输入年月日时分秒:
2008
8
16
9
55
25
time_t
t;

就有了
各种时间信息,例如星期几...
#include
#include
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));
}
第3个回答  2012-03-18
这个试看看应该可以的
#include
<stdio.h>;
#include
<time.h>;
time_t
scanf_time(char
*
timestr)
{
struct
tm
t;
if(!timestr)
return
0;
memset(&t,
0,
sizeof(t));
sscanf(timestr,
"%02d%02d%02d
%02d:%02d:%02d",
&(t.tm_mday),
&(t.tm_mon),
&(t.tm_year),
&(t.tm_hour),
&(t.tm_min),
&(t.tm_sec));
t.tm_year
+=
100;
t.tm_mon
-=1;
return
mktime(&t);
}
int
main(int
argc,
char
*argv[])
{
time_t
x
=
0;
time_t
y
=
0;
x
=
scanf_time("160312
00:00:00");
y
=
time(NULL);
if(x>y){
//0=1970-00-01
08:00:00
struct
tm
t;
memcpy(&t,
localtime(&x),
sizeof(struct
tm));
printf("\n%d年-%d月-%d日\t%d时:%d分:%d秒\n",
t.tm_year
+
1900,
t.tm_mon+1,
t.tm_mday,
t.tm_hour,
t.tm_min,
t.tm_sec);
memcpy(&t,
localtime(&y),
sizeof(struct
tm));
printf("\n%d年-%d月-%d日\t%d时:%d分:%d秒\n",
t.tm_year
+
1900,
t.tm_mon+1,
t.tm_mday,
t.tm_hour,
t.tm_min,
t.tm_sec);
time_t
result
=
x
-
y;
memcpy(&t,
localtime(&result),
sizeof(struct
tm));
printf("\n%d年-%d月-%d日\t%d时:%d分:%d秒\n",
t.tm_year
-70,
t.tm_mon,
t.tm_mday-1,
t.tm_hour-8,
t.tm_min,
t.tm_sec);
}
system("PAUSE");
return
0;
}
第4个回答  2013-12-24
早就不用TC了,不清楚。
不知道你想要干什么,如果只是想简单地用字符串格式来输出现在的时间,用下面的代码就可以了。如果要自定格式,才需要用到struct
tm结构,不过不是你写的样子。
#include
<time.h>
#include
<stdio.h>
int
main()
{
time_t
lt;
lt
=
time(NULL);
printf("The
Calendar
Time
now
is
%s\n",
ctime(&
lt));
return
0;
}
相似回答