CTime、CTimeSpan、COleDateTime、COleDateTimeSpan

来源:互联网 发布:伯爵和唯一视觉知乎 编辑:程序博客网 时间:2024/05/16 20:28

CTime和CTimeSpan主要封装了ANSI time_t和关于time_t的Run-Time库的主要函数,CTime里面使用的成员变量是time_t类型,该类型是个long型,由于long 类型的原因,所以该类只能处理4294967296秒约68年的数据,所以用CTime只能处理1970年到2038年的日期。

CTime Class Members

Construction

CTimeConstructs CTime objects in various ways.GetCurrentTimeCreates a CTime object that represents the current time (static member function).

Extraction

GetTimeReturns a time_t that corresponds to this CTime object.GetYearReturns the year that this CTime object represents.GetMonthReturns the month that this CTime object represents (1 through 12).GetDayReturns the day that this CTime object represents (1 through 31).GetHourReturns the hour that this CTime object represents (0 through 23).GetMinuteReturns the minute that this CTime object represents (0 through 59).GetSecondReturns the second that this CTime object represents (0 through 59).GetDayOfWeekReturns the day of the week (1 for Sunday, 2 for Monday, and so forth).
获取CTime对象代表的周日,1代表周日,2代表周一等

Conversion

GetGmtTmBreaks down a CTime object into components — based on UTC.GetLocalTmBreaks down a CTime object into components — based on the local time zone.GetAsSystemTimeConverts the time information stored in the CTime object to a Win32-compatibleSYSTEMTIME structureFormatConverts a CTime object into a formatted string — based on the local time zone.FormatGmtConverts a CTime object into a formatted string — based on UTC.

Operators

operator =Assigns new time values.operator + –Add and subtract CTimeSpan and CTime objects.operator +=, –=Add and subtract a CTimeSpan object to and from thisCTime object.operator ==, < , etc.Compare two absolute times.

Archive/Dump

operator <<Outputs a CTime object to CArchive orCDumpContext.operator >>Inputs a CTime object from CArchive.

CTimeSpan Class Members

Construction

CTimeSpanConstructs CTimeSpan objects in various ways.

Extraction

GetDaysReturns the number of complete days in this CTimeSpan.GetHoursReturns the number of hours in the current day (–23 through 23).GetTotalHoursReturns the total number of complete hours in this CTimeSpan.GetMinutesReturns the number of minutes in the current hour (–59 through 59).GetTotalMinutesReturns the total number of complete minutes in this CTimeSpan.GetSecondsReturns the number of seconds in the current minute (–59 through 59).GetTotalSecondsReturns the total number of complete seconds in this CTimeSpan.

Conversion

FormatConverts a CTimeSpan into a formatted string.

Operators

operator =Assigns new time-span values.operator + –Adds and subtracts CTimeSpan objects.operator += –=Adds and subtracts a CTimeSpan object to and from thisCTimeSpan.operator == < etc.Compares two relative time values.

Archive/Dump

operator <<Outputs a CTimeSpan object to CArchive orCDumpContext.operator >>Inputs a CTimeSpan object from CArchive.将CString转为CTime的常用2种方法:

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);

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);


COleDateTime和COleDateTimeSpan两个类完全可以代替CTime和 CTimeSpan,COleDateTime和COleDateTimeSpan类所使用的成员变量是DATE类型,该类型是个double类型,而且使用的单位是日,所以可以处理从100年1月1日到9999年12月31日的日期时间,COleDateTime类的日期计算主要是操作公有成员变量 COleDateTime::m_dt,该变量是DATE即double类型,该变量是为零时是1899年12月30日0时0分0秒,大于零时的日期比 1899年12月30日0时0分0秒大,小于零比1899年12月30日0时0分0秒小。

COleDateTime Class Members

Construction

COleDateTimeConstructs a COleDateTime object.GetCurrentTimeCreates a COleDateTime object that represents the current time (static member function).

Attributes

