Sai A Sai A
Updated date Feb 12, 2024
In this blog, we will learn how to convert timestamps into easily understandable date and time formats using C programming language.

Introduction:

Timestamps are essential in computer science and programming, representing a particular moment in time, typically measured in seconds or milliseconds since a reference point. While timestamps are precise and efficient for computers, they can be challenging for humans to interpret. In this blog, we will learn the process of converting timestamps into human-readable date and time formats using the C programming language. 

Method 1: Using the time() Function

The time() function from the time.h header in C returns the current timestamp in seconds since the Unix epoch (January 1, 1970). We can use this timestamp and the localtime() function to convert it into a struct tm object representing the local time. Finally, we can format this object into a human-readable string using strftime().

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

int main() {
    time_t timestamp = time(NULL);
    struct tm *local_time = localtime(&timestamp);
    char time_str[100];

    strftime(time_str, sizeof(time_str), "%c", local_time);
    printf("Current date and time: %s\n", time_str);

    return 0;
}

Output:

Current date and time: Sun Feb  6 22:40:38 2024
  • We retrieve the current timestamp using the time() function.
  • The localtime() function converts this timestamp into a struct tm object representing the local time.
  • The strftime() function formats the struct tm object into a human-readable string using the specified format ("%c" represents the default date and time format).

Method 2: Using mktime() Function

In some cases, we may have individual components of a date and time (year, month, day, hour, minute, second) instead of a timestamp. We can utilize the mktime() function to convert these components into a timestamp, which we can then process using the previously discussed method.

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

int main() {
    struct tm time_components = {0};
    time_t timestamp;
    char time_str[100];

    // Assigning time components
    time_components.tm_year = 2024 - 1900; // Year - 1900
    time_components.tm_mon = 1; // Month (0 - 11)
    time_components.tm_mday = 7; // Day of the month (1 - 31)
    time_components.tm_hour = 12; // Hour (0 - 23)
    time_components.tm_min = 30; // Minute (0 - 59)
    time_components.tm_sec = 0; // Second (0 - 60)

    // Convert time components to timestamp
    timestamp = mktime(&time_components);

    // Format timestamp into a string
    strftime(time_str, sizeof(time_str), "%c", &time_components);
    printf("Given date and time: %s\n", time_str);

    return 0;
}

Output:

Given date and time: Sun Feb  7 12:30:00 2024
  • We initialize a struct tm object with individual components of date and time.
  • Using mktime(), we convert these components into a timestamp.
  • Finally, we format the timestamp into a human-readable string using strftime().

Conclusion:

In this blog, we have explored the concept of timestamps and demonstrated multiple methods to convert them into human-readable date and time formats using the C programming language. From using the time() function to working with individual components of date and time, each method offers flexibility and caters to different use cases. 

Comments (0)

There are no comments. Be the first to comment!!!