Skip to content

Commit

Permalink
[#322] Fix seq for timezone-aware start value (#353)
Browse files Browse the repository at this point in the history
* [issue-322] seq increment datetime matching tz-awareness of start date

- This also introduces a test case (additional assert in existing test
  method) to cover the regression

* [issue-322] Update CHANGELOG
  • Loading branch information
timjklein36 committed Oct 14, 2022
1 parent 370406f commit b90253b
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
### Added

### Changed
- Fixed a bug with `seq` being passed a tz-aware start value [PR #353](https://github.com/model-bakers/model_bakery/pull/353)

### Removed

Expand Down
3 changes: 2 additions & 1 deletion model_bakery/utils.py
Expand Up @@ -82,7 +82,8 @@ def seq(value, increment_by=1, start=None, suffix=None):
date = value

# convert to epoch time
start = (date - datetime.datetime(1970, 1, 1)).total_seconds()
epoch_datetime = datetime.datetime(1970, 1, 1, tzinfo=date.tzinfo)
start = (date - epoch_datetime).total_seconds()
increment_by = increment_by.total_seconds()
for n in itertools.count(increment_by, increment_by):
series_date = tz_aware(datetime.datetime.utcfromtimestamp(start + n))
Expand Down
10 changes: 10 additions & 0 deletions tests/test_utils.py
Expand Up @@ -146,6 +146,7 @@ def test_datetime(self, settings, use_tz):
settings.USE_TZ = use_tz
tzinfo = datetime.timezone.utc if use_tz else None

# Starting with tz-unaware (naive) datetime
sequence = seq(
datetime.datetime(2021, 2, 11, 15, 39, 58, 457698),
increment_by=datetime.timedelta(hours=3),
Expand All @@ -160,6 +161,15 @@ def test_datetime(self, settings, use_tz):
2021, 2, 12, 00, 39, 58, 457698
).replace(tzinfo=tzinfo)

# Starting with tz-aware datetime
sequence = seq(
datetime.datetime(2021, 2, 11, 15, 39, 58, 457698, tzinfo=tzinfo),
increment_by=datetime.timedelta(hours=3),
)
assert next(sequence) == datetime.datetime(
2021, 2, 11, 18, 39, 58, 457698
).replace(tzinfo=tzinfo)

@pytest.mark.parametrize(
"value",
[
Expand Down

0 comments on commit b90253b

Please sign in to comment.