learn-python/contrib/advanced-python/dates_and_times.md

4.1 KiB
Czysty Wina Historia

Working with Dates and Times in Python

Handling dates and times is an essential aspect of many programming tasks. Python provides robust modules to work with dates and times, making it easier to perform operations like formatting, parsing, and arithmetic. This guide provides an overview of these modules and their key functionalities.

1. 'datetime' Module

The datetime module supplies classes for manipulating dates and times. The main classes in the datetime module are:

  • date: Represents a date (year, month, day).
  • time: Represents a time (hour, minute, second, microsecond).
  • datetime: Combines date and time information.
  • timedelta: Represents the difference between two dates or times.
  • tzinfo: Provides time zone information objects.

Key Concepts:

  • Naive vs. Aware: Naive datetime objects do not contain time zone information, while aware datetime objects do.
  • Immutability: date and time objects are immutable; once created, they cannot be changed.

Example:

import datetime
# Get the current date and time
now = datetime.datetime.now()
print("Current date and time:", now)

2. Formatting Dates and Times

Formatting involves converting datetime objects into human-readable strings. This is achieved using the strftime method, which stands for "string format time." You can specify various format codes to dictate how the output string should be structured.

Common Format Codes:

  • %Y: Year with century (e.g., 2024)
  • %m: Month as a zero-padded decimal number (e.g., 01)
  • %d: Day of the month as a zero-padded decimal number (e.g., 15)
  • %H: Hour (24-hour clock) as a zero-padded decimal number (e.g., 13)
  • %M: Minute as a zero-padded decimal number (e.g., 45)
  • %S: Second as a zero-padded decimal number (e.g., 30)

Example:

import datetime

now = datetime.datetime.now()
formatted_now = now.strftime("%Y-%m-%d %H:%M:%S")
print("Formatted current date and time:", formatted_now)

3. Parsing Dates and Times

Parsing is the process of converting strings representing dates and times into datetime objects. The strptime method, which stands for "string parse time," allows you to specify the format of the input string.

Example:

import datetime

date_string = "2024-05-15 13:45:30"
date_object = datetime.datetime.strptime(date_string, "%Y-%m-%d %H:%M:%S")
print("Parsed date and time:", date_object)

4. Working with Time Differences

The timedelta class is used to represent the difference between two datetime objects. This is useful for calculations involving durations, such as finding the number of days between two dates or adding a certain period to a date.

Example:

import datetime

date1 = datetime.datetime(2024, 5, 15, 12, 0, 0)
date2 = datetime.datetime(2024, 5, 20, 14, 30, 0)

difference = date2 - date1
print("Difference:", difference)
print("Days:", difference.days)
print("Total seconds:", difference.total_seconds())

5. Time Zones

Time zone handling in Python is facilitated by the pytz library. It allows you to convert naive datetime objects into timezone-aware objects and perform operations across different time zones.

Key Concepts:

  • Timezone-aware: A datetime object that includes timezone information.
  • Localization: The process of associating a naive datetime with a time zone.

Example:

import datetime
import pytz

# Define a timezone
tz = pytz.timezone('Asia/Kolkata')

# Get the current time in a specific timezone
now = datetime.datetime.now(tz)
print("Current time in Asia/Kolkata:", now)

6. Date Arithmetic

Date arithmetic involves performing operations like addition or subtraction on date or datetime objects using timedelta. This is useful for calculating future or past dates based on a given date.

Example:

import datetime

today = datetime.date.today()
future_date = today + datetime.timedelta(days=10)
print("Date after 10 days:", future_date)

Summary

Pythons datetime module and the pytz library provide comprehensive tools for working with dates, times, and time zones. They enable you to perform a wide range of operations, from basic date manipulations to complex time zone conversions.