On 7 Nov 2006 10:35:37 -0800, "moni" <mo*******@gmai l.comwrote:
>
struct tm *newtime, *oldtime;
time_t result;
time_t long_time;
double elapsed_time;
sscanf(stringto convert, "%d:%d:%d", &hour,&minutes, &seconds);
time( &long_time ); /* Get time as long integer. */
Note that time_t must be _some_ arithmetic type, and is usually an
integer (and in Unix/POSIX is required to be), but it isn't required
to be 'long' specifically, so this name and comment could be wrong.
newtime = localtime( &long_time ); /* Convert to local time. */
oldtime = localtime( &long_time );
Warning: localtime() and also gmtime() returns (if no error) a pointer
that is permitted to be and usually is to 'static' (shared) memory.
Changing any one of these 'values', as you do just below, affects the
others. In your code you don't actually use oldtime for anything so
this error is masked, but if you want to actually use it you need to
allocate an actual struct tm, not just a pointer, and _copy_ to it.
newtime->tm_isdst = 0;
Do you really want to force DST off? Even if the current day is during
a DST/SummerTime period in a zone that uses it? If you are worried
about times on a transition day, you can set tm_isdst to -1 to tell
the library to figure out (from scratch) whether the (modified)
broken-down time you give it is during DST or not. If it is during
both and thus is ambiguous, e.g. 01:30 on the 'fall back' day under US
rules, you will lose and get the -1 return. But from the very limited
requirements statement you have given us so far, it's not at all clear
what if anything is the desired answer in that case.
newtime->tm_hour = hour;
newtime->tm_min = minutes;
newtime->tm_sec = seconds;
//newtime->tm_year = 2006;
result = mktime(&newtime );
This is wrong. You are passing the address of the pointer, not the
value of the pointer = the address of the struct. If you had
#include'd <time.has you must to make your program valid, your
compiler would (must) have detected this mismatch and given you a
'diagnostic' (in usual terms, an error or warning message).
If you do make newtime an actual struct tm, as I discussed above,
_then_ this would be right.
printf("time is %d, %d", result, long_time);
If time_t is indeed long, that is not the correct format specifier for
printf, you should use %ld. If you don't know whether it is or not,
and in portable code you cannot easily know, you can cast to and
format as the largest possible type; in C89 this is long or unsigned
long using %ld or %lu, in C99 [u]intmax_t using %jd or %ju. Fully
conforming C99 implementations are still fairly rare; ones that
implement at least fairly basic/easy features including this are
becoming more common but still not ubiquitous.
Here the result always come s to -1, ie. mktime is always returning -1.
Can you tell me the reason?
See above.
- David.Thompson1 at worldnet.att.ne t