资讯

精准传达 • 有效沟通

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

包含c语言提取系统时间函数的词条

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

这是一个获取时间的,并且写入文件的函数。你琢磨下吧。

创新互联于2013年创立,是专业互联网技术服务公司,拥有项目网站建设、成都网站建设网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元镇海做网站,已为上家服务,为镇海各地企业和个人服务,联系电话:18980820575

void time()

{

FILE *Tp;

Tp=fopen("系统使用记录.txt","a");

time_t t;

//struct tm *gmt, *area;

struct tm *area;

t = time(NULL);

area = localtime(t);

printf("当前系统时间: %s", asctime(area));

fprintf(Tp," %s",asctime(area));

fclose(Tp);

//gmt = gmtime(t);

//printf("GMT is: %s", asctime(gmt));

}

怎么用C语言来获得系统时间

c++使用

头文件

time.h

,c使用ctime,具体使用方法搜一下

函数

sturct

tm

*localtime(const

time_t

*time)来获得本地时间

程序

既输出系统的本地时间

#includetime.h

#includestdio.h

int

main(void)

{

struct

tm

*local;

time_t

t;

t=time(NULL);

local=localtime(t);

printf("local

time

and

date:%s\n",asctime(local));

return

0;

}

几个常用的时间函数

time()//取得系统时间

gmtime()//转换成国标标准之间

localtime()//转换成本地时间

asctime()//转换成字符形式

ctime()//转换成字符形式

strftime()//类似于printf()

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;

}

c语言中取系统时间

主要分为两种方法:

1.这种方法比较高级

#include time.h  

#include stdio.h  

#include time.h

int main(int argc, char ** argv)

{

time_t temp;

struct tm *t;

time(temp);

t = localtime(temp);

printf("当前时间是:\n%d年%d月%d日\n", t-tm_year+1900, t-tm_mon+1 , t-tm_mday);

printf("%d时%d分%d秒\n", t-tm_hour, t-tm_min, t-tm_sec);

/*

t结构体内的成员变量还有以下几个:

tm_wday 星期的第几天 tm_yday 这天是这年的第几天

*/

return 0;

}

需要注意的是tm_year返回的是1900年之后的年数,tm_mon返回的比实际月份小1(至于为什么要这样设计,我不是太清楚)

2.这种方法较为简单方便,但是同时可能会对接下来的其它操作不利。

#include time.h

#include stdio.h  

int main(int argc, char ** argv)

{

time_t temp;

time(temp);

printf("当前时间为:\n%s", ctime(temp));

return 0;

}

用c语言如何获取系统当前时间的函数?

方法一,#includetime.h

int main()

{

time_t timep;

struct tm *p;

time (timep);

p=gmtime(timep);

printf("%d\n",p-tm_sec); /*获取当前秒*/

printf("%d\n",p-tm_min); /*获取当前分*/

printf("%d\n",8+p-tm_hour);/*获取当前时,这里获取西方的时间,刚好相差八个小时*/

printf("%d\n",p-tm_mday);/*获取当前月份日数,范围是1-31*/

printf("%d\n",1+p-tm_mon);/*获取当前月份,范围是0-11,所以要加1*/

printf("%d\n",1900+p-tm_year);/*获取当前年份,从1900开始,所以要加1900*/

printf("%d\n",p-tm_yday); /*从今年1月1日算起至今的天数,范围为0-365*/

}

方法二.#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;}

扩展资料

1、CTimeSpan类

如果想计算两段时间的差值,可以使用CTimeSpan类,具体使用方法如下:

CTime t1( 1999, 3, 19, 22, 15, 0 );

CTime t = CTime::GetCurrentTime();

CTimeSpan span=t-t1; //计算当前系统时间与时间t1的间隔

int iDay=span.GetDays(); //获取这段时间间隔共有多少天

int iHour=span.GetTotalHours(); //获取总共有多少小时

int iMin=span.GetTotalMinutes();//获取总共有多少分钟

int iSec=span.GetTotalSeconds();//获取总共有多少秒

2、timeb()函数

_timeb定义在SYS\TIMEB.H,有四个fields

dstflag

millitm

time

timezone

void _ftime( struct _timeb *timeptr );

struct _timeb timebuffer;

_ftime( timebuffer );

参考资料来源:百度百科:time函数


名称栏目:包含c语言提取系统时间函数的词条
网页地址:http://cdkjz.cn/article/dochhoe.html
多年建站经验

多一份参考,总有益处

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

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

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