What is the format of the string containing the date? And what format do you want to convert it to? Rob
"Programming is like art...It makes me feel like chopping my ear off."
Here's something that will hopefully help. If the format of your original string differs, you'll just need to adjust the sscanf() function part.
#include <stdio.h>
#include <time.h>
int main(void)
{
char time_str[]="05/09/2008 12:30:45";
struct tm tms;
int ret;
/* extract the pieces from the string */
ret=sscanf(time_str,"%2d/%2d/%4d %2d:%2d:%2d",
&tms.tm_mon,
&tms.tm_mday,
&tms.tm_year,
&tms.tm_hour,
&tms.tm_min,
&tms.tm_sec);
if (ret!=6) {
/* If sscanf() didn't convert exactly 6 items,
* there was something wrong with the string */
puts("Conversion failed"
} else {
/* string was parsed correctly, make some
* final adjustments */
struct tm *final;
time_t tmp;
tms.tm_mon--; /* january is 0 */
tms.tm_year-=1900; /* tm_year is the # of years since 1900 */
/* mktime() will set tms.tm_wday for us
* even though it wasn't in our string */
tmp=mktime(&tms);
/* Get the time into a time_t with the correct weekday */
final=localtime(&tmp);
/* display in a common format, you
* you could also use sprintf() here
* instead of calling localtime()
* above for finer control over how
* the resulting string ends up */
puts(asctime(final));
}
return 0;
}
Russ
bobbitts@hotmail.com
You can use CTime class for this.
It uses ANSI time_t data type to store time values.
There are different constructors of CTime.
The one you could use is
CTime(int year,int month,int day,int hour,int min,
int sec, inst DST=1);
DST stands for Daylight Saving time which is
computed by default (DST=1). You can set this to zero
.
Hence assuming your string consists of
year/month/day/hour/min/sec in that order.
#include <afx.h>
int yr;
int mnth;
int day;
int hr;
int min;
int sec;
You can use GetTime() to convert this to
a time_t which can be used with functions like
localtime() etc.
Anand
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Mail me at abpillai@excite.com if you want additional help. I dont promise that I can solve your problem, but I will try my best.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<I am compiling a C-UNIX program on a NT platform>
He is compiling on NT most probably with VC++.
afx.h is in $MSVC/mfc/include
$MSVC = installation directory of VC.
This could be a question for Microsfot C++, not
this forum. hence confusion if any.
Anand
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Mail me at abpillai@excite.com if you want additional help. I dont promise that I can solve your problem, but I will try my best.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
I did note the question. The fact that he's on an NT platform only indicates a *possibility* that he's using MSVC++. There are many other compilers for Win NT and other Windows platforms. I, for instance, don't use MSVC++ *at all* for compiling C programs on windows platforms. If I were the one asking the question, your example would be meaningless to me.
I'm not necessarily objecting to the fact that you're posting a compiler-specific answer (although the problem can be solved easily in a non compiler-specific way), more that you're posting a compiler-specific answer without any mention of this fact. i.e. "If you're using MSVC++, you can do this..."
Russ
bobbitts@hotmail.com
Well, here are some explanations about the compiler and the problem itself ....
I use the "CygWin" compiler on NT and it does not include the "afx.h" and the "strptime" function...
On UNIX I did the following :
...
time_t myDateinTime_tFormat;
char myDateInCharFormat[50];
struct tm temp;
...
...
strptime(myDateInCharFormat,"%m/%d/%Y %T",&temp);
myDateinTime_tFormat = mktime(&temp);
...
Sorry for the *nonportable* answer. I guess I am a
microserf, hence my logic invariably tends to be like
that.
The best portable solution is already
posted by Russ.
Anand
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Mail me at abpillai@excite.com if you want additional help. I dont promise that I can solve your problem, but I will try my best.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Sorry for the *nonportable* answer. I guess I am a
microserf :-D, hence my logic invariably tends to be like
that.
The best portable solution is already
posted by Russ.
Anand
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Mail me at abpillai@excite.com if you want additional help. I dont promise that I can solve your problem, but I will try my best.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.