Open In App

Convert the Local Time to GMT in C

Last Updated : 15 Jun, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Time zone handling is a mandatory feature for applications that work across multiple geographical regions. This includes the conversion of time from one time zone to another.
There are only two natively supported time types in the standard C library via <time.h>, which are the local time and Greenwich Mean Time (GMT), also known as Coordinated Universal Time (UTC).

In this practice Problem, we will learn a complete implementation for converting local time to Greenwich Mean Time (GMT) in C language.

Prerequisite: C Basics, Time Library in C.

Implementation

Step 1: Initial setup

The initial setup includes importing the header files, creating a structure ( tm) to hold the values ,and creating other variables like raw_time( to store raw time) , is_ds,t etc.

C
#include <stdio.h>
#include <time.h>
#include <string.h>

int main() {
    //structure to hold time components
    // iinitailly all set to 0
    struct tm local_time = {0};
    //to storre raw_time 
    time_t raw_time;
    // DST set to -1 for auto detection
    int is_dst = -1; 
    
    return 0;
}

Step 2: Taking Local Time from User

In this step the the program asks for user inputs which refers to the local time and the values in the structure tm is updated according to the local time given by the user.

C
printf("Program to convert Local time to GMT \n");
    // Taking user input for Date
    printf("Enter date (DD MM YYYY): ");
    scanf("%d %d %d", &local_time.tm_mday, 
                      &local_time.tm_mon, 
                      &local_time.tm_year);
    // Months are from 0 to 11
    local_time.tm_mon -= 1;    
    // Years define years since 1900
    local_time.tm_year -= 1900; 

    // Taking user input for Date
    printf("Enter time (HH MM SS): ");
    scanf("%d %d %d", &local_time.tm_hour, 
                      &local_time.tm_min, 
                      &local_time.tm_sec);
                      
                      return 0
}

Step 3: Getting raw time from local time

In this step we will use the mktime() function infected from the time.h header which converts the local time into raw time and store the detail result into raw_time variable.

C
raw_time = mktime(&local_time);
// Checking for invalid time. 
    if (raw_time == -1) {
        printf("Error: Invalid date/time input\n");
        return 1;
    }

This step also check for invalid time input.

Step 4: Converting the raw time to GMT and printing the results.

For this step we will use the `gmtime()` function to convert the raw time to GMT; again, we will use `asctime()` to convert the time to a string and print the result.

C
// Converting to GMT
    struct tm *gmt_time = gmtime(&raw_time);

    // Showing output:  User's Local time and GMT time 
    printf("\nLocal Time: %s", asctime(&local_time));
    printf("GMT Time:   %s", asctime(gmt_time));

Final Execution

Now we combine all of the above steps to get the executable source code for the program:

C
#include <stdio.h>
#include <time.h>
#include <string.h>

int main() {
    struct tm local_time = {0};
    time_t raw_time;
    // DST set to -1 for auto detection
    int is_dst = -1; 

    printf("Program to convert Local time to GMT \n");
    // Taking user input for Date
    printf("Enter date (DD MM YYYY): ");
    scanf("%d %d %d", &local_time.tm_mday, 
                      &local_time.tm_mon, 
                      &local_time.tm_year);
    // Months are from 0 to 11
    local_time.tm_mon -= 1;    
    // Years define years since 1900
    local_time.tm_year -= 1900; 

    // Taking user input for Date
    printf("Enter time (HH MM SS): ");
    scanf("%d %d %d", &local_time.tm_hour, 
                      &local_time.tm_min, 
                      &local_time.tm_sec);

    // Convert to time_t (seconds since epoch)
    raw_time = mktime(&local_time);
    // Checking for invalid time. 
    if (raw_time == -1) {
        printf("Error: Invalid date/time input\n");
        return 1;
    }

    // Converting to GMT
    struct tm *gmt_time = gmtime(&raw_time);

    // Showing output:  User's Local time and GMT time 
    printf("\nLocal Time: %s", asctime(&local_time));
    printf("GMT Time:   %s", asctime(gmt_time));

    return 0;
}

For the following input values:

13 06 2025
08 48 25

Output

Local Time: Fri Jun 13 08:48:25 2025
GMT Time:  Fri Jun 13 03:18:25 2025

Similar Reads