Problem in writing Time in File?

Login to reply to this topic.
Sat, 2007-08-25 07:18
Joined: 2007-06-20
Forum posts: 93

hi,
I m working on S60 2nd Edition FP3.
I write current time in a file, but get some error of TBuf/TDesc. here is my code:- whats wrong here, anybody guide plz............. When I compiled i got error in this line file.write(timeString)

TBuf<30> timeString;
        TTime time;
    time.HomeTime();
               
        _LIT(KDateString6,"%-B%:0%J%:1%T%:3%+B");
        time.FormatL(timeString,KDateString6);
// dateString contains "11:59 pm" - Two component time (hour:minute).
  
        file.Write(timeString);


Thanks & Regards,
C Singh


Sat, 2007-08-25 07:40
Joined: 2007-06-20
Forum posts: 93
Re: Problem in writing Time in File?

hi,

TBuf<30> timeString;
        TTime time;
    time.HomeTime();
              
        _LIT(KDateString6,"%-B%:0%J%:1%T%:3%+B");
        time.FormatL(timeString,KDateString6);
// dateString contains "11:59 pm" - Two component time (hour:minute).
 
TBuf8<10> mystring;
    mystring.Copy(timeString);
    file.Write(mystring);

after using this time is write in file..............
TBuf8<10> mystring;
mystring.Copy(timeString);
file.Write(mystring);


Thanks & Regards,
C Singh

Tue, 2007-08-28 14:21
Joined: 2005-09-16
Forum posts: 141
Re: Problem in writing Time in File?

Hi,

Yes, for reading or writing from file you have to use 8-bit descriptors.

regards,
eswar

Fri, 2007-08-31 14:40
Joined: 2007-06-20
Forum posts: 93
Re: Problem in writing Time in File?

hi eswar,
ya its fine to writimg time in file.I write time in a text file using a simple exe file.
after that i want to convert it into again a TTime object in my another app where i read this string and try to convert it in TTime object. for this i use .Set() method. after this i use some Arithmetical operation but result is not correct .....
whats wrong there ? Any clue ?


Thanks & Regards,
C Singh

Fri, 2007-08-31 15:13
Joined: 2004-11-29
Forum posts: 1155
Re: Problem in writing Time in File?

If you never want to open the text file and read the time stamps yourself, but only will open it with another app, I would suggest writing a binary file instead.

Then you don't have to do the conversion to text string and then parse it again.

Just wrap the TTime in a descriptor and write it to file, and then the same on the other end to read it back.
like this: http://www.newlc.com/forum/how-store-and-retrieve-ttime-and-ttimeintervalseconds-stream

Wed, 2007-09-05 11:17
Joined: 2007-06-20
Forum posts: 93
Re: Problem in writing Time in File?

hi alh,
first I create a Blank Project of exe type, In that i create a file and write Date on that.... ok .....
here is my code for that:-
its works fine.
I am using S60 2nd Edition FP3 and IDE is Code Warrior.

RFs fsSession;
    RFile file;
       
        fsSession.Connect ();
       
        TInt err=file.Open(fsSession,KExpFileName,EFileWrite);<code>
        if (err==KErrNotFound) // file does not exist - create it
                err=file.Create(fsSession,KExpFileName,EFileRead|EFileWrite);
       
        TBuf<30> timeString;
       
        TTime time;
    time.HomeTime();
              
    _LIT(KDateString9,"%Y%D%M%/0%1%/1%2%/2%3%/3");
        time.FormatL(timeString,KDateString9);
    // dateString contains 05/09/2007
         
    TBuf8<30> mystring;
    mystring.Copy(timeString);
    file.Write(mystring);
                  
        file.Close();
        fsSession.Close();

