Thread: using strftime() with gmtime()

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

    using strftime() with gmtime()

    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 PDT_function(void);
    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");
        printf("(-co) Colorado\n");
            printf("(-ph) Phillipines\n");
    
    
        PDT_function();
    
    
        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 PDT_function(void)
    {
        time_t now;
        struct tm *ptr;
    
    
        time(&now);
        ptr = gmtime(&now);
    
    
        printf("PDT (San Diego/Local) : %2d:%02d\n", (ptr->tm_hour+PDT)%24, ptr->tm_min);
    
    
        return;
    }
    
    
    void co(void)
    {
        time_t now;
        struct tm *ptr;
    
    
        time(&now);
        ptr = gmtime(&now);
    
    
        // 1st. Output date in MM/DD/YYYY format
        // printf("Denver  : %d/%d/%d\n", ptr->tm_mon, ptr->tm_mday, ptr->tm_year);
        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);
    }
    After compiling the code and running "./a.out -co"

    Code:
    [jamie@bindfix ~/j_src/time_convertor]$ ./a.out -co
    Timezone Convertor v0.01
    FLAGS: -co or -ph
    (-co) Colorado
    (-ph) Phillipines
    PDT (San Diego/Local) : 14:49
    Denver  : 15:49
    [jamie@bindfix ~/j_src/time_convertor]$
    First of all, theres a segmentation fault if running program without any command arguments but that's not the part i'm working on right now. I would like to first change the output

    Code:
            printf("PDT (San Diego/Local) : %2d:%02d\n", (ptr->tm_hour+PDT)%24, ptr->tm_min);
    To take advantage of this function, strftime(), to first of all use 12-hour clock (%I) then when using the "./a.out -ph" argument the output is

    Code:
    [jamie@bindfix ~/j_src/time_convertor]$ ./a.out -ph
    Timezone Convertor v0.01
    FLAGS: -co or -ph
    (-co) Colorado
    (-ph) Phillipines
    PDT (San Diego/Local) : 14:55
    Phillipines :  5:55
    [jamie@bindfix ~/j_src/time_convertor]$
    Phillipines is the next day after PDT (san diego) and theres no way show this without, least to my knowledge, without using strftime(). Thanks

  2. #2
    Registered User
    Join Date
    Feb 2019
    Posts
    498
    Since you are calculating all times based on your system's configured timezone, you could do this:

    Code:
    struct tm *tm;
    time_t now;
    
    now = time(NULL);  // get your local time.
    tm = gmtime( &now ); // get UTC time, based on your TZ.
    now = mktime( tm ); // 'now' is now UTC time.
    Then, all UTC+tz calculations should be made in seconds (not hours) from 'now':

    Code:
    // offsets, in seconds.
    #define MDT -21600
    #define CCT 28800
    
    time_t co_t = now + MDT;
    time_t ph_t = now + CCT;
    All you have to do now, is display the time after a short string:

    Code:
    // Example of call:
    // showtime( "Phillipines", ph_t );
    // showtime( "Denver/Colorado", co_t );
    void showtime ( char *local, time_t now )
    {
      struct tm *tm;
      int h;
    
      tm = localtime ( &now );
    
      h = tm->tm_hour;
    
      if ( h > 12 )
        h -= 12;
    
      if ( h == 0 )
        h = 12;
    
      printf ( "%s: %2d:%02d %s\n",
               local, h, tm->tm_min,
               tm->tm_hour < 12 ? "AM" : "PM" );
    }

  3. #3
    Registered User
    Join Date
    Feb 2019
    Posts
    498
    Ahhh... and take a look at this video, before you decide to play with timezones!

  4. #4
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    653
    Quote Originally Posted by flp1969 View Post
    Since you are calculating all times based on your system's configured timezone, you could do this:

    Code:
    struct tm *tm;
    time_t now;
    
    now = time(NULL);  // get your local time.
    tm = gmtime( &now ); // get UTC time, based on your TZ.
    now = mktime( tm ); // 'now' is now UTC time.
    Uh, no. time() returns the number of seconds since the Epoch, so it's already in UTC. So you don't have to call gmtime() and mktime().

    If you want to print times in different time zones, set ptr->tm_gmtoff to the number of seconds west of UTC, and then use strftime(). The tm_gmtoff field is non-standard, but it's found in 4.3BSD and later, so it's a de facto standard and can usually be relied on to exist.

    You shouldn't mess around with tm_hour, unless you really do want to specify a time in the past or future.

  5. #5
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,859
    Have a look at the response that I gave the other week


    using gmtime() with strftime() (modifying time zone)
    Fact - Beethoven wrote his first symphony in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. using gmtime() with strftime() (modifying time zone)
    By _jamie in forum C Programming
    Replies: 2
    Last Post: 03-17-2019, 02:05 PM
  2. strptime and gmtime
    By wjblack in forum C Programming
    Replies: 23
    Last Post: 10-15-2013, 04:41 AM
  3. strftime function
    By pv2010 in forum C Programming
    Replies: 1
    Last Post: 04-14-2010, 08:33 AM
  4. Problem with strftime
    By Elemento in forum C Programming
    Replies: 2
    Last Post: 03-02-2005, 06:35 PM
  5. FYI: asctime(gmtime(&mytime)) = crash!
    By anonytmouse in forum C Programming
    Replies: 2
    Last Post: 09-29-2003, 02:24 AM

Tags for this Thread