arrow-py / arrow-py/arrow

incorrect time difference for times before and after DST switch

Open
#1,136 2 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
Python
Stars
9.1k
Forks
784
PR merge metrics
No merged PRs in 30d

Description

## Issue Description

The time difference between two localized times isn't the actual difference in seconds, but rather the naive difference.

Example:
```
import arrow

def test_dst():
# 1667725199 11/6/2022 1:59:59AM GMT-7 DST
# 1667725200 11/6/2022 1:00:00AM GMT-8
before_dst = arrow.get(1667725199, tzinfo='America/Los_Angeles')
print(before_dst)
after_dst = arrow.get(1667725200, tzinfo='America/Los_Angeles')
print(after_dst)
secs = (after_dst - before_dst).total_seconds()
print(secs)

test_dst()
```
results in:
```
2022-11-06T01:59:59-07:00
2022-11-06T01:00:00-08:00
-3599.0
```

would expect 1.0 instead.

Workaround use timestamp() instead:
```
def test_dst_using_epoch():
# 1667725199 11/6/2022 1:59:59AM GMT-7 DST
# 1667725200 11/6/2022 1:00:00AM GMT-8
before_dst = arrow.get(1667725199, tzinfo='America/Los_Angeles')
print(before_dst)
after_dst = arrow.get(1667725200, tzinfo='America/Los_Angeles')
print(after_dst)

secs = after_dst.timestamp() - before_dst.timestamp()
print(secs)

test_dst_using_epoch()
```

results in:
```
2022-11-06T01:59:59-07:00
2022-11-06T01:00:00-08:00
1.0
```

## System Info

- 🖥 **OS name and version**: CentOS 7
- 🐍 **Python version**: 3.8.11
- 🏹 **Arrow version**: 1.2.3

Contributor guide

No contributing guide indexed for this repository

Research direction

Start by reproducing the subtraction in the supplied test_dst example with Arrow 1.2.3 and inspect the implementation behind arrow.get and Arrow datetime subtraction. Done means the DST-boundary pair returns 1.0 seconds while timestamp subtraction remains correct, with a regression test covering the example.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.