Cannot use offset-aware datetime in Container logs
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 7.2k
- Forks
- 1.7k
- Avg merge
- 13d 8h
- Merged PRs (30d)
- 2
Description
Environment
OS: Ubuntu 18.04.5 LTS
docker-py version: 4.4.0
python version: 3.8.0
docker version: 19.03.13
Description
The SDK raises a TypeError when using an offset-aware datetime object as since argument to Container#logs.
.../docker/models/containers.py:306: in logs
return self.client.api.logs(self.id, **kwargs)
.../docker/utils/decorators.py:19: in wrapped
return f(self, resource_id, *args, **kwargs)
.../docker/api/container.py:845: in logs
params['since'] = utils.datetime_to_timestamp(since)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
dt = datetime.datetime(2020, 11, 30, 18, 24, 55, 938894, tzinfo=datetime.timezone.utc)
def datetime_to_timestamp(dt):
"""Convert a UTC datetime to a Unix timestamp"""
> delta = dt - datetime.utcfromtimestamp(0)
E TypeError: can't subtract offset-naive and offset-aware datetimes
.../docker/utils/utils.py:389: TypeError
The docs state it's possible to pass a datetime object as this argument, though they do not mention timezone offsets:
since (datetime or int) – Show logs since a given datetime or integer epoch (in seconds)
Similarly, the parameter until has the same issue, as it uses the same conversion function:
https://github.com/docker/docker-py/blob/1757c974fa3a05b0e9b783af85242b18df09d05d/docker/api/container.py#L860
Steps to reproduce
Having a started Container object try calling logs as below:
from datetime import datetime, timezone
from docker.models.containers import Container
started_container: Container
started_container.logs(since=datetime.now(timezone.utc))
The following works fine (as the datetime object is now offset-naive):
from datetime import datetime, timezone
from docker.models.containers import Container
started_container: Container
started_container.logs(since=datetime.utcnow())
However, this goes against the recommendation from datetime docs:
Warning: Because naive datetime objects are treated by many datetime methods as local times, it is preferred to use aware datetimes to represent times in UTC. As such, the recommended way to create an object representing the current time in UTC is by calling datetime.now(timezone.utc).
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with docker/utils/utils.py at datetime_to_timestamp, then inspect the logs entry point in docker/api/container.py where since and until are converted. Run the offset-aware reproduction from the issue and check related utility coverage. Done means both parameters accept offset-aware datetimes without the reported TypeError while existing inputs continue to work.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- docker, python
- Domain
- api, backend
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 45/100