Skip to content

Commit 8af1e2e

Browse files
authored
Fix now() behavior for DST (#483)
* Fix now() behavior for DST * Upgrade pip before installing dependencies
1 parent 36e8726 commit 8af1e2e

File tree

5 files changed

+110
-11
lines changed

5 files changed

+110
-11
lines changed

.github/workflows/tests.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ jobs:
4444
with:
4545
path: .venv
4646
key: venv-${{ runner.os }}-${{ steps.full-python-version.outputs.version }}-${{ hashFiles('**/poetry.lock') }}
47+
- name: Upgrade pip
48+
run: |
49+
source $HOME/.poetry/env
50+
poetry run python -m pip install pip -U
4751
- name: Install dependencies
4852
run: |
4953
source $HOME/.poetry/env
@@ -86,6 +90,10 @@ jobs:
8690
with:
8791
path: .venv
8892
key: venv-${{ runner.os }}-${{ steps.full-python-version.outputs.version }}-fix-${{ hashFiles('**/poetry.lock') }}
93+
- name: Upgrade pip
94+
run: |
95+
source $HOME/.poetry/env
96+
poetry run python -m pip install pip -U
8997
- name: Install dependencies
9098
run: |
9199
source $HOME/.poetry/env
@@ -127,6 +135,10 @@ jobs:
127135
with:
128136
path: .venv
129137
key: venv-${{ runner.os }}-${{ steps.full-python-version.outputs.version }}-${{ hashFiles('**/poetry.lock') }}
138+
- name: Upgrade pip
139+
run: |
140+
$env:Path += ";$env:Userprofile\.poetry\bin"
141+
poetry run python -m pip install pip -U
130142
- name: Install dependencies
131143
run: |
132144
$env:Path += ";$env:Userprofile\.poetry\bin"

pendulum/__init__.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,17 @@ def now(tz=None): # type: (Optional[Union[str, _Timezone]]) -> DateTime
216216
tz = _safe_timezone(tz)
217217
dt = tz.convert(dt)
218218

219-
return instance(dt, tz)
219+
return DateTime(
220+
dt.year,
221+
dt.month,
222+
dt.day,
223+
dt.hour,
224+
dt.minute,
225+
dt.second,
226+
dt.microsecond,
227+
tzinfo=dt.tzinfo,
228+
fold=dt.fold if _HAS_FOLD else 0,
229+
)
220230

221231

222232
def today(tz="local"): # type: (Union[str, _Timezone]) -> DateTime

poetry.lock

Lines changed: 38 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ pytest = "^4.6"
3232
pytest-cov = "^2.5"
3333
pytz = ">=2018.3"
3434
babel = "^2.5"
35-
cleo = "^0.7.5"
35+
cleo = "^0.8.1"
3636
tox = "^3.0"
3737
black = { version = "^19.3b0", markers = "python_version >= '3.6' and python_version < '4.0' and implementation_name != 'pypy'" }
3838
isort = { version = "^4.3.21", markers = "python_version >= '3.6' and python_version < '4.0'" }
@@ -41,6 +41,7 @@ mkdocs = { version = "^1.0", python = "^3.5" }
4141
pymdown-extensions = "^6.0"
4242
pygments = "^2.2"
4343
markdown-include = "^0.5.1"
44+
freezegun = "^0.3.15"
4445

4546

4647
[tool.isort]
@@ -62,6 +63,7 @@ known_third_party = [
6263
"babel",
6364
"cleo",
6465
"dateutil",
66+
"freezegun",
6567
"pytzdata",
6668
]
6769

tests/datetime/test_construct.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
from datetime import datetime
44

55
from dateutil import tz
6+
from freezegun import freeze_time
67

78
import pendulum
89
import pytest
910
import pytz
1011

1112
from pendulum import DateTime
1213
from pendulum.tz import timezone
14+
from pendulum.utils._compat import PY36
1315

1416
from ..conftest import assert_datetime
1517

@@ -102,6 +104,50 @@ def test_now():
102104
assert now.hour != in_paris.hour
103105

104106

107+
@pytest.mark.skipif(not PY36, reason="fold attribute only present in Python 3.6+")
108+
@freeze_time("2016-03-27 00:30:00")
109+
def test_now_dst_off():
110+
utc = pendulum.now("UTC")
111+
in_paris = pendulum.now("Europe/Paris")
112+
in_paris_from_utc = utc.in_tz("Europe/Paris")
113+
assert in_paris.hour == 1
114+
assert not in_paris.is_dst()
115+
assert in_paris.isoformat() == in_paris_from_utc.isoformat()
116+
117+
118+
@pytest.mark.skipif(not PY36, reason="fold attribute only present in Python 3.6+")
119+
@freeze_time("2016-03-27 01:30:00")
120+
def test_now_dst_transitioning_on():
121+
utc = pendulum.now("UTC")
122+
in_paris = pendulum.now("Europe/Paris")
123+
in_paris_from_utc = utc.in_tz("Europe/Paris")
124+
assert in_paris.hour == 3
125+
assert in_paris.is_dst()
126+
assert in_paris.isoformat() == in_paris_from_utc.isoformat()
127+
128+
129+
@pytest.mark.skipif(not PY36, reason="fold attribute only present in Python 3.6+")
130+
@freeze_time("2016-10-30 00:30:00")
131+
def test_now_dst_on():
132+
utc = pendulum.now("UTC")
133+
in_paris = pendulum.now("Europe/Paris")
134+
in_paris_from_utc = utc.in_tz("Europe/Paris")
135+
assert in_paris.hour == 2
136+
assert in_paris.is_dst()
137+
assert in_paris.isoformat() == in_paris_from_utc.isoformat()
138+
139+
140+
@pytest.mark.skipif(not PY36, reason="fold attribute only present in Python 3.6+")
141+
@freeze_time("2016-10-30 01:30:00")
142+
def test_now_dst_transitioning_off():
143+
utc = pendulum.now("UTC")
144+
in_paris = pendulum.now("Europe/Paris")
145+
in_paris_from_utc = utc.in_tz("Europe/Paris")
146+
assert in_paris.hour == 2
147+
assert not in_paris.is_dst()
148+
assert in_paris.isoformat() == in_paris_from_utc.isoformat()
149+
150+
105151
def test_now_with_fixed_offset():
106152
now = pendulum.now(6)
107153

0 commit comments

Comments
 (0)