调用方式:
TBuf<128> bSour;
TBuf<128> bDesc;
bSour.Append(_L("2011-05-22T21:50:15+08:00"));
ConvData(bSour, bDesc);
iEikonEnv->InfoWinL(bDesc, KNullDesC);
//传入一个时间,然后计算出时间差,用分钟表示,大于60分钟就用小时表示,大于24小时,就用天表示,大于24天,就用月表示
//aSource :传入格式:2011-05-18T20:54:15+08:00
//aDesc :传出数据,如果为空,表示传入的时间大于当前时间
void CWorldView::ConvData(const TDesC & aSource, TDes & aDesc)
{
//如果小于18位,也就是传过来的时间字符串有误
if (18 > aSource.Length())
return;
TInt nYear;
TInt nMonth;
TInt nDay;
TLex iLex;
//年
iLex = aSource.Mid(0, 4);
iLex.Val(nYear);
//月
iLex = aSource.Mid(5, 2);
iLex.Val(nMonth);
//日
iLex = aSource.Mid(8, 2);
iLex.Val(nDay);
TInt nHour = 0;
TInt nMinute = 0;
//计算小时
iLex = aSource.Mid(11, 2);
iLex.Val(nHour);
//计算分钟
iLex = aSource.Mid(14, 2);
iLex.Val(nMinute);
TDateTime time;
time.SetYear(nYear);
time.SetMonth(TMonth(EJanuary + nMonth - 1));
time.SetDay(nDay - 1);
time.SetHour(nHour);
time.SetMinute(nMinute);
TTime tCur;
tCur.HomeTime();
TTime tFirst;
tFirst = time;
//计算月数
TInt nValue = tCur.MonthsFrom(tFirst).Int();
//时间错误
if (0 > nValue)
{
return;
}
//月份大于大当前月份
if (0 < nValue)
{
aDesc.AppendNum(nValue);
aDesc.Append(_L(" month"));
return;
}
//计算天数
nValue = tCur.DaysFrom(tFirst).Int();
//天数大于当前天数
if (0 > nValue)
{
return;
}
//够一天
if (0 < nValue)
{
aDesc.AppendNum(nValue);
aDesc.Append(_L(" day"));
return;
}
//小时
TTimeIntervalHours hours;
tCur.HoursFrom(tFirst, hours);
nValue = hours.Int();
//时间错误
if (0 > nValue)
{
return;
}
//够一个小时
if (0 < nValue)
{
aDesc.AppendNum(nValue);
aDesc.Append(_L(" hours"));
return;
}
//分钟
TTimeIntervalMinutes minutes;
tCur.MinutesFrom(tFirst, minutes);
nValue = minutes.Int();
//分钟错误
if (0 > nValue)
{
return;
}
aDesc.AppendNum(nValue);
aDesc.Append(_L(" minutes"));
return;
}