?
Solved

Time and Date in a CString or char array

Posted on 2003-03-27
22
Medium Priority
?
197 Views
Last Modified: 2010-04-01
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
Comment
Question by:captnoord
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 12
  • 5
  • 5
22 Comments
 
LVL 30

Expert Comment

by:Mayank S
ID: 8217053
/* 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
 
LVL 8

Expert Comment

by:fl0yd
ID: 8217197
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
 

Author Comment

by:captnoord
ID: 8217232
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:captnoord
ID: 8217243
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
 

Author Comment

by:captnoord
ID: 8217269
by the way i'm working on a NT pc and i have to work on it
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8217277
Did you try my code??

Mayank.
0
 

Author Comment

by:captnoord
ID: 8217296
by the way i'm working on a NT pc and i have to work on it
0
 

Author Comment

by:captnoord
ID: 8217303
No why i'm working in visual c++
0
 

Author Comment

by:captnoord
ID: 8217307
No why i'm working in visual c++
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8217321
Was that a reply to me?
0
 

Author Comment

by:captnoord
ID: 8217334
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
 

Author Comment

by:captnoord
ID: 8217346
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
 
LVL 8

Expert Comment

by:fl0yd
ID: 8217353
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
 
LVL 30

Expert Comment

by:Mayank S
ID: 8217360
I was just about to unsubscribe! Thanks for telling. Anyways, do I get the points?

Mayank.
0
 
LVL 8

Expert Comment

by:fl0yd
ID: 8217367
Take a look at my previous post and see for yourself if your 'solution' qualifies as such.

.f
0
 

Author Comment

by:captnoord
ID: 8217370
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
 
LVL 30

Expert Comment

by:Mayank S
ID: 8217428
>> 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
 

Author Comment

by:captnoord
ID: 8217435
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
 
LVL 8

Accepted Solution

by:
fl0yd earned 100 total points
ID: 8217447
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
 

Author Comment

by:captnoord
ID: 8217536
[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
 

Author Comment

by:captnoord
ID: 8217545
[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
 
LVL 8

Expert Comment

by:fl0yd
ID: 8217623
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question