您的位置:首页 > 其它

Windows中如何计算时间间隔(1)使用CTime 和CTimeSpan

2016-10-08 11:16 393 查看
时间函数在我们的程序中是使用频率较高的函数,现将其归纳总结一下,这一章主要讲下CTime 和CTimeSpan,前者表示一个时间点,而后表示一个时间段。CTime代表的是绝对时间,CTime andCTimeSpan 没有虚函数,大部分函数为内联函数,类对象的大小都为8.[b]CTime
[/b]1.构造初始化CTime类对象
CTime类有下列构造函数:
CTime();
CTime(const CTime& timeSrc );
CTime(time_t time )
CTime(int nYear, int nMonth, int nDay, int nHour, int nMin, int nSec, int nDST = -1 );
CTime(WORD wDosDate, WORD wDosTime, int nDST = -1);
CTime(const SYSTEMTIME& sysTime, int nDST = -1);
CTime(const FILETIME& fileTime, int nDST = -1):
说明:以不同的方式构造一个CTime对象。可以用一个已经存在的CTime对象或一个time_t(在time.h中被定义为long)类型变量来构造初始化CTime对象,也可以用年、月、日、小时、分、秒来构造初始化CTime对象,还可以用SYSTEMTIME、FILETIME结构来构造初始化CTime对象。SYSTEMTIME、FILETIME结构定义如下:
typedef struct _SYSTEMTIME {
WORD wYear;
WORD wMonth;
WORD wDayOfWeek;
WORD wDay;
WORD wHour;
WORD wMinute;
WORD wSecond;
WORD wMilliseconds;
} SYSTEMTIME;
typedef struct _FILETIME {
DWORD dwLowDateTime;
DWORD dwHighDateTime;
} FILETIME, *PFILETIME, *LPFILETIME;
2.时间值的提取函数
(1)GetCurrentTime() 获取系统当前时间。
原型:static CTime PASCAL GetCurrentTime();
(2)GetTime() 由CTime对象返回一个time_t变量。
原型:time_t GetTime()const;
(3)GetYear()获取CTime对象代表的年。
原型:int GetYear()const;
以下(4)至(9)函数原型与GetYear()类似。
(4)GetMonth()获取CTime对象代表的月。
(5)GetDay()获取CTime对象代表的日期。
(6)GetHour()获取CTime对象代表的小时。
(7)GetMinute()获取CTime对象代表的分。
(8)GetSecond()获取CTime对象代表的秒。
(9)GetDayOfWeek()获取CTime对象代表的星期几,1代表周日、2代表周一、等等。
3.格式化时间
成员函数Format() 将CTime对象中的时间信息转化为一个格式化的字符串。其函数原型为:
CString Format(LPCTSTR pFormat ) const;
CString Format(UINT nFormatID ) const;
参数pFormat是格式字符串,类似于printf中的格式字符串,格式字符如下:
%a:周的英文缩写形式;
%A:周的英文全名形式;
%b: 月的英文缩写形式;
%B:月的英文全名形式;
%c: 完整的日期时间;
%d:十进制形式的日期(01-31);
%H:24小时制的小时(00-23);
%I: 12小时制的小时(00-11);
%j: 十进制表示的一年中的第几天(001-366);
%m: 月的十进制表示(01-12);
%M:十进制表示的分钟(00-59);
%p: 12小时制的上下午标示(AM/PM);
%S: 十进制表示的秒(00-59);
%U: 一年中的第几个星期(00-51),星期日是一周的第一天;
%W: 一年中的第几个星期(00-51),星期一是一周的第一天;
%w: 十进制表示的星期几(0-6);
%Y: 十进制表示的年;
参数nFormatID 是格式字符串资源的ID号。
4.重载运算符
operator = : 赋予新的时间。
operator + : 增加CTime和CTimeSpan对象。
operator – : 减小CTime和CTimeSpan对象。
operator += : CTime对象加一个CTimeSpan对象。
operator -= : CTime对象减一个CTimeSpan对象。
operator == : 比较两个绝对时间是否相等。
operator != : 比较两个绝对时间是否不相等。
operator > : 比较两个绝对时间,是否前一个大于后一个。
operator < : 比较两个绝对时间,是否前一个小于后一个。
operator >= : 比较两个绝对时间,是否前一个大于等于后一个。
operator <= : 比较两个绝对时间,是否前一个小于等于后一个。The upper date limit is 12/31/3000. The lower limit is 1/1/1970 12:00:00 AM GMT.CTimeSpan类
The CTimeSpan object is stored in a __time64_t structure, which is 8 bytes.
1.构造函数。
CTimeSpan类有下列构造函数:
(1)CTimeSpan() ;
(2)CTimeSpan(const CTimeSpan& timeSpanSrc);
(3)CTimeSpan(time_t time)(4)CTimeSpan(LONG lDays, int nHours, int nMins, int nSecs);
参数timeSpanSrc为一个已存在的 CTimeSpan 对象,time为一个time_t 类型的时间值,lDays, nHours, nMins, nSecs分别为天数、小时数、分数秒数。
2.时间值的提取函数
(1)GetDays()获得CTimeSpan类对象中包含的完整的天数。
(2)GetHours() 获得当天的小时数,值在-23到23之间。
(3)GetTotalHours() 获得CTimeSpan类对象中包含的完整的小时数。
(4)GetMinutes() 获得当前小时包含的分数,值在-59到59之间。
(5)GetTotalMinutes() 获得CTimeSpan类对象中包含的完整的分数。
(6)GetSeconds() 获得当前分钟包含的秒数,值在-59到59之间。
(7)GetTotalSeconds() 获得CTimeSpan类对象中包含的完整的秒数。
格式化时间
Format()将一个CTimeSpan对象转换成格式字符串。使用方式与CTime类似,格式化字符包括以下几个:
%D: CTimeSpan的总天数;
%H: 不足整天的小时数;
%M: 不足1小时的分数;
%S: 不足1分钟的秒数;
%%: 百分号。
4.重载运算符
CTimeSpan类也重载了运算符“=”,“+”,“-”,“+=”,“-=”,“==”,“!=”,“<”,“>”,“<=”,“>=”,用于CTimeSpan对象的赋值、加减运算及两个CTimeSpan对象的比较。 eg.#include <iostream>
#include <atltime.h>using namespace std;void main()
{
// CTime 函数
CTime curtime =CTime::GetCurrentTime(); int iyear = curtime.GetYear();
int imonth = curtime.GetMonth();
int iday = curtime.GetDay();
int idayofweek = curtime.GetDayOfWeek();
int ihour = curtime.GetHour();
int iminute = curtime.GetMinute();
int isecond = curtime.GetSecond(); cout<<"当前时间:"<<endl;
cout<<iyear<<"年";
cout<<imonth<<"月";
cout<<iday<<"日";
cout<<ihour<<"时";
cout<<iminute<<"分";
cout<<isecond<<"秒"<<endl;
cout<<"星期"<<idayofweek<<"(周日算1)"<<endl<<endl; // 时间比较
CTime begintime =CTime(2006,1,1,0,0,0);
cout<<"起始时间:"<<endl;
cout<<begintime.GetYear()<<"年";
cout<<begintime.GetMonth()<<"月";
cout<<begintime.GetDay()<<"日";
cout<<begintime.GetHour()<<"时";
cout<<begintime.GetMinute()<<"分";
cout<<begintime.GetSecond()<<"秒"<<endl;
cout<<"星期"<<begintime.GetDayOfWeek()<<"(周日算1)"<<endl<<endl; CTimeSpan span;
span = curtime - begintime;
cout<<"两时间相差:"<<endl;
cout<<span.GetDays()<<"天";
cout<<span.GetHours()<<"小时";
cout<<span.GetMinutes()<<"分";
cout<<span.GetSeconds()<<"秒"<<endl; cout<<"总小时"<<span.GetTotalHours()<<"小时"<<endl;
cout<<"总分钟"<<span.GetTotalMinutes()<<"分"<<endl;
cout<<"总秒"<<span.GetTotalSeconds()<<"秒"<<endl; cout<<endl;
// CTime到CString的转化
CString strtime;
strtime = curtime.Format(_T("%Y-%m-%d %X"));

wcout<<(LPCTSTR)strtime<<endl; // 因为使用UNICODE,或用下面的方面显示
wcout<<strtime.GetString()<<endl; system("pause");
}结果为当前时间:
2011年2月16日20时37分3秒
星期4(周日算1)起始时间:
2006年1月1日0时0分0秒
星期1(周日算1)两时间相差:
1872天20小时37分3秒
总小时44948小时
总分钟2696917分
总秒161815023秒2011-02-16 20:37:03
2011-02-16 20:37:03
请按任意键继续. . . 注意:在控制台中使用cout显示CString类,std::cout不支持宽字符,如果需要使用UNICODE,需要使用std::wcout输出宽字符。同时,需要对CString做一下转换,LPCTSTR(cpath)或者wcout<<cpath.GetString()<<endl;;或者使用NotSet或者Multi-CharSet就可以使用cout输出了.转载自: http://m.blog.csdn.net/article/details?id=6880464
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  MFC