python / python/cpython

Allow gzip mtime to be a datetime object

Open
#128,584 8 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

pending stdlib type-feature
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

Feature or enhancement

Proposal:

Currently, the mtime optional parameter of the gzip.GzipFile class and gzip.compress function can be an float representing a Unix timestamp, or None to use the current time. I propose allowing this to also be a datetime.datetime object. All of these would then be possible:

from datetime import datetime, timezone
import gzip
import zoneinfo

compress(data)  # compress using current time
compress(data, mtime=0)  # ignore the time
compress(data, mtime=1736247159)  # specify the time with a Unix timestamp
compress(data, mtime=datetime(2025, 1, 7, tzinfo=timezone.utc))  # specify time with a datetime object
compress(data, mtime=datetime(2025, 1, 7, tzinfo=zoneinfo.ZoneInfo(key='US/Eastern')))  # specify a different timezone

This avoids users needing to cast their datetime objects into timestamps before calling compress or creating a GzipFile instance, which can lead to mistakes.

I have an initial PR for this ready to go.

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

No response

Linked PRs
  • gh-128589

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Review the gzip.GzipFile and gzip.compress mtime interfaces described in the issue, then inspect linked PR gh-128589 to understand the current implementation and tests. Done means the proposed datetime support is covered consistently for both APIs, with behavior for timestamps, None, and timezone-aware values verified.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.