Python Time strptime Function

The strptime function in Python's time module parses a string representing a time according to a format and returns a struct_time object. This function is useful for converting formatted time strings into structured time data.

Table of Contents

  1. Introduction
  2. strptime Function Syntax
  3. Examples
    • Basic Usage
    • Parsing Different Time Formats
  4. Real-World Use Case
  5. Conclusion

Introduction

The strptime function in Python's time module converts a string representing a time to a struct_time object based on a specified format. This is useful for parsing time and date information from strings.

strptime Function Syntax

Here is how you use the strptime function:

import time
time.strptime(string, format)

Parameters:

  • string: The string representing the time.
  • format: The format string that specifies how the time is represented in the string. This should correspond to the structure of the time string.

Returns:

  • A struct_time object representing the parsed 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 strptime.

Example

import time

# Parsing a formatted time string
time_string = "2021-08-17 15:43:06"
parsed_time = time.strptime(time_string, "%Y-%m-%d %H:%M:%S")
print("Parsed time:", parsed_time)

Output:

Parsed time: time.struct_time(tm_year=2021, tm_mon=8, tm_mday=17, tm_hour=15, tm_min=43, tm_sec=6, tm_wday=1, tm_yday=229, tm_isdst=-1)

Parsing Different Time Formats

This example shows how to parse time strings in different formats.

Example

import time

# Parsing a time string with a different format
time_string = "17/08/2021 15:43:06"
parsed_time = time.strptime(time_string, "%d/%m/%Y %H:%M:%S")
print("Parsed time:", parsed_time)

# Parsing another time string with month name and day of the week
time_string = "Tuesday August 17 15:43:06 2021"
parsed_time = time.strptime(time_string, "%A %B %d %H:%M:%S %Y")
print("Parsed time:", parsed_time)

Output:

Parsed time: time.struct_time(tm_year=2021, tm_mon=8, tm_mday=17, tm_hour=15, tm_min=43, tm_sec=6, tm_wday=1, tm_yday=229, tm_isdst=-1)
Parsed time: time.struct_time(tm_year=2021, tm_mon=8, tm_mday=17, tm_hour=15, tm_min=43, tm_sec=6, tm_wday=1, tm_yday=229, tm_isdst=-1)

Real-World Use Case

Parsing Log Timestamps

In real-world applications, the strptime function can be used to parse timestamps from log files and convert them into structured time data for further processing.

Example

import time

def parse_log_timestamp(log_line):
    timestamp_str = log_line.split(' - ')[0]
    return time.strptime(timestamp_str, "%Y-%m-%d %H:%M:%S")

# Example usage
log_line = "2021-08-17 15:43:06 - Event: Start process"
parsed_timestamp = parse_log_timestamp(log_line)
print("Parsed timestamp:", parsed_timestamp)

Output:

Parsed timestamp: time.struct_time(tm_year=2021, tm_mon=8, tm_mday=17, tm_hour=15, tm_min=43, tm_sec=6, tm_wday=1, tm_yday=229, tm_isdst=-1)
Exception ignored in tp_clear of: <class 'dict'>
TypeError: Missed attribute 'n_fields' of type time.struct_time

Conclusion

The strptime function in Python's time module parses a string representing a time according to a format and returns a struct_time object. This function is useful for converting formatted time strings into structured time data.

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