C mktime() Function

The mktime() function in C is a standard library function that converts a struct tm representation of calendar time to a time_t value. It is part of the C standard library (time.h). This function is useful for converting broken-down time to calendar time.

Table of Contents

  1. Introduction
  2. mktime() Function Syntax
  3. Examples
    • Converting tm Structure to time_t
    • Using mktime() with User Input
  4. Real-World Use Case
  5. Conclusion

Introduction

The mktime() function converts a struct tm representation of time to a time_t value. This is particularly useful for manipulating and converting between broken-down time and calendar time.

mktime() Function Syntax

The syntax for the mktime() function is as follows:

#include <time.h>
time_t mktime(struct tm *timeptr);

Parameters:

  • timeptr: A pointer to a struct tm that contains the broken-down time representation.

Returns:

  • The function returns the time_t value representing the specified calendar time. If the conversion fails, it returns -1.

Examples

Converting tm Structure to time_t

To demonstrate how to use mktime() to convert a tm structure to time_t, we will write a simple program that sets a date and time in a tm structure and converts it to time_t.

Example

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

int main() {
    struct tm time_struct = {0};
    time_t time_value;

    // Set the date and time
    time_struct.tm_year = 2023 - 1900; // Year since 1900
    time_struct.tm_mon = 6;            // Month (0-11)
    time_struct.tm_mday = 4;           // Day of the month (1-31)
    time_struct.tm_hour = 12;          // Hour (0-23)
    time_struct.tm_min = 30;           // Minute (0-59)
    time_struct.tm_sec = 0;            // Second (0-59)

    // Convert tm structure to time_t
    time_value = mktime(&time_struct);

    // Print the result
    if (time_value != -1) {
        printf("The time_t value is: %ld\n", time_value);
        printf("The corresponding date and time is: %s", ctime(&time_value));
    } else {
        printf("Conversion failed.\n");
    }

    return 0;
}

Output:

The time_t value is: 1688477400
The corresponding date and time is: Tue Jul  4 12:30:00 2023

Using mktime() with User Input

This example shows how to use mktime() to convert a user-provided date and time to time_t.

Example

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

int main() {
    struct tm time_struct = {0};
    time_t time_value;

    // Get user input for the date and time
    printf("Enter year: ");
    scanf("%d", &time_struct.tm_year);
    time_struct.tm_year -= 1900; // Adjust year

    printf("Enter month (1-12): ");
    scanf("%d", &time_struct.tm_mon);
    time_struct.tm_mon -= 1; // Adjust month

    printf("Enter day of the month (1-31): ");
    scanf("%d", &time_struct.tm_mday);

    printf("Enter hour (0-23): ");
    scanf("%d", &time_struct.tm_hour);

    printf("Enter minute (0-59): ");
    scanf("%d", &time_struct.tm_min);

    printf("Enter second (0-59): ");
    scanf("%d", &time_struct.tm_sec);

    // Convert tm structure to time_t
    time_value = mktime(&time_struct);

    // Print the result
    if (time_value != -1) {
        printf("The time_t value is: %ld\n", time_value);
        printf("The corresponding date and time is: %s", ctime(&time_value));
    } else {
        printf("Conversion failed.\n");
    }

    return 0;
}

Output (example user input year "2023", month "7", day "4", hour "12", minute "30", second "0"):

Enter year: 2023
Enter month (1-12): 7
Enter day of the month (1-31): 4
Enter hour (0-23): 12
Enter minute (0-59): 30
Enter second (0-59): 0
The time_t value is: 1688477400
The corresponding date and time is: Tue Jul  4 12:30:00 2023

Real-World Use Case

Converting User Input to Calendar Time

In real-world applications, the mktime() function can be used to convert user-provided date and time input into calendar time, which can then be used for various calculations or storage in databases.

Example: Storing Event Timestamps

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

// Function to get user input for date and time
void get_user_input(struct tm *time_struct) {
    printf("Enter year: ");
    scanf("%d", &time_struct->tm_year);
    time_struct->tm_year -= 1900; // Adjust year

    printf("Enter month (1-12): ");
    scanf("%d", &time_struct->tm_mon);
    time_struct->tm_mon -= 1; // Adjust month

    printf("Enter day of the month (1-31): ");
    scanf("%d", &time_struct->tm_mday);

    printf("Enter hour (0-23): ");
    scanf("%d", &time_struct->tm_hour);

    printf("Enter minute (0-59): ");
    scanf("%d", &time_struct->tm_min);

    printf("Enter second (0-59): ");
    scanf("%d", &time_struct->tm_sec);
}

int main() {
    struct tm event_time = {0};
    time_t event_timestamp;

    // Get user input for the event date and time
    get_user_input(&event_time);

    // Convert tm structure to time_t
    event_timestamp = mktime(&event_time);

    // Print the result
    if (event_timestamp != -1) {
        printf("The event timestamp is: %ld\n", event_timestamp);
        printf("The corresponding date and time is: %s", ctime(&event_timestamp));
    } else {
        printf("Conversion failed.\n");
    }

    return 0;
}

Output (example user input year "2023", month "7", day "4", hour "12", minute "30", second "0"):

Enter year: 2023
Enter month (1-12): 7
Enter day of the month (1-31): 4
Enter hour (0-23): 12
Enter minute (0-59): 30
Enter second (0-59): 0
The event timestamp is: 1688477400
The corresponding date and time is: Tue Jul  4 12:30:00 2023

Conclusion

The mktime() function is essential for converting a tm structure to a time_t value in C. It is useful in various applications, particularly in date and time manipulation, where it is necessary to convert broken-down time to calendar time for calculations, storage, and display.

Comments

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare