Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   difference between date & time (http://www.velocityreviews.com/forums/t533777-difference-between-date-and-time.html)

alcool 08-30-2007 04:33 PM

difference between date & time
 
hi,
I have 2 date/time values
i.e. the system date/time and (h:m dd:mm:yyyy). I would like know to
find a routine that calculate this difference. Maybe using the struct
time_t and difftime.

how I can do?

Please help me.


Eric Sosman 08-30-2007 07:53 PM

Re: difference between date & time
 
alcool wrote On 08/30/07 12:33,:
> hi,
> I have 2 date/time values
> i.e. the system date/time and (h:m dd:mm:yyyy). I would like know to
> find a routine that calculate this difference. Maybe using the struct
> time_t and difftime.


You are probably thinking about struct tm; time_t is
not a struct. Store the values for one time in a struct tm
object[*] and use mktime() to convert them to a time_t. Then
do the same with the other, getting another time_t. Then
use difftime() to find the number of seconds between the two
times.
[*] Remember to make appropriate adjustments when storing
the values! Store the number of years since 1900, yyyy - 1900
rather than yyyy itself. Remember that the months are encoded
with January == 0, not 1. Also, you should probably set the
tm_isdst field to indicate whether your times are expressed in
daylight time or in standard time for your local time zone (or
to -1 to say "I don't know: try to figure it out.")

--
Eric.Sosman@sun.com

alcool 08-31-2007 07:48 AM

Re: difference between date & time
 

> You are probably thinking about struct tm; time_t is
> not a struct. Store the values for one time in a struct tm
> object[*] and use mktime() to convert them to a time_t. Then
> do the same with the other, getting another time_t. Then
> use difftime() to find the number of seconds between the two
> times.
>

I have try:
------------
void cfrDate(char h[], char min[], char y[],char mo[], char d[]){
struct tm *newtime, *oldtime;

time_t result;
time_t long_time;
double elapsed_time;


time( &long_time );

newtime = localtime( &long_time );
oldtime = localtime( &long_time );


newtime->tm_hour = (int)h;
newtime->tm_min = (int)min;
newtime->tm_year = (int)y - 1900;
newtime->tm_mon = (int)mo - 1;
newtime->tm_mday = (int)d;

result = mktime(newtime);

if ( (result = mktime(newtime)) == (time_t)-1){
fprintf(stderr, "Bad mktime\n");
return EXIT_FAILURE;
}else{
elapsed_time = difftime( result, long_time );
printf("time is %d, %d", result, long_time);
printf("time elapsed is %d", elapsed_time);
}
(...)
------


but I obtain only "Bad mktime"

why?


Keith Thompson 08-31-2007 10:06 AM

Re: difference between date & time
 
alcool <fabio.maresca@gmail.com> writes:
>> You are probably thinking about struct tm; time_t is
>> not a struct. Store the values for one time in a struct tm
>> object[*] and use mktime() to convert them to a time_t. Then
>> do the same with the other, getting another time_t. Then
>> use difftime() to find the number of seconds between the two
>> times.
>>

> I have try:
> ------------
> void cfrDate(char h[], char min[], char y[],char mo[], char d[]){


A parameter declared as an array is really a pointer, so your
parameters are effectively:

char *h, char *min, char *y, char *mo, char *d

I don't know why you'd want them to be either arrays or pointers.

I suspect you really want all the parameters to be of type int. Using
char because they're within a relatively short range isn't likely to
save you anything -- and the year number isn't likely to fit in a
single byte.

> struct tm *newtime, *oldtime;
>
> time_t result;
> time_t long_time;
> double elapsed_time;
>
>
> time( &long_time );
>
> newtime = localtime( &long_time );
> oldtime = localtime( &long_time );
>
>
> newtime->tm_hour = (int)h;


This cast, like almost all casts, is suspicious.

You're converting a pointer value to type int, which makes no sense in
this context. If you had declared 'h' as int you could have written

newtime->tm_hour = h;

> newtime->tm_min = (int)min;
> newtime->tm_year = (int)y - 1900;
> newtime->tm_mon = (int)mo - 1;
> newtime->tm_mday = (int)d;


See above.

> result = mktime(newtime);
>
> if ( (result = mktime(newtime)) == (time_t)-1){
> fprintf(stderr, "Bad mktime\n");
> return EXIT_FAILURE;
> }else{
> elapsed_time = difftime( result, long_time );
> printf("time is %d, %d", result, long_time);


"%d" is the format for type int; your arguments are of type time_t.
And you're missing a new-line. I suggest:

printf("time is %ld, %ld\n", (long)result, (long)long_time);

There's no guarantee that a time_t value will fit in a long, but it's
likely to work. If it doesn't, use a different type and adjust the
format string appropriately.

> printf("time elapsed is %d", elapsed_time);


elapsed_time is of type double. Use "%g" or "%f".

> }
> (...)
> ------
>
>
> but I obtain only "Bad mktime"
>
> why?


See above. There may be other errors in your code. Fix the ones I've
described, and try again.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

John Gordon 08-31-2007 03:10 PM

Re: difference between date & time
 
In <1188546533.444384.49170@r23g2000prd.googlegroups. com> alcool <fabio.maresca@gmail.com> writes:


> void cfrDate(char h[], char min[], char y[],char mo[], char d[]){
> struct tm *newtime, *oldtime;


> newtime->tm_hour = (int)h;


You can't convert from a string to an int this way. Instead, use atoi().

newtime->tm_hour = atoi(h);

--
John Gordon A is for Amy, who fell down the stairs
gordon@panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"


David Thompson 09-16-2007 09:45 PM

Re: difference between date & time
 
On Fri, 31 Aug 2007 07:48:53 -0000, alcool <fabio.maresca@gmail.com>
wrote:

>
> > You are probably thinking about struct tm; time_t is
> > not a struct. Store the values for one time in a struct tm
> > object[*] and use mktime() to convert them to a time_t. Then
> > do the same with the other, getting another time_t. Then
> > use difftime() to find the number of seconds between the two
> > times.
> >

> I have try:
> ------------
> void cfrDate(char h[], char min[], char y[],char mo[], char d[]){
> struct tm *newtime, *oldtime;
>
> time_t result;
> time_t long_time;
> double elapsed_time;
>

Aside: The name long_time could be misleading, since time_t isn't
necessarily /*signed*/ long, or even unsigned long, although those are
common. It also isn't a good variable name as it says nothing whatever
about the _purpose_ for which the variable is used. In this routine I
would probably call it current_time or time_now or just now. result is
a little better, but still not very specific; I would probably call it
other_time or time_then or then. The choice of names is important to
the human(s) reading your code -- possibly including yourself, some
time in the future -- but make no difference to the compiler, as long
as they are legal and unique (which these are).

>
> time( &long_time );
>
> newtime = localtime( &long_time );
> oldtime = localtime( &long_time );
>

Note that localtime() (or gmtime())) returns a pointer to static
memory, not a unique object or value. If after this you used both
oldtime->foo and newtime->bar you would have problems. You don't in
fact do that, so this time you're OK, but skating on thin ice.

>
> newtime->tm_hour = (int)h;
> newtime->tm_min = (int)min;
> newtime->tm_year = (int)y - 1900;
> newtime->tm_mon = (int)mo - 1;
> newtime->tm_mday = (int)d;
>

As already noted these are your real problem. Also remember Eric's
advice to set tm_isdst in the struct tm you will pass to mktime().

> result = mktime(newtime);
>
> if ( (result = mktime(newtime)) == (time_t)-1){


There's no reason to call mktime() twice with the same argument. Or
more precisely, with pointers to the same* data, whether or not
located in the same place. (* They won't actually be identical if the
first call normalized it, but then they will be equivalent.)

result = mktime(newtime);
if( result == (time_t)-1 ) ...
is one common style, and
if( (result = mktime(newtime)) == (time_t)-1 ) ...
is another common style.
Both are legal. Some people prefer one or the other across the board,
and some choose depending on the particular situation. At the level of
learning you display in your post, you might want to just stick with
one for now, while you focus on more important points.

- formerly david.thompson1 || achar(64) || worldnet.att.net


All times are GMT. The time now is 01:22 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.