newbie time problem (time.h)
Author Message
newbie time problem (time.h)

I'm working on a problem that given a month and a year, calculates if
the seconds since 01/01/1970 is greater than current time since 01/01/
1970. My problem is, when I use the test case given to us in class (8/31/
1999..936143999) my time is off by exactly 5 hours. however when I
change my system time to GMT, my result is the same as 936143999 seconds.
I've looked all over the web trying to find if i need to set a flagto
turn gmt on or something any information would be very much appreciated.
Thank you

#include <stdio.h>
#include <time.h>

#define LEAP(year)  year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)

int main (void)
{
int month = 0;
int year  = 0;
struct tm expiration_time;
time_t expiration_second;
time_t current_second;

printf("Please enter the expiration date,in the format 'month year':
");
scanf("%d %d",&month,&year);

current_second = time(NULL); /* Gets seconds since Jan 01 1970 */
expiration_time.tm_isdst = 0;
expiration_time.tm_year  = year - 1900;
expiration_time.tm_mon = month - 1;

switch (expiration_time.tm_mon)
{
case 1:
expiration_time.tm_mday = LEAP(year) == 1 ? 29 : 28;
break;
case 3:
expiration_time.tm_mday = 30;
break;
case 5:
expiration_time.tm_mday = 30;
break;
case 8:
expiration_time.tm_mday = 30;
break;
case 10:
expiration_time.tm_mday = 30;
break;
default:
expiration_time.tm_mday = 31;
break;

Quote:
}    }

expiration_time.tm_hour = 23;
expiration_time.tm_min  = 59;
expiration_time.tm_sec  = 59;

expiration_second = mktime(&expiration_time);
(difftime(expiration_second,current_second) < 1) ? printf("Expired !\
n") :  printf("Valid !\n");

return 0;

Quote:
}

Tue, 05 Apr 2005 02:12:31 GMT
newbie time problem (time.h)

Quote:
> I'm working on a problem that given a month and a year, calculates if
> the seconds since 01/01/1970 is greater than current time since 01/01/
> 1970. My problem is, when I use the test case given to us in class (8/31/
> 1999..936143999) my time is off by exactly 5 hours. however when I
> change my system time to GMT, my result is the same as 936143999 seconds.
> I've looked all over the web trying to find if i need to set a flagto
> turn gmt on or something any information would be very much appreciated.

Look up 'gmtime()'.

-Mike

Tue, 05 Apr 2005 03:02:09 GMT
newbie time problem (time.h)

Quote:

> #define LEAP(year)  year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)

This is a poor macro definition in that it is insufficiently
defensive.  You should write it this way:
#define LEAP(year)  ((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0))

Quote:
>             expiration_time.tm_mday = LEAP(year) == 1 ? 29 : 28;

and then maybe your leap year check would work out okay.

--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.\
\n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}

Tue, 05 Apr 2005 03:08:13 GMT
newbie time problem (time.h)

Quote:
> I'm working on a problem that given a month and a year, calculates if
> the seconds since 01/01/1970 is greater than current time since 01/01/
> 1970. My problem is, when I use the test case given to us in class
> (8/31/ 1999..936143999) my time is off by exactly 5 hours. however
> when I change my system time to GMT, my result is the same as
> 936143999 seconds. I've looked all over the web trying to find if i
> need to set a flagto turn gmt on or something any information would be
> very much appreciated. Thank you

I can't answer that, but here are some general remarks:

Quote:
> #include <stdio.h>
> #include <time.h>

> #define LEAP(year)  year % 4 == 0 && (year % 100 != 0 || year % 400 ==
> 0)

Warning. Such a macro and parameters should be enclosed in parentheses:

#define LEAP(year) \
((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0))

