1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

find timezone using C

Discussion in 'C' started by technosavvy, Jan 8, 2008.

  1. technosavvy

    technosavvy New Member

    Joined:
    Jan 2, 2008
    Messages:
    52
    Likes Received:
    0
    Trophy Points:
    0
    how to know the timezone using C ...

    i want something which will work both on Linux and Windows...

    for example in case of India...i wish to know using the program that the timezone is GMT+ 5:30

    i found a variable called _timezone which works in case of Windows but not on Linux.
     
  2. Salem

    Salem New Member

    Joined:
    Nov 15, 2007
    Messages:
    133
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Please don't PM me for 1:1 support.
  3. technosavvy

    technosavvy New Member

    Joined:
    Jan 2, 2008
    Messages:
    52
    Likes Received:
    0
    Trophy Points:
    0
    i have tried all the above functions...but what i need is to print the current time in the undermentioned format:
    Tue Jan 08 2008 22:25:22 GMT+0530 (India Standard Time)

    i am able to print everything except GMT+530 using the function strftime...
    so i am looking for some variable or function which can tell me the current timezone..
    and i have to find the solution only within the ANSI/C89 boundaries..the functions like localtime must be using something to chge the time from UTC to localtime..
     
  4. Salem

    Salem New Member

    Joined:
    Nov 15, 2007
    Messages:
    133
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Please don't PM me for 1:1 support.
    So what does this produce for you?
    Code:
    #include <stdio.h>
    #include <time.h>
    
    int main() {
        time_t  now = time(NULL);
        struct tm tnow = *gmtime(&now);
        char    buff[100];
        strftime( buff, sizeof buff, "%a %b %d %Y %T %Z%z", &tnow );
        printf( ">>%s<<\n", buff );
        return 0;
    }
    
     
  5. technosavvy

    technosavvy New Member

    Joined:
    Jan 2, 2008
    Messages:
    52
    Likes Received:
    0
    Trophy Points:
    0
    This is the output i get..instead of the time difference in number...i get India Standard Time twice...!!


    >>Wed Jan 09 2008 India Standard TimeIndia Standard Time<<

    as I stated earlier ..i need GMT+0530..

    for the time being what i am doing is ..subtracting the value of localtime and gmtime..
    hence i get the time difference in seconds..(which comes out to be 19800 seconds in case of India) ...so i calculate number of hours from it and concatenate it to my output string...

    but i still need a way out of calculating the time difference...
     
  6. technosavvy

    technosavvy New Member

    Joined:
    Jan 2, 2008
    Messages:
    52
    Likes Received:
    0
    Trophy Points:
    0
    this is something what i have tried and is now giving the right output...
    still looking for an easy way out :D :)

    Code:
    #include <stdio.h>
    #include <time.h>
    #include <string.h>
    #include <stdarg.h>
    #include <stdlib.h>
    #include <math.h>
    
    char tzarray[13][3] = {"00", "01", "02", "03", "04", "05", "06", \
    "07", "08", "09", "10", "11", "12"};
    char tzmin[][3] = {"00", "30"};
    
    int main(void) {
        long dtime = 0;
        short diffhour = 0;
    	char diffmin[3] = {"00"};
        char *strtm, *strzone;
        time_t tm1, tm2;
    	struct tm *t1, *t2;
        strtm = malloc(120 * sizeof(char));
        strzone = malloc(50 * sizeof(char));
        tm1 = time(NULL);
        t2 = gmtime(&tm1);
        tm2 = mktime(t2);
    	t1 = localtime(&tm1);
        dtime = (long)(tm1 - tm2);
    /* Print local time as a string */
        strftime(strtm, 100, "%a %b %d %Y %H:%M:%S GMT", t1);
        if (dtime >= 0 )    
    		strcat(strtm, "+");
        else 
    		strcat(strtm, "-");
    	strcat(strtm, tzarray[(short)(abs(dtime) / 3600)]);
        if (1800 == (short)(abs(dtime) % 3600))
    		strcat(strtm, tzmin[1]);
    	else
    		strcat(strtm, tzmin[0]);
        strftime(strzone, 50, " (%Z)", t1);
        strcat(strtm, strzone);
        printf("%s\n\n\n", strtm);
        return 0;
    }
     
  7. technosavvy

    technosavvy New Member

    Joined:
    Jan 2, 2008
    Messages:
    52
    Likes Received:
    0
    Trophy Points:
    0
    ignore the indentation in the last post...
    it messed while copying the code from Visual Studio editor to explorer.. :)
     

Share This Page