当前位置:首页 » 编程语言 » c语言时间函数

c语言时间函数

发布时间: 2022-01-14 03:46:37

1. c语言函数的时间日期函数

函数库为time.h、dos.h
在时间日期函数里,主要用到的结构有以下几个:
总时间日期贮存结构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-11*/ │
│ int tm_year; /*自1900的年数*/ │
│ int tm_wday; /*自星期日的天数0-6*/ │
│ int tm_yday; /*自1月1日起的天数,0-365*/ │
│ int tm_isdst; /*是否采用夏时制,采用为正数*/│
│} │
└──────────────────────┘
日期贮存结构date
┌───────────────┐
│struct date │
│{ │
│ int da_year; /*自1900的年数*/│
│ char da_day; /*天数*/ │
│ char da_mon; /*月数 1=Jan*/ │
│} │
└───────────────┘
时间贮存结构time
┌────────────────┐
│struct time │
│{ │
│ unsigned char ti_min; /*分钟*/│
│ unsigned char ti_hour; /*小时*/│
│ unsigned char ti_hund; │
│ unsigned char ti_sec; /*秒*/ │
│ │
└────────────────┘char *ctime(long *clock)
本函数把clock所指的时间(如由函数time返回的时间)转换成下列格式的
字符串:Mon Nov 21 11:31:54 1983
char *asctime(struct tm *tm)
本函数把指定的tm结构类的时间转换成下列格式的字符串:
Mon Nov 21 11:31:54 1983
double difftime(time_t time2,time_t time1)
计算结构time2和time1之间的时间差距(以秒为单位)
struct tm *gmtime(long *clock)本函数把clock所指的时间(如由函数time返回的时间)
转换成格林威治时间,并以tm结构形式返回
struct tm *localtime(long *clock)本函数把clock所指的时间(如函数time返回的时间)
转换成当地标准时间,并以tm结构形式返回
void tzset()本函数提供了对UNIX操作系统的兼容性
long dostounix(struct date *dateptr,struct time *timeptr)
本函数将dateptr所指的日期,timeptr所指的时间转换成UNIX格式,并返回
自格林威治时间1970年1月1日凌晨起到现在的秒数
void unixtodos(long utime,struct date *dateptr,struct time *timeptr)
本函数将自格林威治时间1970年1月1日凌晨起到现在的秒数utime转换成
DOS格式并保存于用户所指的结构dateptr和timeptr中
void getdate(struct date *dateblk)本函数将计算机内的日期写入结构dateblk
中以供用户使用
void setdate(struct date *dateblk)本函数将计算机内的日期改成
由结构dateblk所指定的日期
void gettime(struct time *timep)本函数将计算机内的时间写入结构timep中,
以供用户使用
void settime(struct time *timep)本函数将计算机内的时间改为
由结构timep所指的时间
long time(long *tloc)本函数给出自格林威治时间1970年1月1日凌晨至现在所经
过的秒数,并将该值存于tloc所指的单元中.
int stime(long *tp)本函数将tp所指的时间(例如由time所返回的时间)
写入计算机中.

2. 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>
intmain()
{
time_trawtime;
structtm*timeinfo;
time(&rawtime);
timeinfo=localtime(&rawtime);
printf("Thecurrentdate/timeis:%s",asctime(timeinfo));

return0;
}

3. C语言计算时间函数

标准库的time.h里有时间函数

time_t time (time_t *timer)
计算从1970年1月1日到当前系统时间,并把结果返回给timer变量,
函数本身返回的也是这个结果.time_t这个类型其实就是一个int.

另有:
double difftime ( time_t timer2, time_t timer1 )
把返回time2和time1所储存的时间的差.

4. C语言调用系统时间函数是什么啊

C++可以,C貌似不行.
C++里边有一个CTime
的class
自己在msdn里搜索CTime就有了.
或者你CTime
time;
\\回车
然后输入"time."
VC会给里列出所有的function
不过C
倒是可以include
<time.h>
里边有一些和时间有关的function,
有没有调用系统时间的我就忘记了.毕竟不常用.你可以自己查查MSDN,搜索time.h试试看.

5. c语言调用时间函数

time_t t; /*定义一个time_t型(在time.h中有typedef long time_t;语句,由此可知,time_t类型也就是long类型)的变量*/

time(&t); /*将当前的日历时间(即从1970-1-1到执行此语句时所经历的秒数)保存到t中*/

printf("%s/n", ctime(&t)); /*ctime(&t)将把t所指向的日历时间转换为系统所提供的一个字符串,这个函数将返回这个字符串的基址,然后由printf语句将这个字符串输出,从而得到现在的时刻*/
来源http://..com/question/32925135.html?si=1

6. (高分)c语言关于时间的函数

time_t sec = time(NULL);
struct tm t = *localtime(&sec);
//第一次
printf("%.4d-%.2d-%.2d %.2d:%.2d:%.2d\n",info[a].year_in,info[a].mon_in,info[a].mday_in,info[a].hour_in,info[a].min_in,info[a].sec_in);
//第二次
sec = time(NULL);
t = *localtime(&sec);
printf("%.4d-%.2d-%.2d %.2d:%.2d:%.2d\n",info[a].year_in,info[a].mon_in,info[a].mday_in,info[a].hour_in,info[a].min_in,info[a].sec_in);

7. C语言计算时间

在C语言中计算时间,可以使用标准库中的计时函数——clock()。

函数原型:

clock_tclock(void);

其中clock_t是用来保存时间的数据类型,在time.h文件中,可以找到对它的定义:

#ifndef_CLOCK_T_DEFINED
typedeflongclock_t;
#define_CLOCK_T_DEFINED
#endif


很明显,clock_t是一个长整形数。在time.h文件中,还定义了一个常量CLOCKS_PER_SEC,它用来表示一秒钟会有多少个时钟计时单元,其定义如下:

#defineCLOCKS_PER_SEC((clock_t)1000)

可以看到每过千分之一秒(1毫秒),调用clock()函数返回的值就加1。下面举个例子,可以使用公式clock()/CLOCKS_PER_SEC来计算一个进程自身的运行时间:

voidelapsed_time()
{
printf("Elapsedtime:%usecs. ",clock()/CLOCKS_PER_SEC);
}

当然,也可以用clock函数来计算的机器运行一个循环或者处理其它事件到底花了多少时间:

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
intmain(void)
{
longi=10000000L;
clock_tstart,finish;
doubleration;
printf("Timetodo%ldemptyloopsis",i);
start=clock();
while(i--);
finish=clock();
ration=(double)(finish-start)/CLOCKS_PER_SEC;
printf("%fseconds ",ration);
system("pause");
}

8. c语言中time函数的用法

头文件time.h

@函数名称: localtime
函数原型: struct tm *localtime(const time_t *timer)
函数功能: 返回一个以tm结构表达的机器时间信息
函数返回: 以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;
};
参数说明: timer-使用time()函数获得的机器时间

