这节制作的是Ctime类的学习:
1.初始化 m_begintime=CTime(2004,1,1,0,0,0,-1);//参数依次为
year,month,day,hour,minite,second,nDST
m_endtime =CTime::GetCurrentTime();//当前时间
2.日期比较
CTimeSpan span;
span=time1-time2;
得到两时间的间隔.
可以取得span.GetHours().等
3.access数据库查询
使用DateDiff()函数,具体参照access帮助
CString timesql;
timesql.Format(" Where DateDiff('d',%s,'%s')<=0","日期",m_begintime.Format("%Y-%m-%d"));
4读取日期字段(odbc)
CDBVariant var;
recset.GetFieldValue(i,var);
s.Format("%d-%d-%d",(var.m_pdate)->year,(var.m_pdate)->month,
(var.m_pdate)->day);
5.CTime转换为CString
例:
m_begintime.Format("%Y-%m-%d");//2004-10-03
6.CString转换为CTime
//s="2004-10-5"
int first=s.Find('-');
int second=s.Find('-',first+1);
int year=atoi(s.Left(4));
int month=atoi(s.Mid(first+1,second-first+1));
int day=atoi(s.Mid(second+1,s.GetLength()-second-1));
CTime temp(year,month,day,0,0,0);
7.判断CString是否表示的正确日期格式
//判断是否为2004-01-13 ch 可代表其他分隔符
bool IsDate(CString str,char ch)
{
if(str.IsEmpty()) return false;
//日期分段
int first=str.Find(ch);
int second=str.Find(ch,first+1);
int year=atoi(str.Left(4));
int month=atoi(str.Mid(first+1,second-first+1));
int day=atoi(str.Mid(second+1,str.GetLength()-second-1));
//判断
if (year < 2000 || year >= 2010)
{
return false;
}
else if (month< 1 || month >12)
{
return false;
}
else if (day< 1 || day > 31)
{
return false;
}
else if (month == 4 || month == 6 || month == 9 || month == 11)
{
if(day > 30)
{
return false;
}
else
{
return true;
}
}
else if (month == '2')
{
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
{
if (day>29)
{
return false;
}
else
{
return true;
}
}
else if (day>28)
{
return false;
}
return true;
}
else
{
return true;
}
}
用VC编写数据库程序不可避免的会遇到_bstr_t 、CString 、CTime这几个类型之间的转换问题,令人头疼。
解决:CString和CTime的类型转换问题。
//CTime--〉CString
CTime t;
t=CTime::GetCurrentTime();
CString sur;
sur.Format("%s",t.Format("%Y-%m-%d"));
MessageBox(sur);
//long --> CString
long a ;
CString b;
b.format("%ld",a);
//double --->CString
double a;
CString b;
b.format("%f",a);
//CString--〉CTime
CString sur;
sur="2006-08-09";
COleDateTime time1;
time1.ParseDateTime(sur);
SYSTEMTIME systime;
VariantTimeToSystemTime(time1, &systime);
CTime tm(systime);
m_time=tm;
UpdateData(false);
CString和int,float之间的转换。
1。int <->CString
1) int ->CString
int n = 1;
CString str;
str.Format("%d",n);
2) CString->int
CString str = "1";
int n = atoi(str.GetBuffer(0));
2. char* 与CString
1)char*->CString
char sz[128];
CString str;
str.Format("%s",sz);
2) CString -> char*
CString str;
int nLength = str.GetLength();
char* sz = new char[nLength];
sz = str.GetBuffer(0);
3. float<->CString
1)float->CString
float f = 0.0;
CString str;
str.Format("%f",f);
2) CString->float
CString str = "0.0";
float f = atof(str.GetBuffer(0));
Format函数第一个参数是要转的那个数的类型
FileTime 和 CTime之间的转换问题
1. FileTime 转换成 CTime
方法(1)
FILETIME ft;
CTime time(ft);
方法(2)
FILETIME ft;
SYSTEMTIME st;
BOOL bSuccess=::FileTimeToSystemTime(&ft, &st)
if (bSuccess) //转换为SYSTEMTIME成功,下面转换成CTime
CTime time(st);
2. CTime 转换成 FileTime
CTime time(CTime::GetCurrentTime());
SYSTEMTIME st;
time.GetAsSystemTime(st);
FILETIME ft;
::SystemTimeToFileTime(&st, &ft);
简单实现:
void CMFCtimeDlg::OnBnClickedButton1() { CTime t = CTime::GetCurrentTime(); int nYear = t.GetYear(); int nMonth = t.GetMonth(); int nDay=t.GetDay(); int nHour = t.GetHour(); int nMin = t.GetMinute(); int nSec = t.GetSecond(); int nWeek = t.GetDayOfWeek(); CString str; str.Format(_T("当前时间是:%d年%02d月%02d日 %02d:%02d:%02d"), nYear,nMonth,nDay,nHour,nMin,nSec); //AfxMessageBox(str); this->SetWindowText(str);//SetTimer } void CMFCtimeDlg::OnBnClickedButton2() { time_t tt = time(NULL); tm *ptime=localtime(&tt); int nYear = ptime->tm_year + 1900; int nMon = ptime->tm_mon + 1; int nDay = ptime->tm_mday; int nHour = ptime->tm_hour; int nMin = ptime->tm_min; int Sec = ptime->tm_sec; CString str; str.Format(_T("当前时间是:%d年%02d月%02d日 %02d:%02d:%02d"), nYear, nMon, nDay, nHour, nMin, Sec); //AfxMessageBox(str); this->SetWindowText(str);//SetTimer }结果显示图:
接下来学习一下CTime类对于time的封装。
新建win32程序(非应用),然后右击项目添加CMyTime,设置为静态类型,返回类型为CMyTime,函数名为GetCurrentTime,和几个年月日时分秒周的获取函数(const),分别在各个函数里添加返回值
具体代码如下:
#include "stdafx.h" #include "MyTime.h" CMyTime::CMyTime() { } CMyTime::~CMyTime() { } CMyTime CMyTime::GetCurrentTime() { CMyTime t; t.m_time = time(NULL); return t; } int CMyTime::GetYear() const { struct tm* pTime = localtime(&m_time); return pTime->tm_year+1900; } int CMyTime::GetMonth() const { struct tm* pTime = localtime(&m_time); return pTime->tm_mon + 1; } int CMyTime::GetDay() const { struct tm* pTime = localtime(&m_time); return pTime->tm_mday; } int CMyTime::GetHour() const { struct tm* pTime = localtime(&m_time); return pTime->tm_hour; } int CMyTime::GetMinute() const { struct tm* pTime = localtime(&m_time); return pTime->tm_min; } int CMyTime::GetSecond() const { struct tm* pTime = localtime(&m_time); return pTime->tm_sec; } int CMyTime::GetDayOfWeek() const { struct tm* pTime = localtime(&m_time); return pTime->tm_wday; }CMyTime的类封装好然后在win32平台进行调用,点开win32的cpp文件,
添加头文件:
#include <stdio.h>
#include "MyTime.h"
并且在主函数中添加:
int APIENTRY _tWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPTSTR lpCmdLine, _In_ int nCmdShow) { UNREFERENCED_PARAMETER(hPrevInstance); UNREFERENCED_PARAMETER(lpCmdLine); // TODO: 在此放置代码。 CMyTime t = CMyTime::GetCurrentTime(); int nYear = t.GetYear(); int nMonth = t.GetMonth(); int nDay = t.GetDay(); int nHour = t.GetHour(); int nMin = t.GetMinute(); int nSec = t.GetSecond(); int nWeek = t.GetDayOfWeek();这样就调试可以实现封装类的调用。