获得系统时间

获得系统时间获得系统时间代码Include head file time.h, though it's a C include file, C++ certainly can use it.

Under C++, you can include<ctime>instead of<time.h>

___________



time.h





@函数名称: localtime

函数原型:
structtmlocaltime(consttime_ttimer)

函数功能: 返回一个以tm结构表达的机器时间信息

函数返回: 以tm结构表达的时间,结构tm定义如下:

structtm{

inttm_sec;

inttm_min;

inttm_hour;

inttm_mday;

inttm_mon;

inttm_year;

inttm_wday;

inttm_yday;

inttm_isdst;

};

参数说明: timer
-使用time()函数获得的机器时间

所属文件:
<time.h>



#include
<time.h>

#include
<stdio.h>

#include
<dos.h>

intmain()

{

time_t timer;

structtmtblock;

timer
=time(NULL);

tblock
=localtime(&timer);

printf(
"Local time is: %s",asctime(tblock));

return0;

}





@函数名称: asctime

函数原型:
charasctime(structtmptr)

函数功能: 得到机器时间(日期时间转换为ASCII码)

函数返回: 返回的时间字符串格式为:星期,月,日,小时:分:秒,年

参数说明: 结构指针ptr应通过函数localtime()和gmtime()得到

所属文件:
<time.h>



#include
<stdio.h>

#include
<string.h>

#include
<time.h>

intmain()

{

structtm t;

charstr[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);

return0;

}





@函数名称: ctime

函数原型:
charctime(longtime)

函数功能: 得到日历时间

函数返回: 返回字符串格式:星期,月,日,小时:分:秒,年

参数说明: time
-该参数应由函数time获得

所属文件:
<time.h>



#include
<stdio.h>

#include
<time.h>

intmain()

{

time_t t;

time(
&t);

printf(
"Today's date and time: %s",ctime(&t));

return0;

}





@函数名称: difftime

函数原型:
doubledifftime(time_t time2, time_t time1)

函数功能: 得到两次机器时间差,单位为秒

函数返回: 时间差,单位为秒

参数说明: time1
-机器时间一,time2-机器时间二.该参数应使用time函数获得

所属文件:
<time.h>



#include
<time.h>

#include
<stdio.h>

#include
<dos.h>

#include
<conio.h>

intmain()

{

time_t first, second;

clrscr();

first
=time(NULL);

delay(
2000);

second
=time(NULL);

printf(
"The difference is: %f seconds",difftime(second,first));

getch();

return0;

}





@函数名称: gmtime

函数原型:
structtmgmtime(time_ttime)

函数功能: 得到以结构tm表示的时间信息

函数返回: 以结构tm表示的时间信息指针

参数说明: time
-用函数time()得到的时间信息

所属文件:
<time.h>



#include
<stdio.h>

#include
<stdlib.h>

#include
<time.h>

#include
<dos.h>

chartzstr="TZ=PST8PDT";

intmain()

{

time_t t;

structtmgmt,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));

return0;

}





@函数名称: time

函数原型: time_t time(time_t
timer)

函数功能: 得到机器的日历时间或者设置日历时间

函数返回: 机器日历时间

参数说明: timer
=NULL时得到机器日历时间,timer=时间数值时,用于设置日历时间,time_t是一个long类型

所属文件:
<time.h>



#include
<time.h>

#include
<stdio.h>

#include
<dos.h>

intmain()

{

time_t t;

t
=time();

printf(
"The number of seconds since January 1,1970 is %ld",t);

return0;

}

@函数名称: tzset

函数原型:
voidtzset(void)

函数功能: UNIX兼容函数,用于得到时区,在DOS环境下无用途

函数返回:

参数说明:

所属文件:
<time.h>



#include
<time.h>

#include
<stdlib.h>

#include
<stdio.h>

intmain()

{

time_t td;

putenv(
"TZ=PST8PDT");

tzset();

time(
&td);

printf(
"Current time=%s",asctime(localtime(&td)));

return0;

}



/方案— 优点:仅使用C标准库;缺点:只能精确到秒级

#include
<time.h>

#include
<stdio.h>

intmain(void)

{

time_t t
=time(0);

chartmp[64];

strftime( tmp,
sizeof(tmp),"%Y/%m/%d %X %A 本年第%j天 %z",localtime(&t) );

puts( tmp );

return0;

}

size_t strftime(
charstrDest, size_t maxsize,constcharformat,conststructtmtimeptr);

根据格式字符串生成字符串。

structtmlocaltime(consttime_t*timer);

取得当地时间,localtime获取的结果由结构tm返回

返回的字符串可以依下列的格式而定:

%a 星期几的缩写。Eg:Tue

%A 星期几的全名。 Eg: Tuesday

%b 月份名称的缩写。

%B 月份名称的全名。

%c 本地端日期时间较佳表示字符串。

%d 用数字表示本月的第几天 (范围为0031)。日期

%H 用24小时制数字表示小时数 (范围为0023)。

%I 用12小时制数字表示小时数 (范围为0112)。

%j 以数字表示当年度的第几天 (范围为001366)。

%m 月份的数字 (范围由112)。

%M 分钟。

%p 以''AM''''PM''表示本地端时间。

%S 秒数。

%U 数字表示为本年度的第几周,第一个星期由第一个周日开始。

%W 数字表示为本年度的第几周,第一个星期由第一个周一开始。

%w 用数字表示本周的第几天 (0为周日)。

%x 不含时间的日期表示法。

%X 不含日期的时间表示法。 Eg:15:26:30

%y 二位数字表示年份 (范围由0099)。

%Y 完整的年份数字表示,即四位数。 Eg:2008

%Z(%z) 时区或名称缩写。Eg:中国标准时间

%%%字符。



//方案二 优点:能精确到毫秒级;缺点:使用了windows API

#include<windows.h>

#include
<stdio.h>

intmain(void)

{

SYSTEMTIME sys;

GetLocalTime(
&sys );

printf(
"%4d/%02d/%02d %02d:%02d:%02d.%03d 星期%1dn",sys.wYear,sys.wMonth,sys.wDay,sys.wHour,sys.wMinute, sys.wSecond,sys.wMilliseconds,sys.wDayOfWeek);

return0;

}



//方案三,优点:利用系统函数,还能修改系统时间

//此文件必须是c++文件

#include<stdlib.h>

#include
<iostream>

usingnamespacestd;

voidmain()

{

system(
"time");

}



//方案四,将当前时间折算为秒级,再通过相应的时间换算即可

//此文件必须是c++文件

#include<iostream>

#include
<ctime>

usingnamespacestd;

intmain()

{

time_t now_time;

now_time
=time(NULL);

cout
<<now_time;

return0;

}

原文链接: https://www.cnblogs.com/duzouzhe/archive/2010/03/11/1683750.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/8746

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月6日 下午8:00
下一篇 2023年2月6日 下午8:00

相关推荐