#include <time.h>
#include <stdio.h>
#include <dos.h>
int main()
{
time_t timer;
struct tm *tblock;
timer=time(NULL);
tblock=localtime(&timer);
printf("Local time is: %s",asctime(tblock));
return 0;
}

@函数名称: asctime
函数原型: char* asctime(struct tm * ptr)
函数功能: 得到机器时间(日期时间转换为ASCII码)
函数返回: 返回的时间字符串格式为:星期,月,日,小时:分:秒,年
参数说明: 结构指针ptr应通过函数localtime()和gmtime()得到
所属文件: <time.h>

#include <stdio.h>
#include <string.h>
#include <time.h>
int main()
{
struct tm t;
char str[80];
t.tm_sec=1;
t.tm_min=3;
t.tm_hour=7;
t.tm_mday=22;
t.tm_mon=11;
t.tm_year=56;
t.tm_wday=4;
t.tm_yday=0;
t.tm_isdst=0;
strcpy(str,asctime(&t));
printf("%s",str);
return 0;
}

@函数名称: ctime
函数原型: char *ctime(long time)
函数功能: 得到日历时间
函数返回: 返回字符串格式:星期,月,日,小时:分:秒,年
参数说明: time-该参数应由函数time获得
所属文件: <time.h>

#include <stdio.h>
#include <time.h>
int main()
{
time_t t;
time(&t);
printf("Today's date and time: %s",ctime(&t));
return 0;
}

