Mailing List Archive

Struggling to understand timedelta rpesentation when applying an offset for an hour earlier - why is days = -1?
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??
--
https://mail.python.org/mailman/listinfo/python-list
Re: Struggling to understand timedelta rpesentation when applying an offset for an hour earlier - why is days = -1? [ In reply to ]
On Tue, 31 Aug 2021 03:59:52 -0700 (PDT), dcs3spp via Python-list
<python-list@python.org> declaimed the following:


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

Read the documentation...
https://docs.python.org/3/library/datetime.html#timedelta-objects


>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??

"""
and days, seconds and microseconds are then normalized so that the
representation is unique, with

0 <= microseconds < 1000000

0 <= seconds < 3600*24 (the number of seconds in one day)

-999999999 <= days <= 999999999
"""

Note that microseconds and seconds are ALWAYS normalized to be a positive
integer. So your input on -3600 is normalized to +82800 from the previous
day.


--
Wulfraed Dennis Lee Bieber AF6VN
wlfraed@ix.netcom.com http://wlfraed.microdiversity.freeddns.org/

--
https://mail.python.org/mailman/listinfo/python-list
Re: Struggling to understand timedelta rpesentation when applying an offset for an hour earlier - why is days = -1? [ In reply to ]
On Wed, Sep 1, 2021 at 1:55 AM dcs3spp via Python-list
<python-list@python.org> wrote:
>
> 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??

It's consistent with modulo arithmetic:

>>> x = -3600
>>> x // 86400
-1
>>> x % 86400
82800

>>> help(datetime.timedelta)
...
| ----------------------------------------------------------------------
| Data descriptors defined here:
|
| days
| Number of days.
|
| microseconds
| Number of microseconds (>= 0 and less than 1 second).
|
| seconds
| Number of seconds (>= 0 and less than 1 day).
|
| ----------------------------------------------------------------------

The sub-day portions are guaranteed to be zero or above, meaning that
a small negative offset is described as "a day ago, plus 23 hours"
rather than "an hour ago". It's the exact same thing, though.

If you would prefer to see ALL components negative, just negate the
timedelta and then negate each component; that will give you an
equivalent timedelta.

>>> datetime.timedelta(seconds=-3600)
datetime.timedelta(days=-1, seconds=82800)
>>> -datetime.timedelta(seconds=-3600)
datetime.timedelta(seconds=3600)
>>> datetime.timedelta(seconds=-3600-86400)
datetime.timedelta(days=-2, seconds=82800)
>>> -datetime.timedelta(seconds=-3600-86400)
datetime.timedelta(days=1, seconds=3600)

Hope that explains it!

ChrisA
--
https://mail.python.org/mailman/listinfo/python-list