unixshellgmt

Offset from GMT standard time


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

Solution

  • 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.