Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   find timezone using C (http://www.go4expert.com/forums/timezone-using-c-t8159/)

technosavvy 8Jan2008 17:07

find timezone using C
 
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.

Salem 8Jan2008 19:20

Re: find timezone using C
 
http://www.hmug.org/man/3/strftime.php
http://www.hmug.org/man/3/ctime.php
gmtime(), localtime(), strftime() would seem to be functions worth a look.

technosavvy 8Jan2008 22:21

Re: find timezone using C
 
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..

Salem 9Jan2008 05:21

Re: find timezone using C
 
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;
}


technosavvy 9Jan2008 09:50

Re: find timezone using C
 
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...

technosavvy 9Jan2008 10:44

Re: find timezone using C
 
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;
}


technosavvy 9Jan2008 10:46

Re: find timezone using C
 
ignore the indentation in the last post...
it messed while copying the code from Visual Studio editor to explorer.. :)


All times are GMT +5.5. The time now is 17:30.