2009-11-10 99 views
0

代码:转换时间纪元时间格式

TBuf<50> TimeDesc; 
TBuf <50> singleLog; 
TTime time = event.Time(); 
_LIT(KTimeFormat,"%I%:1%T%:1%S %B"); 
RTz tzServer; 
User::LeaveIfError(tzServer.Connect()); 
CleanupClosePushL(tzServer); 
CTzConverter* tzConverter = CTzConverter::NewL(tzServer); 
CleanupStack::PushL(tzConverter); 
tzConverter->ConvertToLocalTime(time); 
time.FormatL(TimeDesc,KTimeFormat); 
singleLog.Append(TimeDesc); 
singleLog.Append('|'); 

如何这次划时代的时间格式转换?

回答

0

我发现这个函数,它接受一个Unix纪元时间戳,并返回一个TTime

// This function converts a Unix Epoch timestamp to a TTime 
TTime UnixToEpocTimeL(TUint32 aTimestamp) 
{ 

// define the start of the Unix Epoch as beginning of Jan 1, 1970 
_LIT(KUnixEpoch, "19700000:"); 

// Create a new time variable, and give it the starting value of Jan 1, 1970 
TTime time; 
User::LeaveIfError(time.Set(KUnixEpoch)); 

// The timestamp is the number of seconds since Jan 1, 1970 
// Add the number of seconds in the timestamp to start date. 
TTimeIntervalSeconds secs(aTimeStamp); 
time += secs; 

// the variable 'time' now contains the requested datetime 
return time; 
} 

http://discussion.forum.nokia.com/forum/showthread.php?t=110494


更新:我不知道很多关于这个(我也不具有任何这里测试它的方式!),但我试图添加详细的评论,解释我认为它的工作原理。你可以添加类似于你的代码的东西,甚至可以添加一个函数并直接调用它。

+0

谢谢 给出错误 未定义的标识符“KUnixEpoch” 未定义的标识符“KUnixEpoch” 我应该包括某种形式的头文件还是什么? – sonia 2009-11-10 13:13:10

+0

恐怕你在这方面超出了我的知识 - 这个例子是一个剪切和粘贴的工作。我以为'_LIT(KUnixEpoch,“19700000:”);'定义了'KUnixEpoch'。我会尝试添加一些评论,说明我认为它在做什么 – 2009-11-10 17:10:23