@函数名称: difftime
函数原型: double difftime(time_t time2, time_t time1)
函数功能: 得到两次机器时间差,单位为秒
函数返回: 时间差,单位为秒
参数说明: time1-机器时间一,time2-机器时间二.该参数应使用time函数获得
所属文件: <time.h>

#include <time.h>
#include <stdio.h>
#include <dos.h>
#include <conio.h>
int main()
{
time_t first, second;
clrscr();
first=time(NULL);
delay(2000);
second=time(NULL);
printf("The difference is: %f seconds",difftime(second,first));
getch();
return 0;
}

@函数名称: gmtime
函数原型: struct tm *gmtime(time_t *time)
函数功能: 得到以结构tm表示的时间信息
函数返回: 以结构tm表示的时间信息指针
参数说明: time-用函数time()得到的时间信息
所属文件: <time.h>

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <dos.h>
char *tzstr="TZ=PST8PDT";
int main()
{
time_t t;
struct tm *gmt, *area;
putenv(tzstr);
tzset();
t=time(NULL);
area=localtime(&t);
printf("Local time is:%s", asctime(area));
gmt=gmtime(&t);
printf("GMT is:%s", asctime(gmt));
return 0;
}

@函数名称: time
函数原型: time_t time(time_t *timer)
函数功能: 得到机器的日历时间或者设置日历时间
函数返回: 机器日历时间
参数说明: timer=NULL时得到机器日历时间,timer=时间数值时,用于设置日历时间,time_t是一个long类型
所属文件: <time.h>

#include <time.h>
#include <stdio.h>
#include <dos.h>
int main()
{
time_t t;
t=time();
printf("The number of seconds since January 1,1970 is %ld",t);
return 0;
}

@函数名称: tzset
函数原型: void tzset(void)
函数功能: UNIX兼容函数,用于得到时区,在DOS环境下无用途
函数返回:
参数说明:
所属文件: <time.h>

#include <time.h>
#include <stdlib.h>
#include <stdio.h>
int main()
{
time_t td;
putenv("TZ=PST8PDT");
tzset();
time(&td);
printf("Current time=%s",asctime(localtime(&td)));
return 0;
}

9. C语言里面的时间函数怎么用

函数名: difftime
功 能: 计算两个时刻之间的时间差
用 法: double difftime(time_t time2, time_t time1);
程序例:

include <time.h>

10. C语言的时间函数

程序似乎有问题,无效数据 输入什么都是无效的

热点内容
linux虚拟地址物理地址 发布:2024-09-20 06:23:29 浏览:563
大华监控云存储 发布:2024-09-20 06:13:24 浏览:596
SQL写序列 发布:2024-09-20 06:02:29 浏览:964
装缓存下载 发布:2024-09-20 05:42:36 浏览:72
gon引擎自动回收脚本 发布:2024-09-20 05:39:39 浏览:247
好医生连锁店密码多少 发布:2024-09-20 05:09:38 浏览:15
魔兽脚本代理 发布:2024-09-20 05:09:35 浏览:99
python登陆网页 发布:2024-09-20 05:08:39 浏览:758
安卓qq飞车如何转苹果 发布:2024-09-20 04:54:30 浏览:178
存储过程中in什么意思 发布:2024-09-20 04:24:20 浏览:315