Quote:
> int main (void)
> {
>     int month = 0;
>     int year  = 0;
>     struct tm expiration_time;
>     time_t expiration_second;
>     time_t current_second;

>     printf("Please enter the expiration date,in the format 'month
>     year':
> ");
>     scanf("%d %d",&month,&year);

You should test that scanf() returns 2.

Quote:
>     current_second = time(NULL); /* Gets seconds since Jan 01 1970 */
>     expiration_time.tm_isdst = 0;
>     expiration_time.tm_year  = year - 1900;
>     expiration_time.tm_mon = month - 1;

>     switch (expiration_time.tm_mon)
>     {
>         case 1:

Rather than those ugly magic numbers, I recommend the use of enumerated:

enum
{
Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec

Quote:
};

Works as it, because it sticks to the month number definition of struct tm
(zero-based).

Quote:
>             expiration_time.tm_mday = LEAP(year) == 1 ? 29 : 28;

I suggest parentheses around the boolean expression.

expiration_time.tm_mday = (LEAP(year) == 1) ? 29 : 28;
or simply

expiration_time.tm_mday = LEAP(year) ? 29 : 28;

(of course, the parentheses has been added into the macros's definition)

Quote:
>             break;
>         case 3:
>             expiration_time.tm_mday = 30;
>             break;
>         case 5:
>             expiration_time.tm_mday = 30;
>             break;
>         case 8:
>             expiration_time.tm_mday = 30;
>             break;
>         case 10:
>             expiration_time.tm_mday = 30;
>             break;
>         default:
>             expiration_time.tm_mday = 31;
>             break;
> }    }

extra curly brace.

The whole thing can be rewritten:

switch (expiration_time.tm_mon)
{
case Feb:
expiration_time.tm_mday = LEAP(year) ? 29 : 28;
break;
case Apr:
case Jun:
case Sep:
case Nov:
expiration_time.tm_mday = 30;
break;
default:
expiration_time.tm_mday = 31;
}

Quote:
>     expiration_time.tm_hour = 23;
>     expiration_time.tm_min  = 59;
>     expiration_time.tm_sec  = 59;

>     expiration_second = mktime(&expiration_time);
>     (difftime(expiration_second,current_second) < 1) ? printf("Expired
>     !\
> n") :  printf("Valid !\n");

Avoid long lines. It hurts...

Quote:

>     return 0;
> }

--
-ed- emdel at noos.fr ~]=[o
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
C-library: http://www.dinkumware.com/htm_cl/index.html
"Mal nommer les choses c'est ajouter du malheur au monde."
-- Albert Camus.

Tue, 05 Apr 2005 04:21:24 GMT
newbie time problem (time.h)

Quote:
> I'm working on a problem that given a month and a year, calculates if
> the seconds since 01/01/1970 is greater than current time since 01/01/
> 1970. My problem is, when I use the test case given to us in class (8/31/
> 1999..936143999) my time is off by exactly 5 hours. however when I
> change my system time to GMT, my result is the same as 936143999 seconds.
> I've looked all over the web trying to find if i need to set a flagto
> turn gmt on or something any information would be very much appreciated.
> Thank you

mktime() assumes the struct tm is in local time, and returns
time_t in UTC (that is, zone 0 standard time, formerly GMT).

Going the other way, you can choose between gmtime()
and localtime(), but Standard C has no mkgmtime().

Either set your timezone to GMT or equivalent --
for many systems this can be done for a particular
user or process rather than the whole system, and
on POSIX/Unix systems (at least) it can be done
within the program with tzset().

Or just do the computation yourself; it's not that complicated,
as long as you don't handle timezones (and leap seconds).
But you do have to assume a representation for time_t; see below.

...

Quote:
>     current_second = time(NULL); /* Gets seconds since Jan 01 1970 */

Standard C does not require that time_t (the return of time())
have that representation, although POSIX/Unix does and
many (nearly all?) other systems use it for compatibility.

Quote:
>     expiration_time.tm_isdst = 0;

Do you really want to use standard time all year?  (There are
some places this is correct, like Arizona.)  If you want to use
current local time whether daylight or not, set tm_isdst = -1
to mean that the library should make its own determination.

Quote:
>     switch (expiration_time.tm_mon)
>     {
...
>         case 3:
>             expiration_time.tm_mday = 30;
>             break;
>         case 5:
>             expiration_time.tm_mday = 30;
>             break;
>         case 8:
>             expiration_time.tm_mday = 30;
>             break;
>         case 10:
>             expiration_time.tm_mday = 30;
>             break;

If you want you can put multiple case labels on one code block:
case 3: case 5: case 8: case 10: x.tm_mday = 30; break;

Quote:
>     (difftime(expiration_second,current_second) < 1) ...

This corrects the flaw above; difftime() will return the difference
in seconds, even if time_t is not seconds-since-1970-01-01.

--
- David.Thompson 1 now at worldnet.att.net

Fri, 08 Apr 2005 10:09:21 GMT

 Page 1 of 1 [ 5 post ]

Relevant Pages