国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
CTime類,CTime 與 CString轉換

CTime類,CTime 與 CString轉換

轉載 2012年06月11日 19:46:08 標簽:date /byte /c 12951

CTime類,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 later

ASSERT( 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, 1999

CTime t2( 1999, 3, 20, 22, 15, 0 ); // 10:15PM March 20, 1999

CTimeSpan ts = t2 - t1; // Subtract 2 Ctimes

ASSERT( 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    year   

   memset(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    month   

   memset(tt,    0,    sizeof(tt));   

   tt[0]    =    timestr[6];   

   tt[1]    =    timestr[7];   

   month    =    atoi((char    *)tt);   

    

   //get    day   

   memset(tt,    0,    sizeof(tt));   

   tt[0]    =    timestr[10];   

   tt[1]    =    timestr[11];   

    

   CTime    time(year,month,day,0,0,0);

從上面來看,很明顯使用sscanf()函數的優(yōu)勢.

 

二.將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
原創(chuàng) 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()     獲取系統(tǒng)當前時間。
原型: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
請按任意鍵繼續(xù). . .
 
注意:
在控制臺中使用cout顯示CString類,
std::cout不支持寬字符,如果需要使用UNICODE,需要使用std::wcout輸出寬字符。同時,需要對CString做一下轉換,LPCTSTR(cpath)或者wcout<<cpath.GetString()<<endl;;或者使用NotSet或者Multi-CharSet就可以使用cout輸出了
本站僅提供存儲服務,所有內容均由用戶發(fā)布,如發(fā)現有害或侵權內容,請點擊舉報
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
VS2010/MFC編程入門之四十三(MFC常用類:CTime類和CTimeSpan類)
COleDatetime和CTime的區(qū)別
MFC常用類、成員函數、數組類、Cstring類、CTime類、CPoint類
用CTime類得到當前日期、時間、星期,并格式化輸出
placement new體驗
C++賦值運算符、函數調用運算符、下標運算符(“=”、“()”、“[]”)重載
更多類似文章 >>
生活服務
分享 收藏 導長圖 關注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點擊這里聯系客服!

聯系客服