Time 的练习

TTime 与日期有关的类,与TDateTime相互使用,下面主要说一下常用到的功能

  1. 创建
    1.1 Time time;
    1.2 Time time(TDatetime 对像)
  2. 取得当时日期与时间 time.HomeTime()
  3. 自定义时间
    自定义时间可以通过 TDateTime 对像,请看代码
    TInt year=1996,month=12,day=31;
     TInt error=dateTime.Set(year,TMonth(month-1),day-1,23,59,59,999999);
     User::LeaveIfError(error);
     time=dateTime;
  4. 其中 dateTime 要想得以 time的值可以通过 dateTime = time.DateTime() 方法
  5. 关于日期时间的显示,显示主要通过 格式化参数及保存日期时间的描述符,TTime本身不能显示,但可以通过 FormatL 将显示的放到
    描述符中,显示的日期时间格式通过格式化参数来指定
    例如:
    dateTime = time.DateTime();
     _LIT(KFormat1,"%d %d %d %d:%d:%d.%d\n");
     console->Printf(KFormat1,dateTime.Year(),TInt(dateTime.Month()+1),
      dateTime.Day()+1,dateTime.Hour(),dateTime.Minute(),dateTime.Second(),
      dateTime.MicroSecond());-----_LIT(KFormat11,"%E%D%X%N%Y %1 %2 %3");
     aTime.FormatL(dateString,KFormat11); _LIT(KFormat12,"Full date with date suffix=%S\n");
     console->Printf(KFormat12,&dateString);
  6. 下面说说主要的格式化参数
    % 开始,如果显示一个 % ,则用:%%
    %E  周 文本表示
    %N  月 文本表示
    %X  后缀
    %H  24小时制的时
    %S  秒
    %T  分
    %C  微秒
    %C2,%C3,%C4 分别取微秒的前2、3、4位
    %I  12小时制的时
    %M  月的数字表示
    %D  日的数字表示
    %Y  年的数字表示
    %*  使用简拼/写
    %/0~3   日期处的分隔符
    %:0~3   时间处的分隔符
    %.      .(点)就是一个点分隔符
    %1~5    与本地设置有关的显示日期方式

    关于这个格式化符的更详细描述请看:http://www.cnblogs.com/zziss/archive/2010/01/14/1647800.html
  7. 可以通过 TTimeIntervalMonth等来对 time 进行加减的处理
  8. atime.MonthsFrom(aNewTime)可以得到两个时间的月份的差

下面是我的练习代码,代码中有注释,有详细解释,代码来自 sdk 自身的例子

