mirror of
https://github.com/kennethreitz/maya.git
synced 2026-06-05 23:00:18 +00:00
Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 080831519d | |||
| 43705d84d6 | |||
| 6521a7dbc5 | |||
| 1796fc1d40 |
@@ -73,6 +73,10 @@ Behold, datetimes for humans!
|
||||
>>> rand_day.timezone
|
||||
UTC
|
||||
|
||||
# Range of hours in a day:
|
||||
>>> maya.interval(start=maya.now(), end=maya.now().add(days=1), interval=60*60)
|
||||
<generator object intervals at 0x105ba5820>
|
||||
|
||||
☤ Why is this useful?
|
||||
---------------------
|
||||
|
||||
|
||||
@@ -11,7 +11,7 @@ warnings.simplefilter('ignore', ruamel.yaml.error.UnsafeLoaderWarning)
|
||||
|
||||
import email.utils
|
||||
import time
|
||||
from datetime import datetime as Datetime
|
||||
from datetime import timedelta, datetime as Datetime
|
||||
|
||||
import pytz
|
||||
import humanize
|
||||
@@ -285,3 +285,16 @@ def parse(string, day_first=False):
|
||||
"""
|
||||
dt = pendulum.parse(string, day_first=day_first)
|
||||
return MayaDT.from_datetime(dt)
|
||||
|
||||
def intervals(start, end, interval):
|
||||
"""Yields MayaDT objects between the start and end MayaDTs given, at a given interval (seconds or timedelta)."""
|
||||
|
||||
# Convert seconds into timedelta.
|
||||
if isinstance(interval, int):
|
||||
interval = timedelta(seconds=interval)
|
||||
|
||||
current_timestamp = start
|
||||
while current_timestamp.epoch < end.epoch:
|
||||
yield current_timestamp
|
||||
current_timestamp = current_timestamp.add(seconds=interval.seconds)
|
||||
|
||||
|
||||
+8
-2
@@ -79,7 +79,7 @@ def test_machine_parse():
|
||||
|
||||
def test_dt_tz_translation():
|
||||
d1 = maya.now().datetime()
|
||||
d2 = maya.now().datetime(to_timezone='US/Eastern')
|
||||
d2 = maya.now().datetime(to_timezone='EST')
|
||||
assert (d1.hour - d2.hour) % 24 == 5
|
||||
|
||||
|
||||
@@ -87,7 +87,7 @@ def test_dt_tz_naive():
|
||||
d1 = maya.now().datetime(naive=True)
|
||||
assert d1.tzinfo is None
|
||||
|
||||
d2 = maya.now().datetime(to_timezone='US/Eastern', naive=True)
|
||||
d2 = maya.now().datetime(to_timezone='EST', naive=True)
|
||||
assert d2.tzinfo is None
|
||||
assert (d1.hour - d2.hour) % 24 == 5
|
||||
|
||||
@@ -202,3 +202,9 @@ def test_comparison_operations():
|
||||
now > 1
|
||||
with pytest.raises(TypeError):
|
||||
now >= 1
|
||||
|
||||
def test_intervals():
|
||||
now = maya.now()
|
||||
tomorrow = now.add(days=1)
|
||||
|
||||
assert len(list(maya.intervals(now, tomorrow, 60*60))) == 24
|
||||
|
||||
Reference in New Issue
Block a user