CTime .详解

来源:互联网 发布:乐易编程网 编辑:程序博客网 时间:2024/06/11 00:13
CTime


CTime==>CString

CTime time;
time.GetCurrentTime();
CString str;
str.Format("%s",time.Format("%y:%m:%d %H-%M-%S")

1
CString str;
CTime t = CTime::GetCurrentTime();
str.Format("%d-%d-%d",t.GetYear(),t.GetMonth(),t.GetDay());
2
CString strTime;
CTime tTime = CTime::GetCurrentTime();
strTime = tTime.Format("%Y-%m-%d %H:%M:%S");

CString ==>CTime

strCString="2003-10-27 6:24:37"; //CString--->COleDateTime
COleVariant vtime(strCString);
vtime.ChangeType(VT_DATE);
COleDateTime time4=vtime;

COleDateTime time1(1977,4,16,2,2,2); //COleDataTime--->CTime
SYSTEMTIME systime;
VariantTimeToSystemTime(time1, &systime);
CTime tm(systime);

time_t time2=tm.GetTime(); //CTime--->time_t
COleDateTime time3(time2); //time_t--->COleDateTime

时间差
COleDateTime strFirst,strEnd;
strFirst = COleDateTime(strFirst.GetYear(),strFirst.GetMonth(),strFirst.GetDay(),0,0,0);
strEnd = COleDateTime(tmCurrent.GetYear(),tmCurrent.GetMonth(),tmCurrent.GetDay(),23,59,59);
tspan= strEnd - strFirst;
注:两个时间相减时,小时数不足24小时的,自动略去,不做一天计算。如7.1号 5点与7.2号3点间,就不能计算为一天。想要得出结果为一天的,要注意小时的赋值!!

CTime 使用总结

初始化 m_begintime=CTime(2004,1,1,0,0,0,-1);//参数依次为year,month,day,hour,minite,second
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

 

CTimeSpan

1.构造函数。

CTimeSpan类有下列构造函数:

1CTimeSpan();

2CTimeSpanconst CTimeSpan& timeSpanSrc;

3CTimeSpantime_t time;

4CTimeSpanLONG lDays, int nHours, int nMins, int nSecs;

参数timeSpanSrc为一个已存在的CTimeSpan对象,time为一个time_t类型的时间值,lDays, nHours, nMins, nSecs分别为天数、小时数、分数和秒数。

2.时间值的提取函数

1GetDays()  获得CTimeSpan类对象中包含的完整的天数。

2GetHours()  获得当天的小时数,值在-2323之间。

3GetTotalHours()  获得CTimeSpan类对象中包含的完整的小时数。

4GetMinutes()  获得当前小时包含的分数,值在-5959之间。

5GetTotalMinutes() 获得CTimeSpan类对象中包含的完整的分数。

6GetSeconds()  获得当前分钟包含的秒数,值在-5959之间。

7GetTotalSeconds() 获得CTimeSpan类对象中包含的完整的秒数。

格式化时间

Format()将一个CTimeSpan对象转换成格式字符串。使用方式与CTime类似,格式化字符包括以下几个:

%D   CTimeSpan的总天数;

%H   不足整天的小时数;

%M   不足1小时的分数;

%S   不足1分钟的秒数;

%%   百分号。

4.重载运算符

CTimeSpan类也重载了运算符“=”“+”“-”“+=”“-=”“==”=”“<”“>”“<=”“>=”,用于CTimeSpan对象的赋值、加减运算及两个CTimeSpan对象的比较。

例子代码

构造一个CTimeSpan对象,并获取其中的完整天数、小时数、分数和秒数,将获得的信息在信息框中显示。

CTimeSpan m_timespan(3,4,5,6);       //  3天,4小时,5分,6

LONG m_totalDays=m_timespan.GetDays();                //获得完整天数

LONG m_totalHours=m_timespan.GetTotalHours();         //获得完整小时数

LONG m_totalMinutes=m_timespan.GetTotalMinutes();   //获得完整分数

LONG m_totalSeconds=m_timespan.GetTotalSeconds();   //获得完整秒数

char s1[8], s2[8], s3[8], s4[8];

wsprintf(s1,"%ld",m_totalDays);

wsprintf(s2,"%ld",m_totalHours);

wsprintf(s3,"%ld",m_totalMinutes);

wsprintf(s4,"%ld",m_totalSeconds);

CString m_str = "此时间范围包含:/n完整天数:"+CString(s1)+"/n完整小时数:"+CString(s2)+"/n完整分数:"+CString(s3)+"/n完整秒数:"+CString(s4);

MessageBox(m_str);

运行结果在信息框中显示5行,其中s1的内容为“3”s2的内容为“76”3*24+4),s3的内容为“4565”76*60+5),s4的内容为“273906”4565*60+6)。

 

CTimeSpan::CTimeSpan

Visual Studio 2005

 

Constructs CTimeSpan objects in various ways.

CTimeSpan( ) throw( );
CTimeSpan(
   __time64_t time
) throw( );
CTimeSpan(
   LONG lDays,
   int nHours,
   int nMins,
   int nSecs
) throw( );

 

ParameterstimeSpanSrc

A CTimeSpan object that already exists.

Remarks
  

lDays

0–25,000 (approximately)

nHours

0–23

nMins

0–59

nSecs

0–59

Note that the Debug version of the Microsoft Foundation Class Library asserts if one or more of the time-day components is out of range. It is your responsibility to validate the arguments prior to calling.

Example
CTimeSpan ts1;  // Uninitialized time value
CTimeSpan ts2a(ts1); // Copy constructor
CTimeSpan ts2b = ts1; // Copy constructor again
CTimeSpan ts3(100); // 100 seconds
CTimeSpan ts4(0, 1, 5, 12);    // 1 hour, 5 minutes, and 12 seconds  

CTimeSpan::Format

Generates a formatted string that corresponds to this CTimeSpan.

CString Format(
   LPCSTR pFormat
) const;
CString Format(
   LPCTSTR pszFormat
) const;
CString Format(
   UINT nID
) const;

ParameterspFormat, pszFormat

A formatting string similar to the printf formatting string. Formatting codes, preceded by a percent (%) sign, are replaced by the correspondingCTimeSpan component. Other characters in the formatting string are copied unchanged to the returned string. The value and meaning of the formatting codes forFormat are listed below:

%D   Total days in this CTimeSpan

%H   Hours in the current day

%M   Minutes in the current hour

%S   Seconds in the current minute

%%   Percent sign

nID

The ID of the string that identifies this format.

A CString object that contains the formatted time.

Remarks

The Debug version of the library checks the formatting codes and asserts if the code is not in the list above.

Example
// example for CTimeSpan::Format CTimeSpan ts( 3, 1, 5, 12 );// 3 days, 1 hour, 5 min, and 12 sec
CString s = ts.Format( "Total days: %D, hours: %H, mins: %M, secs: %S" );
 ATLASSERT( s == "Total days: 3, hours: 01, mins: 05, secs: 12" );
原创粉丝点击