obtaining the time offset from UTC

J

__jakal__

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

Regards,
deep
 
E

Eric Sosman

__jakal__ said:
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.
 
C

CBFalconer

__jakal__ said:
I need to find out the time difference between UTC and local time.
I am doing it the following way

#include <sys/timeb.h>

No such standard header.
#include <stdio.h>
int main()
{
struct timeb tp;

No such standard type
ftime(&tp);

No such standard function.
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.

Which makes your code entirely non-portable. The things available
to you via <time.h> are detailed below, in an excerpt from N869:

B.22 Date and time <time.h>
NULL size_t *time_t
CLOCKS_PER_SEC clock_t struct tm
clock_t clock(void);
double difftime(time_t time1, time_t time0);
time_t mktime(struct tm *timeptr);
time_t time(time_t *timer);
char *asctime(const struct tm *timeptr);
char *ctime(const time_t *timer);
struct tm *gmtime(const time_t *timer);
struct tm *localtime(const time_t *timer);
size_t strftime(char * restrict s,
size_t maxsize,
const char * restrict format,
const struct tm * restrict timeptr);
 
C

Charles Richmond

CBFalconer said:
No such standard header.


No such standard type


No such standard function.
"return to sender...no such number, no such home" -- Elvis

--
+----------------------------------------------------------------+
| Charles and Francis Richmond It is moral cowardice to leave |
| undone what one perceives right |
| richmond at plano dot net to do. -- Confucius |
+----------------------------------------------------------------+
 
D

Dave Thompson

__jakal__ said:
Hello,
I need to find out the time difference between UTC and local time.
I am [using (POSIX) struct timeb and ftime()]
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.
And if it does, standard C doesn't require it to use env var TZ.
POSIX/SUS does, and provides ftime() and struct timeb, which also
includes member dstflag, which is nonzero when (for a time that) DST
is in effect (assuming that the daylight/summer change is always +1hr,
which is very common but probably not universal).
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);
Examine the corresponding elements of `lcl' and `gmt' to
determine your UTC offset.

Or just look at lcl.tm_isdst to determine if there is _any_
daylight/summer in effect and if so assume +1hr.

These are I believe the only fully-standard-C ways. Note that
localtime(), and its difference from gmtime(), can be applied to times
other than the current time, if you wish.

- David.Thompson1 at worldnet.att.net
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top