The strftime
function in Python's time
module formats a struct_time
object or a tuple representing a time as a string according to a specified format. This function is useful for creating human-readable representations of time and date.
Table of Contents
- Introduction
strftime
Function Syntax- Examples
- Basic Usage
- Formatting Current Time
- Real-World Use Case
- Conclusion
Introduction
The strftime
function in Python's time
module converts a struct_time
object or a tuple representing time to a formatted string. This is useful for displaying time and date information in a human-readable format.
strftime Function Syntax
Here is how you use the strftime
function:
import time
time.strftime(format, t)
Parameters:
format
: A string specifying the desired format of the output. It can include various format codes to represent different parts of the date and time.t
: An optionalstruct_time
object or a tuple representing time. If not provided, the current time is used.
Returns:
- A string representing the formatted time.
Common Format Codes:
%Y
: Year with century (e.g., 2021)%m
: Month as a zero-padded decimal number (e.g., 08)%d
: Day of the month as a zero-padded decimal number (e.g., 17)%H
: Hour (24-hour clock) as a zero-padded decimal number (e.g., 15)%M
: Minute as a zero-padded decimal number (e.g., 43)%S
: Second as a zero-padded decimal number (e.g., 06)%A
: Full weekday name (e.g., Tuesday)%B
: Full month name (e.g., August)
Examples
Basic Usage
Here is an example of how to use strftime
.
Example
import time
# Creating a struct_time object representing a specific time
time_struct = time.struct_time((2021, 8, 17, 15, 43, 6, 1, 229, 1))
# Formatting the time as a string
formatted_time = time.strftime("%Y-%m-%d %H:%M:%S", time_struct)
print("Formatted time:", formatted_time)
Output:
Formatted time: 2021-08-17 15:43:06
Formatting Current Time
This example shows how to format the current time as a string using strftime
.
Example
import time
# Getting the current local time as a struct_time object
current_time_struct = time.localtime()
# Formatting the current time as a string
formatted_current_time = time.strftime("%Y-%m-%d %H:%M:%S", current_time_struct)
print("Current formatted time:", formatted_current_time)
Output:
Current formatted time: 2024-07-23 20:33:47
Real-World Use Case
Creating Timestamps for Logging
In real-world applications, the strftime
function can be used to create formatted timestamps for logging events.
Example
import time
def log_event(event):
current_time_struct = time.localtime()
timestamp = time.strftime("%Y-%m-%d %H:%M:%S", current_time_struct)
print(f"[{timestamp}] Event: {event}")
# Example usage
log_event("Start process")
time.sleep(2)
log_event("End process")
Output:
[2024-07-23 20:33:47] Event: Start process
[2024-07-23 20:33:49] Event: End process
Conclusion
The strftime
function in Python's time
module formats a struct_time
object or a tuple representing time as a string according to a specified format, making it useful for creating human-readable representations of time and date.
Comments
Post a Comment
Leave Comment