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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Timezone problem with VC++ 6.0?

Status
Not open for further replies.

cpjust

Programmer
Sep 23, 2003
2,132
US
This is strange. I'm wondering if it's a bug in Visual C++ 6.0 or what? BTW, I'm running Windows XP Home SP2 if that matters.
I have the following function:
Code:
time_t timeZoneAdjustment()
{
    time_t epoch( 0 );
    return -(mktime( gmtime( &epoch ) ));
}
If my timezone is anything from negative to GMT, this function returns the proper number of seconds; but if I set my timezone to a positive value it always returns 1. (To be specific, mktime() always returns -1)

Any ideas?
 
> To be specific, mktime() always returns -1
Because there has been an error - my guess is adjusting time to before the epoch.

You're also assuming that time_t can even represent negative values.

How about this?
Code:
int timeZoneAdjustment()
{
  time_t epoch = 86400;   /* exactly 1 day later */
  int offset = mktime( gmtime( &epoch ) );
  offset = offset - 86400;/* take back our day */
  return -offset;
}

My TZ is BST (GMT+1) at the moment, and this prints 3600 running on my Linux machine.

--
 
Thanks, that works. But I still think Visual C++ has a bug. time_t is a typedef for an int, so it can represent negative values. Oh well, as long as this works I guess I'll just do it this way.
 
I think it's not an error, it's a right way to go. The epoch == 0 is a midnight, January 1, 1970. The mktime() converts the local time. Of course, UTC < local time contained in (static) struct tm in that case (1970-01-01T00:00:00+XX:XX), so mktime() returns error code.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top