代码
void printDatatime(TTime);
void showProperties(TTime);
void showFormatting(TTime);
void printDifferences(TTime,TTime);
void WaitForKey()
{
_LIT(KMsgPreAnykey,
"Please any key");
console
->Printf(KMsgPreAnykey);
console
->Getch();}void printDatatime(TTime aTime)
{
TBuf
<40> datetimeString;
_LIT(KFormat2,
"%D%M%Y%/0%1%/1%2%/1%3%/3 %:0%H%:1%T%:2%S.%C%:3");
/*
经测试 %/0 没有起作用
%/1 是分隔符,把这个及%/2换成%/0 则没有分隔符了
关于 %/ 的说明:%/后要加上 0-3 表示4种分隔符

%:表示获取本地设置中的时间分隔符,紧跟一个0~3范围的数字来指定哪一个日期分隔符
*/
aTime.FormatL(datetimeString,KFormat2);
_LIT(KFormat3,
"new date and time:%S \n");
console
->Printf(KFormat3,&datetimeString);
}
void showProperties(TTime aTime)
{
TBuf
<10> nameString;
_LIT(KFormat4,
"%F%N");
aTime.FormatL(nameString,KFormat4); _LIT(KFormat5,
"Days in month (%S) = %d\n");
console
->Printf(KFormat5,&nameString
,aTime.DaysInMonth());

_LIT(KFormat6,
"%E");
aTime.FormatL(nameString,KFormat6); _LIT(KFormat7,
"Day number in year is %d \n");
console
->Printf(KFormat7,aTime.DayNoInYear()); _LIT(KFormat8,"Day number in week=%d (%S) \n");
console
->Printf(KFormat8,aTime.DayNoInWeek(),&nameString); _LIT(KFormat9,"Week number in year=%d\n");
console
->Printf(KFormat9,aTime.WeekNoInYear()); _LIT(KFormat10,"counting fir full week as week 1,\nWeek number in year %d\n");
console
->Printf(KFormat10,aTime.WeekNoInYear(EFirstFullWeek));
}
void showFormatting(TTime aTime)
{
_LIT(KTxt8,
"Formatting the date and time\n");
console
->Printf(KTxt8);
console
->Getch();
TBuf
<40> dateString;
/*
%E = 星期
%D = 日
%X = 后缀
%N = 月的描述
%Y = 年
%1,2,3 = 根据本地的设置显示第1/2/3个内容
*/
_LIT(KFormat11,
"%E%D%X%N%Y %1 %2 %3");
aTime.FormatL(dateString,KFormat11); _LIT(KFormat12,
"Full date with date suffix=%S\n");
console
->Printf(KFormat12,&dateString);
console
->Getch();
/*
这里加 * 表示用简写
*/
_LIT(KFormat13,
"%*E%*D%X%*N%Y %1 %2 %3");
aTime.FormatL(dateString,KFormat13); _LIT(KFormat14,
"Full date with abbreviated names=%S\n");
console
->Printf(KFormat14,&dateString);
console
->Getch();
/*
%D = 日
%M = 月
%Y = 年
%/0 是第一个分隔符,以此类推
*/
_LIT(KFormat15,
"%D%M%Y %/0%1%/1%2%/2%3%/3");
aTime.FormatL(dateString,KFormat15); _LIT(KFormat16,
"dd/mm/yyyy=%S\n");
console
->Printf(KFormat16,&dateString);
console
->Getch();
/*
%:0 = 时间的分隔符
%H = 小时
%T = 分
%S = 秒
%C = 微秒 %*C3 表示取简写微秒的前3位
*/
_LIT(KFormat17,
"%:0%H%:1%T%:2%S.%*C3%:3");
aTime.FormatL(dateString,KFormat17); _LIT(KFormat18,
"Hour,minute,second,microssecond(24 hr) %S\n");
console
->Printf(KFormat18,&dateString);
console
->Getch();
/*
%I = 12 小时制 的小时
*/
_LIT(KFormat19,
"%:0%I%:1%T%:2%S.%*C3%:3%A");
aTime.FormatL(dateString,KFormat19);
_LIT(KFormat20,
"hour,minute,second,microsecond (12 hr) %S\n");
console
->Printf(KFormat20,&dateString);
}
void printDifferences(TTime atime,TTime aNewTime)
{
TBuf
<40> dateString;
TBuf
<40> newDateString; _LIT(KFormat21,"%D%M%Y%/0%1%/1%2%/2%3%/3 %:0%H%:1%T:2%S.%C%:3");
atime.FormatL(dateString,KFormat21); _LIT(KFormat22,
"%D%M%Y%/0%1%/1%2%/2%3%/3 %:0%H%:1%T%:2%S.%C%:3");
aNewTime.FormatL(newDateString,KFormat22); _LIT(KFormat23,
"number of months betweenn%S and \n%S = %d\n");
console
->Printf(KFormat23,&dateString,&newDateString,
atime.MonthsFrom(aNewTime));
}
// Local FunctionsLOCAL_C void MainL(const TDesC& aArgs)
{
//
// add your program code here, example code below
//
//console->Write(_L("Hello, world!\n"));
TTime time;
TDateTime dateTime; time.HomeTime();
_LIT(KTxt1,
"Current date and time in fields(locale in independent):\n");
console
->Printf(KTxt1); dateTime = time.DateTime();
_LIT(KFormat1,
"%d %d %d %d:%d:%d.%d\n");
console
->Printf(KFormat1,dateTime.Year(),TInt(dateTime.Month()+1),
dateTime.Day()
+1,dateTime.Hour(),dateTime.Minute(),dateTime.Second(),
dateTime.MicroSecond()); console
->Getch(); // 自定义时间
TInt year=1996,month=12,day=31;
TInt error
=dateTime.Set(year,TMonth(month-1),day-1,23,59,59,999999);
User::LeaveIfError(error);
time
=dateTime;

console
->ClearScreen();
_LIT(KTxt2,
"Reset date and time ...\n");
console
->Printf(KTxt2);
printDatatime(time);
// 时间的间隔
TTimeIntervalMonths timeIntervalmonths(4);
time
+=timeIntervalmonths;
_LIT(Ktxt3,
"add 4 months to date:\n");
console
->Printf(Ktxt3);
printDatatime(time);
console
->Getch();
// 减少两个月
timeIntervalmonths = -2;
// 或者用 time-= timeIntervalmonths,timeIntervalmonths 为 2
_LIT(Ktxt31,"dec 2 months to date:\n");
time
+=timeIntervalmonths;
console
->Printf(Ktxt31);
printDatatime(time);
console
->Getch();
// 秒的操作 2147483647 是最大值
TTimeIntervalSeconds secInterval(2147483647);
time
+=secInterval;
_LIT(KTxt5,
"add 2147483647 seconds to date is overflow:\n");
console
->Printf(KTxt5);
printDatatime(time);
// 设置时间
console->ClearScreen();
day
=5,month=1,year=1997;
dateTime.SetMonth(TMonth(month
-1));
dateTime.SetDay(day);
dateTime.SetYear(year);
time
= dateTime;
_LIT(KTxt6,
"reset date\n");
console
->Printf(KTxt6);
printDatatime(time);
showProperties(time);
console
->Getch();
console
->ClearScreen();
showFormatting(time);
// 计算日期
TDateTime newDatetime(1996,EJanuary,5,23,59,59,999999);
TTime newTime(newDatetime);
printDifferences(time,newTime); TTimeIntervalSeconds timeIntervalMS(
1);
newTime
+= timeIntervalMS;
printDifferences(time,newTime);
console
->Getch();
newDatetime.SetYear(
1998);
newTime
= newDatetime;
printDifferences(time,newTime);
newTime
+= timeIntervalMS;
printDifferences(time,newTime);
console
->Printf(_L("Command line args: \"%S\"\n"), &aArgs);
}

 



安平2009@原创
qi_jianzhou@126.com

posted @ 2010-01-14 17:37  zziss  阅读(378)  评论(0编辑  收藏  举报