Operating System - HP-UX
1834153 Members
2229 Online
110064 Solutions
New Discussion

Re: calculating timezone difference on HP-UX

 
SOLVED
Go to solution
Nandibhatla Ravi
Occasional Advisor

calculating timezone difference on HP-UX

Hi,

In my program I need to find out the difference in time zones from UTC to local time zone.

For this I use the following code:
==================================
// get the system time
time((time_t*)&long_time);

// Get the GM time
ptime = gmtime(&long_time);
ptime->tm_isdst= -1 ;
long_timegm = mktime(ptime);

// get the local time
ptime = localtime(&long_time);
ptime->tm_isdst= -1 ;
long_timelocal = mktime(ptime);

// calculate the difference in timezones in terms of hours
timezone = long_timelocal - long_timegm;
m_timezone = (float)timezone/3600;

printf("Timezone off set in hours is : %f\n",m_timezone);

=========================================

The above programs behaviour is different with different values of TZ environment variable.

When TZ is set to IST (which is GMT+5:30), the timezone printed with above program is ZERO. Which is incorrect.

When the TZ is set to GMT+5:30, the value printed is -5.5 which is correct (IST = GMT + 5: 30)

When TZ is set to GMT, the value printed is again ZERO.

I am unable to understand the behaviour of the program. Can someone explain me whats wrong with this code ?

Thanks in advance,
Ravi Nandibhatla.
9 REPLIES 9
Dennis Handly
Acclaimed Contributor
Solution

Re: calculating timezone difference on HP-UX

There is NO IST timezone. The correct name is: IST-5:30
Nandibhatla Ravi
Occasional Advisor

Re: calculating timezone difference on HP-UX

Hi Dennis,

Thank you for the reply.

Could you please tell me whether the logic I am using to calculate the time difference between UTC to local time is correct or not ?

Thanks,
Ravi Nandibhatla.
Dennis Handly
Acclaimed Contributor

Re: calculating timezone difference on HP-UX

>Could you please tell me whether the logic I am using to calculate the time difference between UTC to local time is correct or not?

Well mktime(3) says it takes the local time. So you are misusing mktime and may cause problems during the DST transitions.

Instead of doing all of that coding, you can simplify it to:
tzset();
printf("the value of timezone is: %f\n", (double)timezone/3600);
Nandibhatla Ravi
Occasional Advisor

Re: calculating timezone difference on HP-UX

Hi Dennis,

I could not follow your response. How did you get the timezone value so that we can divide it by 3600 ?

Also the man page indicates that tzset() will be called automatically whenever we call functions like localtime, mktime, ctime etc.

Thanks,
Ravi Nandibhatla.
Dennis Handly
Acclaimed Contributor

Re: calculating timezone difference on HP-UX

>How did you get the timezone value so that we can divide it by 3600?

When you call tzset(), timezone is automatically set.

>Also the man page indicates that tzset() will be called automatically whenever we call functions like localtime, mktime, ctime etc.

Yes but my "example" ONLY had tzset and the printf of timezone.
Nandibhatla Ravi
Occasional Advisor

Re: calculating timezone difference on HP-UX

Thanks Dennis. Now I understand completely.
Nandibhatla Ravi
Occasional Advisor

Re: calculating timezone difference on HP-UX

Now I can easily calculate the timezone difference with just using the tzset() function.
Dennis Handly
Acclaimed Contributor

Re: calculating timezone difference on HP-UX

You neglected to assign any points to the my answers:
http://forums1.itrc.hp.com/service/forums/helptips.do?#33

You can reopen it with:
http://forums1.itrc.hp.com/service/forums/helptips.do?#41
Nandibhatla Ravi
Occasional Advisor

Re: calculating timezone difference on HP-UX

As mentioned in my previous close thread message