after that I create a Blank Project of App type and write these code for read Date from file and Set that of an Object of TTime and manipulates some arithmetical Operation, here is my code:-
RFs fs;
    RFile file;
    User::LeaveIfError(fs.Connect());
    TInt err = file.Open(fs,KExpFileName,EFileRead ) ;
            if( err == KErrNone)       
            {
                TBuf8<30> timeString;
             file.Read(timeString);
             TBuf<30> mystring;
         mystring.Copy(timeString);
                 
         file.Close();
                   fs.Close();
             TTime iInstallTime;
             TInt err=iInstallTime.Set(mystring); in debug value of err is -2 and i think problem is here to set
             TTime currentTime;
                
                 TTimeIntervalDays iDiff; //Days
                 currentTime.HomeTime();
                  iDiff=currentTime.DaysFrom(iInstallTime);
                  TTimeIntervalDays iMaxDiff=2;
                
                 if(iDiff.operator>(iMaxDiff))
                 {
                     //do something                     
                 }
              }

My problem is that value store in iDiff is not correct so if condition not works properly..
so what i do ...?
In SDK its mention that for Set():-

Set()
TInt Set(const TDesC& aString);


Thanks & Regards,
C Singh

Thu, 2007-09-06 09:28
Joined: 2004-11-29
Forum posts: 1155
Re: Problem in writing Time in File?

If you read the rest of the documentation for TTime::Set, you can also see that it wants a very specific format on the string.

It also says that KErrGeneral(-2) is returned if your string is not formatted right.

Make sure to format it right, or follow my advice to use a binary format.

The TTime::Parse function seem to be a little bit more flexible if you really need it in text format.

If neither of those formats suite you, you need to use TLex and parse the values separatly to build your time object.

I have never used these functions, read about them in the SDK documentation, they are for once quite well documented.

Mon, 2007-09-10 14:32
Joined: 2006-10-05
Forum posts: 80
Re: Problem in writing Time in File?

Hi Chandan,

If you still haven't found a solution to your problem, then here's one:

The string that you have used for formatting your TTime object is _LIT(KDateString9,"%Y%D%M%/0%1%/1%2%/2%3%/3").

This string will format your TTime object depending on the settings of your mobile phone/emulator i.e. it is a locale-dependent formatting.

Hence after formatting, your date will contain the separator that has been set in the phone/emulator, for e.g. if your date is 10th Sept '07 and the date separator is set to '/', the string will contain 09/10/2007

While the "Set" function of TTime class requires a specific format which is clearly mentioned in the SDK doc:

Set()
TInt Set(const TDesC& aString);
Description
Assigns a date and time contained in a descriptor to this TTime. The string consists of up to three components, any or all of which may be omitted: —

year, month and day, followed by a colon

hour, minute and second, followed by a dot

microsecond

When all three components are present, the string should take the form:

YYYYMMDD:HHMMSS.MMMMMM

So before converting the string to a TTime object, you have to first convert it to a string in the above mentioned format.

Mon, 2007-09-10 15:48
Joined: 2007-06-20
Forum posts: 93
Re: Problem in writing Time in File?

hi JP,
Thanks, u r right . Definitely my mistake is that. and one thing that how i change again in required format?


Thanks & Regards,
C Singh

Tue, 2007-09-11 08:26
Joined: 2006-10-05
Forum posts: 80
Re: Problem in writing Time in File?

hi chandan,

Can u plz elaborate what is your required format..
I mean specify the exact process that you want to do step by step, so that accordingly i can reply u.

Tue, 2007-09-11 08:50
Joined: 2007-06-20
Forum posts: 93
Re: Problem in writing Time in File?

ok JP,
I am using S60 2nd Edition FP3 and IDE is Code Warrior.
first i write Date in a file from an exe proj, which is simply created by codewarrior wizard and write this code:-

    RFs fsSession;
    RFile file;
    fsSession.Connect ();
      
    TInt err=file.Open(fsSession,KExpFileName,EFileWrite);<code>
    if (err==KErrNotFound) // file does not exist - create it
                err=file.Create(fsSession,KExpFileName,EFileRead|EFileWrite);
      
    TBuf<30> timeString;
    TTime time;
    time.HomeTime();
             
    _LIT(KDateString9,"%Y%D%M%/0%1%/1%2%/2%3%/3");
    time.FormatL(timeString,KDateString9);
    // dateString contains 05/09/2007
        
    TBuf8<30> mystring;
    mystring.Copy(timeString);
    file.Write(mystring);
                 
    file.Close();
    fsSession.Close();

