Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 198
  • Last Modified:

Time and Date in a CString or char array

I have to write a time and date to a ini file and I've come up with this but there are some problems and i'm shure that is something easy but I can't get it rite:

[Source]
CTime t;
t.GetCurrentTime();
CString s = t.Format( "%A, %B %d, %Y" );
sprintf(buf, "%s", s);
WritePrivateProfileString("Time", "Time", buf, "Presets\\" + name + ".ini");

[makes]
Time=Thursday, January 15, 1970

Can anyone tel me why I do not get the write time and date?

I've not mutch point but this IS imptant.

many greets and respect to who can help me

Captnoord

0
captnoord
Asked:
captnoord
  • 12
  • 5
  • 5
1 Solution
 
Mayank SAssociate Director - Product EngineeringCommented:
/* Try this: */


#include <iostream.h>
#include <time.h>

int main ()
{
  time_t t ;

  time ( &t ) ;
  cout << ctime ( &t ) ;

  return 0 ;

} // end of main ()


/* Mayank */
0
 
fl0ydCommented:
What exactly is going wrong? I do have to make a suggestion apart from the actual error. You can avoid the sprintf function and the temporary buffer by writing this instead:

CTime t;
t.GetCurrentTime();
CString s = t.Format( "%A, %B %d, %Y" );
WritePrivateProfileString("Time", "Time", (LPCTSTR)s, "Presets\\" + name + ".ini");

.f
0
 
captnoordAuthor Commented:
the acual problem is that it will write "Time=Thursday, January 15, 1970" and it have to write "Thursday, march 27, 2003" got it

and every time it's displaying a other date and that's a little strange



0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
captnoordAuthor Commented:
the acual problem is that it will write "Time=Thursday, January 15, 1970" and it have to write "Thursday, march 27, 2003" got it

and every time it's displaying a other date and that's a little strange



0
 
captnoordAuthor Commented:
by the way i'm working on a NT pc and i have to work on it
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Did you try my code??

Mayank.
0
 
captnoordAuthor Commented:
by the way i'm working on a NT pc and i have to work on it
0
 
captnoordAuthor Commented:
No why i'm working in visual c++
0
 
captnoordAuthor Commented:
No why i'm working in visual c++
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Was that a reply to me?
0
 
captnoordAuthor Commented:
Yes mayankeagle I'm just started in visual c++ but I don't think way is the shortest c.q. best I don't know
greets captnoord
0
 
captnoordAuthor Commented:
eeeuuu by the way mayankeagle you rock becouse it's working now thanks a lot a couple little changes and it's good
grtees and respect
Captnoord
0
 
fl0ydCommented:
The problem seems to be, that you are retrieving the current time but aren't using it. The correct way to use GetCurrentTime() is this:

CTime t = CTime::GetCurrentTime();

With this it should be working now. As a side note, The date "Thursday, January 15, 1970" is where timeline starts. Whenever you get this date, check if you have actually assigned it a value.

@ mayankeagle:
You are missing the point, that this is to be stored in an .ini file. While your implementation works for plain text files, it doesn't take into consideration the sections in .ini-file, nor will it prevent duplicate keys.

.f
0
 
Mayank SAssociate Director - Product EngineeringCommented:
I was just about to unsubscribe! Thanks for telling. Anyways, do I get the points?

Mayank.
0
 
fl0ydCommented:
Take a look at my previous post and see for yourself if your 'solution' qualifies as such.

.f
0
 
captnoordAuthor Commented:
eeeuuu by the way mayankeagle you rock becouse it's working now thanks a lot a couple little changes and it's good
grtees and respect
Captnoord
0
 
Mayank SAssociate Director - Product EngineeringCommented:
>> Take a look at my previous post and see for yourself if your 'solution' qualifies as such.

I agree with your solution but if he's got mine working then we better not argue about it.

Mayank.
0
 
captnoordAuthor Commented:
it's both useble becouse when you'r modifingen it this way
[Source]
CString s;
time_t t ;
time (&t) ;
s = ctime(&t);
WritePrivateProfileString("Time", "Time last mod", (LPCTSTR)s, "Presets\\" + name + ".ini");

it does work

but I will try your way to fl0yd but I've to add the time function to it

greets
Captnoord

About the points, the best method wil get the points.
0
 
fl0ydCommented:
The problem is, that he believes for now that it does what he wants, until he comes back with a question "How do I create sections in .ini files?" followed by the one "How do I avoid duplicate keys in my .ini files?" followed by "How do I make sure to not have duplicate sections in my .ini files". A lot of bugs are easily overseen and will only eventually pop up.

.f
0
 
captnoordAuthor Commented:
[source1]
{
#include "time.h"
..

time_t t ;
time (&t) ;
s = ctime(&t)
WritePrivateProfileString("Time", "Time Created", (LPCTSTR)s, "Presets\\" + name + ".ini");
}

[source2]
{
CString s;
CTime t = CTime::GetCurrentTime();
s = t.Format("%A, %B, %d, %Y, %H:%M:%S");
WritePrivateProfileString("Time", "Time Created", (LPCTSTR)s, "Presets\\" + name + ".ini");
}

Ok both of the sources are working and are easely implemented in a program. There's no problem at all but I've to chose between the 2 sources for my program I chose the 2e source becouse he's shorter and uses an object but to end this question and anwser war I DONT CHOSE I Will divide the point (If Possible) and be very gradefull for the both of you.
many many many thanks for anwsering the question

Greets and mutch respect
Captnoord



0
 
captnoordAuthor Commented:
[source1]
{
#include "time.h"
..

time_t t ;
time (&t) ;
s = ctime(&t)
WritePrivateProfileString("Time", "Time Created", (LPCTSTR)s, "Presets\\" + name + ".ini");
}

[source2]
{
CString s;
CTime t = CTime::GetCurrentTime();
s = t.Format("%A, %B, %d, %Y, %H:%M:%S");
WritePrivateProfileString("Time", "Time Created", (LPCTSTR)s, "Presets\\" + name + ".ini");
}

Ok both of the sources are working and are easely implemented in a program. There's no problem at all but I've to chose between the 2 sources for my program I chose the 2e source becouse he's shorter and uses an object but to end this question and anwser war I DONT CHOSE I Will divide the point (If Possible) and be very gradefull for the both of you.
many many many thanks for anwsering the question

Greets and mutch respect
Captnoord
0
 
fl0ydCommented:
Ok, to make your decision a bit easier: ctime is a c-function which might induce compatibility issues in the future when compiling c++ code. Though I do not like MFC a lot it will give you potentially less trouble in the future.

.f
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 12
  • 5
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now