Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to convert a string containing a date into a date ? 2

Status
Not open for further replies.

manu75

Programmer
Jul 9, 2001
2
FR
Hello!
I am compiling a C-UNIX program on a NT platform.
The "strptime" function exists on UNIX but not on
NT.

So, does someone know how to convert a string
containing a date into a date ?
 
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[]=&quot;05/09/2008 12:30:45&quot;;
struct tm tms;
int ret;

/* extract the pieces from the string */
ret=sscanf(time_str,&quot;%2d/%2d/%4d %2d:%2d:%2d&quot;,
&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(&quot;Conversion failed&quot;);
} 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;

sscanf(time_String, &quot;%d %d %d %d %d %d&quot;, &yr, &mnth, &day,
&hr, &min, &sec);
CTime ctime(yr, mnth, day, hr, min, sec, 0);

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.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
$ cat foo.c
#include <afx.h>

int main(void)
{
return 0;
}

$ gcc foo.c
foo.c:1: afx.h: No such file or directory

$ mv foo.c foo.cc

$ g++ foo.cc
foo.cc:1: afx.h: No such file or directory

What is afx.h?

Russ
bobbitts@hotmail.com
 

Note the question!

<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. &quot;If you're using MSVC++, you can do this...&quot;
Russ
bobbitts@hotmail.com
 
Well, here are some explanations about the compiler and the problem itself ....

I use the &quot;CygWin&quot; compiler on NT and it does not include the &quot;afx.h&quot; and the &quot;strptime&quot; function...

On UNIX I did the following :
...
time_t myDateinTime_tFormat;
char myDateInCharFormat[50];
struct tm temp;
...
...
strptime(myDateInCharFormat,&quot;%m/%d/%Y %T&quot;,&temp);
myDateinTime_tFormat = mktime(&temp);
...
 
Sorry I was not logged in, the previous reply was written by me ....
 

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.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top