Time Zone Issue

I have Googled how to use tm_isdst, but no examples are to be found. What I would like to do is include that flag so I can stop manually change it.
void Time_Zones()
{
	time_t a;
	struct tm *b;
	time (&a);
	b = gmtime (&a);
	int c = 24;
	printf ("Washington, D.C. (U.S.):       %2d:%02d:%02d\n", 
		   (b -> tm_hour + -4) % c, b -> tm_min, b -> tm_sec); 
}

Comments

  • The reference say: "The Daylight Saving Time flag (tm_isdst) is greater than zero if Daylight Saving Time is in effect, zero if Daylight Saving Time is not in effect, and less than zero if the information is not available.". I'd never used that flag but i suppose that you can use it with something like this:

    b->tm_hour + (b->tm_isdst>0 ? -1 : 0)

    If daylight saving time is in effect, the hour has been added by one (so it works in Italy) so we should subract one, to have solar hour. In case the tm_isdst is <0, we can't know if it is in effect or not.
  • Yes I know testing it is hard that's because the clock does not need to be changed everyday. Now if my code is left like this 24-7 will it be correct year after year so on and so forth?
    void Time_Zones()
    {
    	time_t a;
    	struct tm *b;
    	time (&a);
    	b = gmtime (&a);
    	int c = 24;
    	printf ("Washington, D.C. (U.S.):       %2d:%02d:%02d\n", 
    		   (b -> tm_hour + (b -> tm_isdst > 0 ? -1 : 0) + -4) % c, b -> tm_min, b -> tm_sec);
    }
    
  • Yes I know testing it is hard that's because the clock does not need to be changed everyday. Now if my code is left like this 24-7 will it be correct year after year so on and so forth?
    void Time_Zones()
    {
    	time_t a;
    	struct tm *b;
    	time (&a);
    	b = gmtime (&a);
    	int c = 24;
    	printf ("Washington, D.C. (U.S.):       %2d:%02d:%02d\n", 
    		   (b -> tm_hour + (b -> tm_isdst > 0 ? -1 : 0) + -4) % c, b -> tm_min, b -> tm_sec);
    }
    

    I think that this way it will works, but you have just to try it, to know. I suggest you to put "Washington..." in a const char * array and the same for -4, so to have a set of couples city+delta_hours (or better use a const array of struct with char* and int on it) then set an enum for any town, something like this:
    struct strTown{ char *city; int delta; };
    const strTown Towns[] = {{"Washington...", -4}, {"Other town...", -2}, ...};
    enum enTowns{ TW_WASHINGTON, TW_OTHER_TOWN, TW... };
    void void Time_Zones(enum enTowns town)
    {
    ...
    }
    
  • I eventually want to do this with a ton of other places around the world the above code is nice but it would drive me nuts after a while. Here is what the whole thing looks like. It was all right at one point in time, but it's not anymore.
    #include <stdio.h>
    #include <time.h>
    #include <windows.h>
    #include <strsafe.h> 
    
    #pragma warning (disable: 4996)
    /* 
    Disables gmtime() warning of unsafe function and wants this gmtime_s() 
     */ 
    
    void Title()
    {
        wchar_t a [1], b [24];
        StringCchPrintf (b, 24, TEXT ("Current Time World Wide"), GetConsoleTitle (a, 0));
        SetConsoleTitle (b);
    }
    /*
    Changing the Title
     */ 
    
    void Color()
    {
        SetConsoleTextAttribute (GetStdHandle (STD_OUTPUT_HANDLE), FOREGROUND_GREEN | FOREGROUND_INTENSITY);
    }
    /*
    The Bright White Text is energy consuming, and can irritate the eyes. 
    The Bright Green Text is energy efficient with the benefit of easiness on the eyes.
     */ 
    
    void clrscr(int a, int b)
    {
        COORD c;
        c.X = a, c.Y = b;
        SetConsoleCursorPosition (GetStdHandle (STD_OUTPUT_HANDLE), c);
    }
    /*
    Clear's The Screen because without it everything prints nonsense.
     */ 
    
    void Time_Zones()
    {
        time_t a;
        struct tm *b;
        time (&a);
        b = gmtime (&a);
        int c = 24;
        printf ("Wellington, (New Zealand):     %2d:%02d:%02d\n", 
               (b -> tm_hour + -11) % c, b -> tm_min, b -> tm_sec);
    
        printf ("Honolulu, Hawaii, (U.S):       %2d:%02d:%02d\n", 
               (b -> tm_hour + -10) % c, b -> tm_min, b -> tm_sec);
    
        printf ("Juneau, (Alaska):              %2d:%02d:%02d\n", 
               (b -> tm_hour + -9) % c, b -> tm_min, b -> tm_sec);
    
        printf ("Los Angeles, California (U.S): %2d:%02d:%02d\n", 
               (b -> tm_hour + -8) % c, b -> tm_min, b -> tm_sec);
    
        printf ("Phoenix, AZ (U.S.):            %2d:%02d:%02d\n", 
               (b -> tm_hour + -7) % c, b -> tm_min, b -> tm_sec);
    
        printf ("Mexico, (Mexico):              %2d:%02d:%02d\n", 
               (b -> tm_hour + -6) % c, b -> tm_min, b -> tm_sec);
    
        printf ("Washington, D.C. (U.S.):       %2d:%02d:%02d\n", 
               (b -> tm_hour + -5) % c, b -> tm_min, b -> tm_sec);
    
        printf ("Manaus, (Brazil):              %2d:%02d:%02d\n", 
               (b -> tm_hour + -4) % c, b -> tm_min, b -> tm_sec);
    
        printf ("Nuuk, (Greenland):             %2d:%02d:%02d\n", 
               (b -> tm_hour + -3) % c, b -> tm_min, b -> tm_sec);
    
        printf ("Sao Paulo, (Brazil):           %2d:%02d:%02d\n", 
               (b -> tm_hour + -2) % c, b -> tm_min, b -> tm_sec); /* a~ */ 
    
        printf ("                               %2d:%02d:%02d\n", 
               (b -> tm_hour + -1) % c, b -> tm_min, b -> tm_sec);
    /*
     Negative Time Zones
     */ 
    
    
        printf ("Edinburgh, (Scotland):         %2d:%02d:%02d\n", 
               (b -> tm_hour + 0) % c, b -> tm_min, b -> tm_sec);
    /*
     Neutral Time Zone
     */ 
    
    
        printf ("Berlin, (Germany):             %2d:%02d:%02d\n", 
               (b -> tm_hour + +1) % c, b -> tm_min, b -> tm_sec);
    
        printf ("Cairo, (Egypt):                %2d:%02d:%02d\n", 
               (b -> tm_hour + +2) % c, b -> tm_min, b -> tm_sec);
    
        printf ("Doha, (Qatar)                  %2d:%02d:%02d\n", 
               (b -> tm_hour + +3) % c, b -> tm_min, b -> tm_sec);
    
        printf ("Moscow, (Russia):              %2d:%02d:%02d\n", 
               (b -> tm_hour + +4) % c, b -> tm_min, b -> tm_sec);
    
        printf ("New Delhi, (India):            %2d:%02d:%02d\n", 
               (b -> tm_hour + +5) % c, b -> tm_min, b -> tm_sec);
    
        printf ("Yekaterinburg, (Russia):       %2d:%02d:%02d\n", 
               (b -> tm_hour + +6) % c, b -> tm_min, b -> tm_sec);
    
        printf ("Jakarta, (Indonesia):          %2d:%02d:%02d\n", 
               (b -> tm_hour + +7) % c, b -> tm_min, b -> tm_sec);
    
        printf ("Beijing, (China):              %2d:%02d:%02d\n", 
               (b -> tm_hour + +8) % c, b -> tm_min, b -> tm_sec);
    
        printf ("Tokyo, (Japan):                %2d:%02d:%02d\n", 
               (b -> tm_hour + +9) % c, b -> tm_min, b -> tm_sec);
    
        printf ("                               %2d:%02d:%02d\n", 
               (b -> tm_hour + +10) % c, b -> tm_min, b -> tm_sec);
    
        printf ("Sydney, (Australia):           %2d:%02d:%02d\n", 
               (b -> tm_hour + +11) % c, b -> tm_min, b -> tm_sec);
        /*
         Positive Time Zones
         */ 
    }
    /*
    Places around the world with possibly the most obvious well known by most people as having relevancy in those time zones.
     */ 
    
    void _24_Hour_World_Clock()
    {
        for (;;)
        {
            Title();
            Color();
            Time_Zones();
            clrscr (0, 0);
        }
    }
    
    int main()
    {
        _24_Hour_World_Clock(); 
    }
    
  • A good way to do this is to make each city into a struct. One struct member would be the name of the city naturally, but another struct member would be it's offset from GMT/ UTC, just a simple integer. So Paris has -1 and New York has +4, San Francisco has +7, etc.

    Now you can create an array of city structs, from you data file (you can get the data on the net, but you might need to reformat it for your program).
Sign In or Register to comment.