ZetCode

Python time.altzone Function

Last modified April 11, 2025

This comprehensive guide explores Python's time.altzone function, which returns the offset of the local DST timezone in seconds west of UTC. We'll cover timezone calculations, daylight savings handling, and examples.

Basic Definitions

The time.altzone function returns the offset of the local daylight saving time (DST) timezone in seconds west of UTC (negative in most of Europe, positive in the US). It's the DST version of time.timezone.

Key characteristics: returns integer seconds, negative east of UTC, positive west of UTC, and represents the DST timezone offset when applicable. The value is constant for a given Python process.

Basic Timezone Offset

This example shows basic usage of time.altzone to get the DST timezone offset and convert it to hours for better readability.

basic_altzone.py
import time

# Get DST timezone offset in seconds
dst_offset = time.altzone
print(f"DST timezone offset in seconds: {dst_offset}")

# Convert to hours
dst_hours = dst_offset / 3600
print(f"DST timezone offset in hours: {dst_hours:.1f}")

# Compare with standard timezone offset
std_offset = time.timezone
print(f"Standard offset: {std_offset/3600:.1f} hours")
print(f"Difference: {(std_offset - dst_offset)/3600:.1f} hours")

This demonstrates getting the DST offset and comparing it with the standard timezone offset. The difference typically shows the DST adjustment amount.

The division by 3600 converts seconds to hours, and the :.1f format shows one decimal place for readability.

Checking Daylight Savings Time

This example uses time.altzone with time.localtime to determine if daylight savings time is currently in effect.

check_dst.py
import time

def is_dst_active():
    local_time = time.localtime()
    return local_time.tm_isdst > 0

# Check current DST status
if is_dst_active():
    print("Daylight Savings Time is currently active")
    print(f"DST offset: {time.altzone/3600:.1f} hours from UTC")
else:
    print("Standard time is currently active")
    print(f"Standard offset: {time.timezone/3600:.1f} hours from UTC")

The tm_isdst flag in the localtime struct indicates DST status. When active, time.altzone provides the correct UTC offset.

This is useful for applications that need to adjust behavior based on DST.

Timezone Conversion with DST

This example shows how to convert UTC timestamps to local time considering the appropriate timezone offset (standard or DST).

timezone_conversion.py
import time

def utc_to_local(utc_timestamp):
    # Get local time struct to check DST
    local_time = time.localtime(utc_timestamp)
    
    # Use appropriate offset based on DST
    offset = time.altzone if local_time.tm_isdst > 0 else time.timezone
    
    # Apply offset (note: altzone/timezone are west of UTC)
    local_timestamp = utc_timestamp - offset
    return time.localtime(local_timestamp)

# Current UTC time
utc_now = time.time()
local_now = utc_to_local(utc_now)

print("UTC time:", time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime(utc_now)))
print("Local time:", time.strftime('%Y-%m-%d %H:%M:%S', local_now))

This function automatically selects the correct offset (altzone or timezone) based on whether DST is active for the given timestamp.

The conversion accounts for the fact that these offsets are west of UTC, requiring subtraction to convert to local time.

Timezone-Aware Time Difference

This example calculates time differences between locations considering their DST offsets using time.altzone.

time_difference.py
import time

def get_local_offset():
    # Returns current local offset in hours
    lt = time.localtime()
    offset = time.altzone if lt.tm_isdst > 0 else time.timezone
    return offset / 3600

def time_difference(hours_ahead):
    local_offset = get_local_offset()
    remote_offset = local_offset + hours_ahead
    
    print(f"Local timezone offset: {local_offset:.1f} hours from UTC")
    print(f"Remote timezone offset: {remote_offset:.1f} hours from UTC")
    print(f"Time difference: {hours_ahead} hours")

# Calculate difference with a location 3 hours ahead
time_difference(3)

This demonstrates calculating time differences considering the local DST status. The function uses altzone when DST is active for accurate results.

This pattern is useful for applications dealing with multiple timezones.

Displaying Timezone Information

This example creates a comprehensive timezone information display using time.altzone and related functions.

timezone_info.py
import time

def display_timezone_info():
    local_time = time.localtime()
    is_dst = local_time.tm_isdst > 0
    
    print("\nCurrent Timezone Information")
    print("===========================")
    print(f"Current local time: {time.strftime('%Y-%m-%d %H:%M:%S', local_time)}")
    print(f"Daylight Savings Time active: {'Yes' if is_dst else 'No'}")
    
    if is_dst:
        print(f"\nDST Timezone (altzone):")
        print(f"Offset: {time.altzone} seconds")
        print(f"Hours from UTC: {time.altzone/3600:.1f}")
    else:
        print(f"\nStandard Timezone (timezone):")
        print(f"Offset: {time.timezone} seconds")
        print(f"Hours from UTC: {time.timezone/3600:.1f}")
    
    print(f"\nTimezone name: {time.tzname[is_dst]}")
    print(f"UTC time: {time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime())}")

display_timezone_info()

This function provides a complete overview of the current timezone situation, including whether DST is active and the corresponding UTC offset.

The tzname tuple provides the timezone names for standard and DST.

Handling Timezone in File Timestamps

This example shows how to properly handle file timestamps considering the local timezone and DST using time.altzone.

file_timestamps.py
import time
import os

def get_local_filetime(filepath):
    mtime = os.path.getmtime(filepath)
    local_time = time.localtime(mtime)
    
    # Determine correct offset
    offset = time.altzone if local_time.tm_isdst > 0 else time.timezone
    
    # Format with timezone info
    time_str = time.strftime('%Y-%m-%d %H:%M:%S', local_time)
    tz_str = f"UTC{offset/3600:+.1f}"
    
    return f"{time_str} {tz_str}"

# Example usage
file_path = __file__  # Use current script as example
print(f"File last modified: {get_local_filetime(file_path)}")

This function provides accurate local file timestamps including the proper timezone offset, adjusting for DST when applicable.

The :+.1f format ensures the timezone offset shows with a sign.

Best Practices

Source References

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all Python tutorials.