Symbian
Symbian OS Library

FAQ-0442 How should I externalize/internalize a TDateTime?

[Index][spacer] [Previous] [Next]



 

Classification: C++ Category: Streams & Stores
Created: 11/30/99 Modified: 09/11/2002
Number: FAQ-0442
Platform: Not Applicable

Question:
The TDateTime class is not store aware so does not support the >> and << operators. What is the best strategy to externalize/Internalize it?

Answer:
Convert it to a TTime, then to a TInt64 which can be externalized. For internalizing do the reverse.The following example code illustrates both:

_LIT(KTestExternalizeDateTime, "C:\\Documents\\DateTimeTest");
RFs fsSession;
User::LeaveIfError(fsSession.Connect());
CFileStore* store = CDirectFileStore::ReplaceLC(fsSession, KTestExternalizeDateTime, EFileWrite);
store->SetTypeL(KDirectFileStoreLayoutUid);
RStoreWriteStream writeStream;
TStreamId id = writeStream.CreateLC(*store);

TDateTime myDateTime(1999, ENovember, 29, 11, 04, 00, 00);
TTime myTime(myDateTime);
writeStream << myTime.Int64();
writeStream.CommitL();
CleanupStack::PopAndDestroy(); // writeStream
store->SetRootL(id);  store->CommitL();
CleanupStack::PopAndDestroy(); // store

store = CDirectFileStore::OpenLC(fsSession, KTestExternalizeDateTime, EFileRead);
RStoreReadStream readStream;
readStream.OpenLC(*store, store->Root());

TInt64 myInt64RepresentingDateTime;
readStream >> myInt64RepresentingDateTime;
CleanupStack::PopAndDestroy(2); //store + stream
fsSession.Close();
TTime myTimeFromInt64(myInt64RepresentingDateTime); 

TDateTime myRestoredDateTime = myTimeFromInt64.DateTime();