Thread: using gmtime() with strftime() (modifying time zone)

  1. #1
    Registered User
    Join Date
    Feb 2018
    Location
    San Diego, CA
    Posts
    123

    using gmtime() with strftime() (modifying time zone)

    Code:
    // time_convertor.c
    // Manilia, Phillipines (15 hrs ahead of San Diego)
    // Denver, CO (1 hr ahead of San Diego)
    
    #include <string.h>
    #include <stdio.h>
    #include <time.h>
    #include <stdlib.h>
    
    #define MDT (-6) // Mountain Daylight Time
    #define CCT (+8) // Phillipine Standard Time
    #define PDT (-7) // Pacific Daylight Time
    
    void co(void);
    void ph(void);
    
    int main(int argc, char *argv[])
    {
    	time_t now;
    	struct tm *ptr;
    	char buf1[80];
    
    	printf("Timezone Convertor v0.01\n");
    
    	printf("FLAGS: -co or -ph\n");
    
    	time(&now);
    
    	ptr = localtime(&now);
    
    	strftime(buf1, 80, "%x - %I:%M%p", ptr);
    	printf("Pacific Daylight Time : |%s|\n", buf1);
    
    	char str1[] = "-co";
    	char str2[] = "-ph";
    	int result;
    	int result_2;
    
    	result = strcmp(argv[1], str1);
    
    	if(result == 0)
    	{
    		co();
    	}
    
    	result_2 = strcmp(argv[1], str2);
    
    	if(result_2 == 0)
    	{
    		ph();
    	}
    
    	return 0;
    }
    
    void co(void)
    {
    	time_t now;
    	struct tm *ptr;
    
    	time(&now);
    	ptr = gmtime(&now);
    
    	printf("Denver	: %2d:%02d\n", (ptr->tm_hour+MDT)%24, ptr->tm_min);
    
    	exit(1);
    }
    
    void ph(void)
    {
    	time_t now;
    	struct tm *ptr;
    
    	time(&now);
    	ptr = gmtime(&now);
    
    	printf("Phillipines : %2d:%02d\n", (ptr->tm_hour+CCT)%24, ptr->tm_min);
    
    	exit(1);
    }
    I was wondering how to use strftime() formatting with these two lines

    Code:
    printf("Denver	: %2d:%02d\n", (ptr->tm_hour+MDT)%24, ptr->tm_min);
    
    printf("Phillipines : %2d:%02d\n", (ptr->tm_hour+CCT)%24, ptr->tm_min);
    I am even confused. Does this even make sense?

    Thanks.

  2. #2
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    strftime() has a printf feel to it...

    This code below will allow you to play with the different formats...
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #include <time.h>
    
    int main(void)
    {
        time_t timeValue;
        struct tm *tmPointer;
    
        char timeFormat[0xFF];
        char timeString[0xFF];
    
        for(;;)
        {
            puts("Enter a strftime() format string or q to quit");
    
            if((NULL == fgets(timeFormat, sizeof(timeFormat), stdin)) ||
                ('q' == timeFormat[0] && '\n' == timeFormat[1]))
            {
                break;
            }
    
            timeValue = time(NULL);
    
            tmPointer = localtime(&timeValue);
    
            if (0 == strftime(timeString, sizeof(timeString), timeFormat, tmPointer))
            {
                puts("Error - timeString is not big enough");
            }
            else
            {
                printf("Output: %s", timeString);
            }
        }
    
    
        return EXIT_SUCCESS;
    }
    strftime() function in C/C++ - GeeksforGeeks
    In addition to that list above, there is also
    %p - AM/PM
    %S - Seconds
    %U - Week number (Sunday based)
    %w - Weekday (0Sun - 6Sat)
    %W - Week number (Monday based)
    %X - Full time of day representation
    %y - Year without century
    %Y - Full year includeing century
    %Z - Timezone (or empty if not available)
    %% - Single '%' char



    Try a few different entries, like this...

    %A, %d/%m/%Y, %H:%M:%S %p


    Enjoy
    Fact - Beethoven wrote his first symphony in C

  3. #3
    Registered User
    Join Date
    Dec 2017
    Posts
    1,628
    It's probably best not to use % 24. It doesn't deal with negative values the way you might think (e.g., the result is negative). Also, it won't adjust the day. Instead, use mktime after you've added/subtracted your offset hours. If the hours go below 0 or above 23 it will adjust the values of the struct so that everything is in range. However, you would need to set the tm_isdst member correctly before the call, 1 if daylight savings, 0 if not.
    A little inaccuracy saves tons of explanation. - H.H. Munro

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Time Zone map data
    By xArt in forum Tech Board
    Replies: 7
    Last Post: 05-11-2015, 04:54 AM
  2. Time Zone
    By fragrax in forum C Programming
    Replies: 3
    Last Post: 07-10-2008, 07:53 AM
  3. need help in time zone
    By Gong in forum C++ Programming
    Replies: 2
    Last Post: 01-03-2007, 04:44 AM
  4. question on how to get different time zone
    By godhand in forum C Programming
    Replies: 5
    Last Post: 04-13-2004, 01:00 PM
  5. Time Zone Bias?
    By EdwardKoo in forum Windows Programming
    Replies: 0
    Last Post: 11-27-2002, 04:28 AM

Tags for this Thread