GetStatusGets the status (validity) of this COleDateTime object.SetStatusSets the status (validity) of this COleDateTime object.GetAsSystemTimeConverts the time in the COleDateTime object to be represented as aSYSTEMTIME data structure.GetYearReturns the year this COleDateTime object represents.GetMonthReturns the month this COleDateTime object represents (1 – 12).GetDayReturns the day this COleDateTime object represents (1 – 31).GetHourReturns the hour this COleDateTime object represents (0 – 23).GetMinuteReturns the minute this COleDateTime object represents (0 – 59).GetSecondReturns the second this COleDateTime object represents (0 – 59).GetDayOfWeekReturns the day of the week this COleDateTime object represents (Sunday = 1).GetDayOfYearReturns the day of the year this COleDateTime object represents (Jan 1 = 1).
获得一年中的某一天
   COleDateTime datetime;
   datetime=COleDateTime::GetCurrentTime();
   int DayOfYear=datetime.GetDayOfYear();

Operations

SetDateTimeSets the value of this COleDateTime object to the specified date/time value.SetDateSets the value of this COleDateTime object to the specified date-only value.SetTimeSets the value of this COleDateTime object to the specified time-only value.FormatGenerates a formatted string representation of a COleDateTime object.ParseDateTimeReads a date/time value from a string and sets the value of COleDateTime.
从字符串中读取时间。
      COleDateTime datetime;
      datetime.ParseDateTime("12:12:23 27 January 93");

Operators

operator DATEConverts a ColeDateTime value into a DATE.operator DATE*Converts a ColeDateTime value into a DATE*.operator =Copies a COleDateTime value.operator +, -Add and subtract ColeDateTime values.operator +=, -=Add and subtract a ColeDateTime value from this COleDateTime object.operator ==, <, <=, etc.Compare two ColeDateTime values.

Data Members

m_dtContains the underlying DATE for this COleDateTime object.m_statusContains the status of this COleDateTime object.

Archive/Dump

operator <<Outputs a COleDateTime value to Carchive orCDumpContext.operator >>Inputs a ColeDateTime object from CArchive.

COleDateTimeSpan Class Members

Constructor

COleDateTimeSpanConstructs a COleDateTimeSpan object.

Attributes

GetStatusGets the status (validity) of this COleDateTimeSpan object.SetStatusSets the status (validity) of this COleDateTimeSpan object.GetDaysReturns the day portion of the span this COleDateTimeSpan object represents.GetHoursReturns the hour portion of the span this COleDateTimeSpan object represents.GetMinutesReturns the minute portion of the span this COleDateTimeSpan object represents.GetSecondsReturns the second portion of the span this COleDateTimeSpan object represents.GetTotalDaysReturns the number of days this COleDateTimeSpan object represents.GetTotalHoursReturns the number of hours this COleDateTimeSpan object represents.GetTotalMinutesReturns the number of minutes this COleDateTimeSpan object represents.GetTotalSecondsReturns the number of seconds this COleDateTimeSpan object represents.

Operations

SetDateTimeSpanSets the value of this COleDateTimeSpan object.FormatGenerates a formatted string representation of a COleDateTimeSpan object.

Operators

operator doubleConverts this COleDateTimeSpan value to a double.operator =Copies a COleDateTimeSpan value.operator +, -Add, subtract, and change sign for COleDateTimeSpan values.operator +=, -=Add and subtract a COleDateTimeSpan value from thisCOleDateTimeSpan value.operator ==, <, <=Compare two COleDateTimeSpan values.

Data Members

m_spanContains the underlying double for this COleDateTimeSpan object.m_statusContains the status of this COleDateTimeSpan object.

Dump/Archive

operator <<Outputs a COleDateTimeSpan value to CArchive orCDumpContext.operator >>Inputs a COleDateTimeSpan object from CArchive.

将CTime(或COleDateTime,方法一样的。)转换为CString的方法:
CTime   tm = CTime::GetCurrentTime();

//取得CTime中的日期

CString cstrDate = tm.Format("%Y-%m-%d");
//取得CTime中的时间
CString cstrTime = tm.Format("%H:%M-%S");
CString szTime = tm.Format("'%Y-%m-%d %H:%M:%S'");
得到"2015-12-08 09:30:59"的格式,注意字母的大小写。

tm.Format(“%Y-%#m-%#d”);
得到的是2015-12-8格式的字符串时间,可以把月份与日的“0”去掉


COleDateTime转换为CTime:
SYSTEMTIME sys_time;
ole_time.GetAsSystemTime(sys_time);
c_time = CTime(sys_time);


CTime转换为COleDateTime:
SYSTEMTIME sys_time;
c_time.GetAsSystemTime(sys_time);
ole_time = COleDateTime(sys_time);

0 0
原创粉丝点击