Windows下C/C++获取当前系统时间
【原文】http://blog.csdn.net/dadalan/article/details/5771693
写软件时经常要用到获取系统时间显示到状态栏,这里在前人的基础上总结了一下几种方案。
方案一:localtime()
time_t是定义在time.h中的一个类型,表示一个日历时间,也就是从1970年1月1日0时0分0秒到此时的秒数,原型是:
typedef long time_t; /* time value */
可以看出time_t其实是一个长整型,由于长整型能表示的数值有限,因此它能表示的最迟时间是2038年1月18日19时14分07秒。
函数time可以获取当前日历时间时间,time的定义:
time_t time(time_t *)
time_t (typedef __int64 time_t )只是一个长整型,不符合我们的使用习惯,需要转换成本地时间,就要用到tm结构,time.h中结构tm的原型是:
- struct tm {
- int tm_sec; /* seconds after the minute - [0,59] */
- int tm_min; /* minutes after the hour - [0,59] */
- int tm_hour; /* hours since midnight - [0,23] */
- int tm_mday; /* day of the month - [1,31] */
- int tm_mon; /* months since January - [0,11] */
- int tm_year; /* years since 1900 */
- int tm_wday; /* days since Sunday - [0,6] */
- int tm_yday; /* days since January 1 - [0,365] */
- int tm_isdst; /* daylight savings time flag */
- };
可以看出,这个机构定义了年、月、日、时、分、秒、星期、当年中的某一天、夏令时。可以用这个结构很方便的显示时间。
用localtime获取当前系统时间,该函数将一个time_t时间转换成tm结构表示的时间,函数原型:
struct tm * localtime(const time_t *)
使用gmtime函数获取格林尼治时间,函数原型:
struct tm * gmtime(const time_t *)
输出方式1:
- #include <iostream>
- #include <time.h>
- using namespace std;
- void dsptime(const struct tm *); //输出时间。
- int main(void)
- {
- time_t nowtime;
- nowtime = time(NULL); //获取日历时间
- cout << nowtime << endl; //输出nowtime
- struct tm *local,*gm;
- local=localtime(&nowtime); //获取当前系统时间
- dsptime(local);
- gm=gmtime(&nowtime); //获取格林尼治时间
- dsptime(gm);
- return 0;
- }
- void dsptime(const struct tm * ptm)
- {
- char *pxq[]={"日","一","二","三","四","五","六"};
- cout << ptm->tm_year+1900 << "年" << ptm->tm_mon+1 << "月" << ptm->tm_mday << "日 " ;
- cout << ptm->tm_hour << ":" << ptm->tm_min << ":" << ptm->tm_sec <<" " ;
- cout << " 星期" <<pxq[ptm->tm_wday] << " 当年的第" << ptm->tm_yday << "天 " << endl;
- }
输出方式2:
- #include <time.h>
- #include <stdio.h>
- int main( void )
- {
- time_t t = time(0);
- char tmp[64];
- strftime( tmp, sizeof(tmp), "%Y/%m/%d %X %A 本年第%j天 %z",localtime(&t) );
- puts( tmp );
- return 0;
- }
C/C++在time.h中提供了一个自定义时间格式的函数strftime,函数原型:
size_t strftime(char *strDest, size_t maxsize, const char *format, const struct tm *timeptr);
参数说明:
char *strDest:用来存放格式化后的字符串缓存,
size_t maxsize:指定最多可以输出的字符数,
const char *format:格式化字符串,
const struct tm *timeptr:要转换的时间。可使用的格式化字符串:
%a 星期几的简写
%A 星期几的全称
%b 月分的简写
%B 月份的全称
%c 标准的日期的时间串
%C 年份的后两位数字
%d 十进制表示的每月的第几天
%D 月/天/年
%e 在两字符域中,十进制表示的每月的第几天
%F 年-月-日
%g 年份的后两位数字,使用基于周的年
%G 年分,使用基于周的年
%h 简写的月份名
%H 24小时制的小时
%I 12小时制的小时
%j 十进制表示的每年的第几天
%m 十进制表示的月份
%M 十时制表示的分钟数
%n 新行符
%p 本地的AM或PM的等价显示
%r 12小时的时间
%R 显示小时和分钟:hh:mm
%S 十进制的秒数
%t 水平制表符
%T 显示时分秒:hh:mm:ss
%u 每周的第几天,星期一为第一天 (值从0到6,星期一为0)
%U 第年的第几周,把星期日做为第一天(值从0到53)
%V 每年的第几周,使用基于周的年
%w 十进制表示的星期几(值从0到6,星期天为0)
%W 每年的第几周,把星期一做为第一天(值从0到53)
%x 标准的日期串
%X 标准的时间串
%y 不带世纪的十进制年份(值从0到99)
%Y 带世纪部分的十进制年份
%z,%Z 时区名称,如果不能得到时区名称则返回空字符。
%% 百分号
方案二:GetLocalTime( &sys );
优点:能精确到毫秒级;缺点:使用了windows API
- #include <windows.h>
- #include <stdio.h>
- int main( void )
- {
- SYSTEMTIME sys;
- GetLocalTime( &sys );
- printf( "%4d/%02d/%02d %02d:%02d:%02d.%03d 星期%1d/n",sys.wYear,sys.wMonth,sys.wDay,sys.wHour,sys.wMinute, sys.wSecond,sys.wMilliseconds,sys.wDayOfWeek);
- return 0;
- }
方案三:system("time");
- //此文件必须是c++文件
- #include<stdlib.h>
- #include<iostream>
- using namespace std;
- void main()
- {
- system("time");
- }
方案四:time(null)
将当前时间折算为秒级,再通过相应的时间换算即可- //此文件必须是c++文件
- #include<iostream>
- #include<ctime>
- using namespace std;
- int main()
- {
- time_t now_time;
- now_time = time(NULL);
- cout<<now_time;
- return 0;
- }
方案五:CTime
- CString str; //获取系统时间
- CTime tm; tm=CTime::GetCurrentTime();
- str=tm.Format("%Y-%m-%d %H:%M:%S"); //主要是Y m d,H M S中间的连接符自己定义
- MessageBox(str,NULL,MB_OK);
- SYSTEMTIME st;
- CString strDate,strTime;
- GetLocalTime(&st);
- strDate.Format("%4d-%2d-%2d",st.wYear,st.wMonth,st.wDay);
- strTime.Format("%2d:%2d:%2d",st.wHour,st.wMinute,st.wSecond);
注释:
如何在C++中将filetime时间转化为字符串?
先
filetime
转化为systemtime再
SYSTEMTIME st;
char strTime[128];
sprintf(strTime,"%d-%d-%d %d:%d:%d",st.wYear,st.wMonth,st.wDay,st.wHour,st.wMinute,st.wSecond);
参考:
如何在C++中将filetime时间转化为常规时间格式?
http://www.xuebuyuan.com/1462191.html
再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow