__jakal__ wrote:
Hello,
I need to find out the time difference between UTC and local time.
I am doing it the following way
#include <sys/timeb.h>
#include <stdio.h>
int main()
{
struct timeb tp;
ftime(&tp);
printf("%d", tp.timezone);
}
But the problem is that the timezone value is not adjusted for daylight
saving and always returns local time zone offset.
My TZ environment variable is set to MET
Now the DST rules apply and the offset should be +2 hours
When daylight saving is not in effect, then the offset should be +1 hour.
Is there any way to find out the correct offset . i.e. it should return +2
when DST is in effect and +1 otherwise
First, the C Standard makes no guarantees about how
good the system's timekeeping is. Some systems keep track
of time zones and seasonal adjustments, while others do not
and must have their clocks adjusted manually. (A very few
systems have no clocks at all.) From C's point of view,
then, your problem may not be solvable.
However, if you are using a system that keeps track of
such things, you can determine the current UTC offset with
something like (error-checking omitted):
time_t now = time(NULL);
struct tm lcl = *localtime(&now);
struct tm gmt = *gmtime(&now);
printf ("%s\t%d\t%d\n", "year", lcl.tm_year, gmt.tm_year);
printf ("%s\t%d\t%d\n", "yday", lcl.tm_yday, gmt.tm_yday);
printf ("%s\t%d\t%d\n", "hour", lcl.tm_hour, gmt.tm_hour);
printf ("%s\t%d\t%d\n", "min", lcl.tm_min, gmt.tm_min);
printf ("%s\t%d\t%d\n", "sec", lcl.tm_sec, gmt.tm_sec);
Examine the corresponding elements of `lcl' and `gmt' to
determine your UTC offset.
--
Er*********@sun.com