Python Time mktime Function

🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.

▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube

▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube

The mktime function in Python's time module converts a struct_time object representing local time to a time expressed in seconds since the Epoch. This function is useful for converting structured time data back into a timestamp.

Table of Contents

  1. Introduction
  2. mktime Function Syntax
  3. Examples
    • Basic Usage
    • Converting Current Local Time
  4. Real-World Use Case
  5. Conclusion

Introduction

The mktime function in Python's time module converts a struct_time object, which represents local time, into a floating-point number of seconds since the Epoch. This is useful for applications that need to convert human-readable time formats back into timestamps.

mktime Function Syntax

Here is how you use the mktime function:

import time
time.mktime(t)

Parameters:

  • t: A struct_time object representing local time.

Returns:

  • A floating-point number representing the time in seconds since the Epoch.

Examples

Basic Usage

Here is an example of how to use mktime.

Example

import time

# Creating a struct_time object representing a specific local time
local_time = time.struct_time((2021, 8, 17, 15, 43, 6, 1, 229, 1))

# Converting struct_time to seconds since the Epoch
time_in_seconds = time.mktime(local_time)
print("Time in seconds since the Epoch:", time_in_seconds)

Output:

Time in seconds since the Epoch: 1629195186.0

Converting Current Local Time

This example shows how to convert the current local time to a timestamp using mktime.

Example

import time

# Getting the current local time as a struct_time object
current_local_time = time.localtime()

# Converting current local time to seconds since the Epoch
current_time_in_seconds = time.mktime(current_local_time)
print("Current time in seconds since the Epoch:", current_time_in_seconds)

Output:

Current time in seconds since the Epoch: 1721747015.0

Real-World Use Case

Adjusting and Storing Timestamps

In real-world applications, the mktime function can be used to adjust structured local time data and convert it back to a timestamp for storage or further processing.

Example

import time

def adjust_time(year, month, day, hour, minute, second):
    # Create a struct_time object with the given parameters
    adjusted_time = time.struct_time((year, month, day, hour, minute, second, 0, 0, -1))
    
    # Convert the struct_time object to seconds since the Epoch
    timestamp = time.mktime(adjusted_time)
    return timestamp

# Example usage
timestamp = adjust_time(2021, 8, 17, 15, 43, 6)
print("Adjusted time in seconds since the Epoch:", timestamp)

Output:

Adjusted time in seconds since the Epoch: 1629195186.0

Conclusion

The mktime function converts a struct_time object representing local time into seconds since the Epoch, making it useful for converting structured time data back into timestamps.

My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:

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