views:

114

answers:

4

Does Unix store the offset of the machine from GMT internally? like for eg:india standard time is GMT + 5:30.is this 5:30 stored some where?

i need this to use it in a script like below

if[[ off is "some value"]]
then
some statements
fi
A: 

The kernel keeps GMT time internally, and when asked for the local time calculates the offset using the timezone information. This way, if a timezone change is required, internally, the clock doesn't need change.

Ben S
+1  A: 

Traditionally in UNIX, the kernel keeps the current time in a timezone-independent form, which is what it reports to applications.

Applications consult environment variables and/or user configuration (which can be different for different users or different sessions for the one user) to determine which timezone to report the time in. For this purpose, there are tables kept on disk which hold the offsets of all timezones that the system knows about (these tables need to be continuously updated for political changes to daylight saving algorithms).

caf
A: 

In the kernel or a driver, no.

Usually, it's stored in a file called /etc/localtime. That file is often a link to a file elsewhere that contains (in compressed form) all the "rules" for converting GMT to the local time, including when daylight saving time begins and ends, the offset from GMT, and so forth.

Barry Brown
It is overridable by the `TZ` environment variable.
ephemient
A: 

The following program prints '-04:00' for me in EDT and prints '04:30' when I set TZ to 'Asia/Kolkata':

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

int
main ()
{
    int hours;
    int minutes;
    int negative_sign = 1;

    tzset ();
    // printf ("tzname: %s tzname[1]: %s\n", tzname [0], tzname [1]);
    // printf ("DST: %d\n", daylight); /* 0 when no DST */
    // printf ("timezone: %ld\n", timezone);
    /* 'timezone' is the number of seconds west of GMT */
    /* It is negative for tzs east of GMT */
    if (timezone <= 0) {
     timezone = -timezone;
     negative_sign = 0;
    }

    if (daylight) {
        timezone -= 3600; /* substract 1h when DST is active */
        if (timezone <= 0) {
            timezone = -timezone;
            negative_sign = 0;
        }
    }
    timezone /= 60; /* convert to minutes */
    hours = timezone / 60;
    minutes = timezone % 60;
    printf ("%s%02d:%02d\n", (negative_sign ? "-" : ""), hours, minutes);
    return 0;
}

Feel free to use/change whatever you want and then call it from your shell script.

Gonzalo