after run this exe i open that file in system and check format of date, Date is written there is 11/09/2007.

now I manipulate arithmetical operation in another application which i mentioned previously, which is as follows:-

    RFs fs;
    RFile file;
    User::LeaveIfError(fs.Connect());
    TInt err = file.Open(fs,KExpFileName,EFileRead ) ;
            if( err == KErrNone)      
            {
                TBuf8<30> timeString;
                file.Read(timeString);
                TBuf<30> mystring;
                mystring.Copy(timeString);
                
                file.Close();
                fs.Close();
                TTime iInstallTime;
                TInt err=iInstallTime.Set(mystring);//Manipulation is required before this method as we discuss rite?
                TTime currentTime;
       
                TTimeIntervalDays iDiff; //Days
                currentTime.HomeTime();
                iDiff=currentTime.DaysFrom(iInstallTime);
                TTimeIntervalDays iMaxDiff=2;
              
                if(iDiff.operator>(iMaxDiff))
                 {
                     //do something                    
                 }
              }


Thanks & Regards,
C Singh

Tue, 2007-09-11 09:58
Joined: 2006-10-05
Forum posts: 80
Re: Problem in writing Time in File?

Ok fine. Now when you open the file and read the string, it will contain the date as follows:
"11/09/2007"

You need to parse this string, it is simple string manipulation. Just find the '/' character and separate 11, 09 and 2007; this you can easily code yourself.

Now convert them in the specified format of TTime i.e. YYYYMMDD:HHMMSS.MMMMMM and put them in a descriptor; say in your
TBuf<30> mystring.

So your new date string should be something like this "20070810:"
( The month and day values are offset from zero, hence you need to put 8 instead of 9 for month and 10 instead of 11 for day.
You can omit the time part since you only require the date, but the semi colon is necessary. Read the TTime class in SDK doc for details)

Now if you do,
TInt err = iInstallTime.Set(mystring)
you wont get any error.

Hope your problem gets solved now.

Tue, 2007-09-11 11:51
Joined: 2004-11-29
Forum posts: 1155
Re: Problem in writing Time in File?

And again:

If you only want to transfer the information from program A to program B, and don't really need a human to read the file, use binary!

It will be faster, more power efficient, less error prone, less lines of code and use less memory.

Tue, 2007-09-11 13:53
Joined: 2007-06-20
Forum posts: 93
Re: Problem in writing Time in File?

hi alh,
Thanks for suggest me. can u explain a bit more that how i use binary format ?
I don't use before so plz give me hint..


Thanks & Regards,
C Singh

Tue, 2007-09-11 14:06
Joined: 2004-11-29
Forum posts: 1155
Re: Problem in writing Time in File?

I posted a link to it in my first post in this thread.

Here is it again: http://www.newlc.com/forum/how-store-and-retrieve-ttime-and-ttimeintervalseconds-stream

My example uses a writestream, but you can do it the same way with RFile.

You don't have to change anything on how you open the file, you already open it in "binary mode".
It is the default way, and mostly you want to open it in "binary mode" even when reading/writing text to the file.

The difference between opening a file in "binary mode" and "text mode" is generally that "text mode" will give you problems Smiling
It does automatic conversions of line endings and such, that you usually does not really need, but will only make your program behave less predictable...

Wed, 2007-09-12 10:02
Joined: 2007-06-20
Forum posts: 93
Re: Problem in writing Time in File?

Thanx alh and also jp,
I used as instruct by alh and its works fine.


Thanks & Regards,
C Singh

  • Login to reply to this topic.