Struggling to understand timedelta rpesentation when applying an offset for an hour earlier - why is days = -1?

dcs3spp simonppears at googlemail.com
Tue Aug 31 06:59:52 EDT 2021


Hi,

I wonder if anyone can help....

I am struggling to understand the representation of timedelta when used in conjunction with astimezone. 

Given the code below, in a python interactive interpreter, I am trying to calculate a resultant datetime an hour earlier from a UTC datetime....

```bash
    >>> dt = datetime(2021, 8, 22, 23, 59, 31, tzinfo=timezone.utc)
    >>> hour_before=dt.astimezone(timezone(-timedelta(seconds=3600)))
    >>> hour_before
    datetime.datetime(2021, 8, 22, 22, 59, 31, tzinfo=datetime.timezone(datetime.timedelta(days=-1, seconds=82800)))
```

I cannot understand why the resultant datetime.timedelta is days=-1, seconds=82800 (23 hours) .

Why is it not an hour earlier as seconds=-3600? Why is days = -1 when the resultant calculated date is the same, year, day, month??


More information about the Python-list mailing list