Skip to content

Commit

Permalink
Merge pull request #186 from cognifloyd/snap_tz
Browse files Browse the repository at this point in the history
Add timezone aware snap modifiers
  • Loading branch information
timofurrer committed May 20, 2020
2 parents ff2a417 + d0d3b01 commit 0a7061c
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
5 changes: 5 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ Behold, datetimes for humans!
>>> dt.snap('@d+3h').rfc2822()
'Mon, 21 Feb 1994 03:00:00 GMT'
# snap modifiers within a timezone
>>> dt = maya.when('Mon, 21 Feb 1994 21:21:42 GMT')
>>> dt.snap_tz('+3h@d', 'Australia/Perth').rfc2822()
'Mon, 21 Feb 1994 16:00:00 GMT'
☤ Advanced Usage of Maya
------------------------

Expand Down
11 changes: 11 additions & 0 deletions src/maya/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,17 @@ def snap(self, instruction):
"""
return self.from_datetime(snaptime.snap(self.datetime(), instruction))

def snap_tz(self, instruction, in_timezone):
"""
Returns a new MayaDT object modified by the given instruction.
The modifications happen in the given timezone.
Powered by snaptime. See https://github.com/zartstrom/snaptime
for a complete documentation about the snaptime instructions.
"""
dt_tz = self.datetime(to_timezone=in_timezone)
return self.from_datetime(snaptime.snap_tz(dt_tz, instruction, dt_tz.tzinfo))

# Timezone Crap
# -------------
@property
Expand Down
20 changes: 20 additions & 0 deletions tests/test_maya.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,3 +383,23 @@ def test_snaptime(when_str, snap_str, expected_when):
dt = dt.snap(snap_str)
# then
assert dt == maya.when(expected_when)


@pytest.mark.parametrize(
"when_str,snap_str,timezone,expected_when",
[
(
"Mon, 21 Feb 1994 21:21:42 GMT",
"@d",
"Australia/Perth",
"Mon, 21 Feb 1994 16:00:00 GMT",
)
],
)
def test_snaptime_tz(when_str, snap_str, timezone, expected_when):
# given
dt = maya.when(when_str)
# when
dt = dt.snap_tz(snap_str, timezone)
# then
assert dt == maya.when(expected_when)

0 comments on commit 0a7061c

Please sign in to comment.