CTime类,CTime 与 CString转换转载 2012年06月11日 19:46:08 标签:date /byte /c 12951CTime类,CTime 与 CString转换1 获取当前时间CTime time = CTime::GetCurrentTime();其中GetYear( ),GetMonth( ), GetDay( ), GetHour( ), GetMinute( ), GetSecond( ), GetDayOfWeek( ) 返回整型(int)对应项目例:int NowMonth = time1.GetMonth();2 两个时间之间的比较BOOL operator ==( CTime time ) const;BOOL operator !=( CTime time ) const;BOOL operator <( CTime time ) const;BOOL operator >( CTime time ) const;BOOL operator <=( CTime time ) const;BOOL operator >=( CTime time ) const;例:CTime t1 = CTime::GetCurrentTime();CTime t2 = t1 + CTimeSpan( 1, 0, 0, 0 ); // 1 Day laterASSERT( t1 != t2 );ASSERT( t1 < t2 );ASSERT( t1 <= t2 );3 时间差CTime operator +( CTimeSpan timeSpan ) const;CTime operator - ( CTimeSpan timeSpan ) const;CTimeSpan operator - ( CTime time ) const;例:CTime t1( 1999, 3, 19, 22, 15, 0 ); // 10:15PM March 19, 1999CTime t2( 1999, 3, 20, 22, 15, 0 ); // 10:15PM March 20, 1999CTimeSpan ts = t2 - t1; // Subtract 2 CtimesASSERT( ts.GetTotalSeconds() == 86400L );ASSERT( ( t1 + ts ) == t2 ); // Add a CTimeSpan to a CTime.ASSERT( ( t2 - ts ) == t1 ); // Subtract a CTimeSpan from a CTime.4 时间格式化将当前时间格式化 CString date = time.Format("%Y-%m-%d %H:%M:%S %W-%A");结果为:2006-10-13 17:23:47 41-Friday----------------------------------------C++中,CTime 与 CString转换CTime m_StartTime1 = CTime::GetCurrentTime();CString csStartTime = m_StartTime1.Format( "%Y%m%d%H%M%S" );一.将CString转为CTime的几种方法CString timestr = "2000年04月05日";int a,b,c ;sscanf(timestr.GetBuffer(timestr.GetLength()),"%d年%d月%d日",&a,&b,&c);CTime time(a,b,c,0,0,0);--------or - ---------------------CString s("2001-8-29 19:06:23");int nYear, nMonth, nDate, nHour, nMin, nSec;sscanf(s, "%d-%d-%d %d:%d:%d", &nYear, &nMonth, &nDate, &nHour, &nMin, &nSec);CTime t(nYear, nMonth, nDate, nHour, nMin, nSec);---- or ------------------------CString timestr = "2000年04月05日";int year,month,day;BYTE tt[5];//get yearmemset(tt, 0, sizeof(tt));tt[0] = timestr[0];tt[1] = timestr[1];tt[2] = timestr[2];tt[3] = timestr[3];year= atoi((char *)tt);//get monthmemset(tt, 0, sizeof(tt));tt[0] = timestr[6];tt[1] = timestr[7];month = atoi((char *)tt);//get daymemset(tt, 0, sizeof(tt));tt[0] = timestr[10];tt[1] = timestr[11];CTime time(year,month,day,0,0,0);从上面来看,很明显使用sscanf()函数的优势.二.将CTIme转换为CString的方法:CTime tmSCan = CTime::GetCurrentTime();CString szTime = tmScan.Format("'%Y-%m-%d %H:%M:%S'");这样得到的日期时间字符串就是以"2006-11-27 23:30:59"的格式.这是不是很方便呢?//取得CTime中的日期CString cstrDate = tmScan.Format("%Y-%m-%d");//取得CTime中的时间CString cstrTime = tmScan.Format("%H:%M-%S");*************************************************************** Windows中如何计算时间间隔(1)使用CTime 和CTimeSpan 原创 2011年10月17日 10:56:22 标签:windows /pascal /structure /struct /object /system 1038 时间函数在我们的程序中是使用频率较高的函数,现将其归纳总结一下,这一章主要讲下CTime 和CTimeSpan,前者表示一个时间点,而后表示一个时间段。CTime代表的是绝对时间,CTime andCTimeSpan 没有虚函数,大部分函数为内联函数,类对象的大小都为8. CTime类 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输出了 |
|