资讯

精准传达 • 有效沟通

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

gettime函数c语言,c语言datetime

求c语言大神指点 gettime函数的使用 ,用来编辑个时钟

你的代码没有错,在DOS的C下没有问题。但DOS的有些时间函数在C++编译器下已经无效了。可尝试用下面的——

创新互联专业为企业提供临海网站建设、临海做网站、临海网站设计、临海网站制作等企业网站建设、网页设计与制作、临海企业网站模板建站服务,十年临海做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。

#include "stdio.h"

#include "time.h"

#include DOS.H

void main(void){

struct tm *pt;

time_t t;

t=time(NULL);

pt=localtime(t);

printf("The current time is: %2d:%02d:%02d\n",

pt-tm_hour, pt-tm_min, pt-tm_sec);

}

但tm结构没有ms级变量。

C语言中的gettime()是在哪个头文件里?

#includeconin.h

#include dos.h

或使用

#include time.h

time_t t;

time(t);

puts(ctime(t));

C语言里time结构体和gettime()函数包含在哪个头文件里?

二者均定义在time.h中。

1 在C语言中,为了操作简单,减少引入头文件的数量,相关功能的类型及函数均会定义在同一头文件中,比如输入输出相关的均定义在stdio.h中,而时间相关的均定义在time.h中。

2 time结构体,即struct time, 是用来存储时间的结构体。

3 gettime函数,为获取时间函数,其参数为struct time *类型。

另外,在不确定是存储在哪个头文件,即编程时不确定要引用哪个头文件时,可以在系统头文件文件夹中,进行全文搜索,从而得知要需要的头文件,及对应的使用方式。

请问在C语言里怎么获取当前时间和日期(精确到毫秒)?

先申明下,这个是我转百度知道的,经常BAIDU一下,就OK了

#include stdio.h

#include time.h

void main ()

{

time_t rawtime;

struct tm * timeinfo;

time ( rawtime );

timeinfo = localtime ( rawtime );

printf ( "\007The current date/time is: %s", asctime (timeinfo) );

exit(0);

}

=================

#include time.h -- 必须的时间函数头文件

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时间格式:

星期 月 日 时:分:秒 年

=========================================

你要的格式可这样输出:

printf ( "%4d-%02d-%02d %02d:%02d:%02d\n",1900+timeinfo-tm_year, 1+timeinfo-tm_mon,

timeinfo-tm_mday,timeinfo-tm_hour,timeinfo-tm_min,timeinfo-tm_sec);

就是直接打印tm,tm_year 从1900年计算,所以要加1900,

月tm_mon,从0计算,所以要加1

其它你一目了然啦。

如何用C语言获取当前系统时间?

需要利用C语言的时间函数time和localtime,具体说明如下:

一、函数接口介绍:

1、time函数。

形式为time_t time (time_t *__timer);

其中time_t为time.h定义的结构体,一般为长整型。

这个函数会获取当前时间,并返回。 如果参数__timer非空,会存储相同值到__timer指向的内存中。

time函数返回的为unix时间戳,即从1970年1月1日(UTC/GMT的午夜)开始所经过的秒数,不考虑闰秒。

由于是秒作为单位的,所以这并不是习惯上的时间,要转为习惯上的年月日时间形式就需要另外一个函数了。

2、localtime函数。

形式为struct tm *localtime (const time_t *__timer);

其中tm为一个结构体,包含了年月日时分秒等信息。

这种结构是适合用来输出的。

二、参考代码:

#include stdio.h

#include time.h

int main ()

{

time_t t;

struct tm * lt;

time (t);//获取Unix时间戳。

lt = localtime (t);//转为时间结构。

printf ( "%d/%d/%d %d:%d:%d\n",lt-tm_year+1900, lt-tm_mon, lt-tm_mday, lt-tm_hour, lt-tm_min, lt-tm_sec);//输出结果

return 0;

}

注意事项:

struct tm中的tm_year 值为实际年减去1900, 所以输出的时候要是lt-tm_year+1900。

c语言如何取得系统时间??

#include "time.h"

time_t time(time_t *timer);

调用后将当前系统时间与1900年1月1日相差的秒数存入到timer中,timer可看成是一个长整型数

struct tm* localtime(const time_t *timer)

将time()函数调用的结果做为参数传入到localtime()函数中就能得到当前时间和日期,注意得到的年是和1970的差值,月份是和1月的差值

struct tm是一个结构体,定义如下

struct tm

{

int tm_sec; //当前秒

int tm_min; //当前分钟

int tm_hour; //当前小时

int tm_mday; //当前在本月中的天,如11月1日,则为1

int tm_mon; //当前月,范围是0~11

int tm_year; //当前年和1900的差值,如2006年则为36

int tm_wday; //当前在本星期中的天,范围0~6

int tm_yday; //当前在本年中的天,范围0~365

int tm_isdst; //这个我也不清楚

}

求当前时间的示例

int getSystemTime()

{

time_t timer;

struct tm* t_tm;

time(timer);

t_tm = localtime(timer);

printf("today is %4d%02d%02d%02d%02d%02d\n", t_tm.tm_year+1900,

t_tm.tm_mon+1, t_tm.tm_mday, t_tm.tm_hour, t_tm.tm_min, t_tm.tm_sec);

return 0;

}

其他时间的函数和结构还有:

timeval结构

#include include/linux/time.h

struct timeval

{

time_t tv_sec;

susecond_t tv_usec; //当前妙内的微妙数

};

tms结构

保存着一个进程及其子进程使用的cpu时间

struct tms

{

clock_t tms_utime;

clock_t tms_stime;

clock_t tms_cutime;

clock_t tms_cstime;

}

timer_struct结构

#include include/linux/timer.h

struct timer_struct

{

unsigned long expires; //定时器被激活的时刻

void (*fn)(void); //定时器激活后的处理函数

}

utime函数

更改文件的存取和修改时间

int utime(const char pathname, const struct utimbuf *times) // return value 0 or -1

times 为空指针,存取和修改时间设置为当前时间

struct utimbuf

{

time_t actime;

time_t modtime;

}


文章名称:gettime函数c语言,c语言datetime
浏览路径:http://cdkjz.cn/article/hdpgjo.html
多年建站经验

多一份参考,总有益处

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

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

大客户专线   成都:13518219792   座机:028-86922220