如何在evc中显示系统时间?
在vc中可如下显示时间:
CTime tm;
CString m_sTime;
tm = CTime::GetCurrentTime();
m_sTime = tm.Format("%Y-%m-%d %X");
可evc不支持CTime类的Format方法,在evc中该如何显示出通常所用的时间格式呢
//*****CTime class*****///EVC
class CTime
{
public:
// Constructors
static CTime PASCAL GetCurrentTime();
CTime();
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 CTime& timeSrc);
CTime(const SYSTEMTIME& sysTime, int nDST = -1);
CTime(const FILETIME& fileTime, int nDST = -1);
const CTime& operator=(const CTime& timeSrc);
const CTime& operator=(time_t t);
// Attributes
struct tm* GetGmtTm(struct tm* ptm = NULL) const;
struct tm* GetLocalTm(struct tm* ptm = NULL) const;
BOOL GetAsSystemTime(SYSTEMTIME& timeDest) const;
time_t GetTime() const;
int GetYear() const;
int GetMonth() const; // month of year (1 = Jan)
int GetDay() const; // day of month
int GetHour() const;
int GetMinute() const;
int GetSecond() const;
int GetDayOfWeek() const; // 1=Sun, 2=Mon, ..., 7=Sat
// Operations
// time math
CTimeSpan operator-(CTime time) const;
CTime operator-(CTimeSpan timeSpan) const;
CTime operator+(CTimeSpan timeSpan) const;
const CTime& operator+=(CTimeSpan timeSpan);
const CTime& operator-=(CTimeSpan timeSpan);
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;
// formatting using "C" strftime
CString Format(LPCTSTR pFormat) const;
CString FormatGmt(LPCTSTR pFormat) const;
CString Format(UINT nFormatID) const;
CString FormatGmt(UINT nFormatID) const;
#ifdef _UNICODE
// for compatibility with MFC 3.x
CString Format(LPCSTR pFormat) const; //
CString FormatGmt(LPCSTR pFormat) const;
#endif
// serialization
#ifdef _DEBUG
friend CDumpContext& AFXAPI operator<<(CDumpContext& dc, CTime time);
#endif
friend CArchive& AFXAPI operator<<(CArchive& ar, CTime time);
friend CArchive& AFXAPI operator>>(CArchive& ar, CTime& rtime);
private:
time_t m_time;
};
//*****CTime class*****///Evc
我们采用sprintf();
方法一:
CTime tm;
CString m_sTime;
char strTime[20];
tm = CTime::GetCurrentTime();
sprintf(strTime,"%04-%02-%02 %02:%02:%02",
tm.GetYear(),tm.GetMonth(),
tm.GetDay(),tm.GetHour,tm.GetMinute(),
tm.GetSecond());
m_sTime=strTime;
//上面方法还是不会返回正确的Unicode码的
方法二:
CTime CrtTime;
CString TimeStr;
char cFormatStr[20] = {0};
CrtTime = CTime::GetCurrentTime();
sprintf(cFormatStr,"%4d-%2d-%2d %2d:%2d:%2d",
CrtTime.GetYear(),CrtTime.GetMonth(),CrtTime.GetDay(),
CrtTime.GetHour(),CrtTime.GetMinute(),
CrtTime.GetSecond());
TimeStr = (CString)cFormatStr;
方法三:
TCHAR strTime[20];
SYSTEMTIME time;
GetSystemTime(&time);
swprintf(strTime,_T("%04d-%02d-%02d %02d:%02d:%02d"),time.wYear,time.wMonth,time.wDay,
time.wHour,time.wMinute,time.wSecond);
方法四:
其实方法三中不采用MFC CTime 类
自己写的采用MFC CTime类的代码 //已经测试
CString CADMGDLG::getCurrentTime()
{
//get current time
TCHAR strTime[20]; //足够大,要不然测试的时候会出现乱码,因为没有足够的空间来存储数据
CTime t=CTime::GetCurrentTime();
swprintf(strTime,_T("%04d-%02d-%02d %02d:%02d:%02d"),t.GetYear(),t.GetMonth(),t.GetDay(),
t.GetHour(),t.GetMinute(),t.GetSecond());
CString nTime=(CString)strTime;
return nTime;
}
nTime中保存正确的Unicode
如果你想测试的话
在STATIC Text 上显示的代码:
在Button Click Message 响应函数中添加代码
CString text=_T("");
text=getCurrentTime();
GetDlgItem(IDC_STATIC。。。)->SetWindowText(text);
IDC_STATIC。。。为你添加的STATIC Text 控件的ID。
基于Dialog的MFC 。。EXE项目
运行程序的时候,按下相应的按钮,时间就显示在ID为IDC_STATIC。。。的控件上