mirror of
https://github.com/kennethreitz/maya.git
synced 2026-06-05 23:00:18 +00:00
Compare commits
19 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 5525beda31 | |||
| eeb07d46db | |||
| 4056d1a9aa | |||
| 7a750a1cff | |||
| bc06315abd | |||
| 7cc767781a | |||
| 8468dd2ead | |||
| 046f005ca7 | |||
| cd0b2300d7 | |||
| d3ddb39d9d | |||
| 3c8fe4478c | |||
| a0983132bb | |||
| 9612d70707 | |||
| 363fae1aaf | |||
| be2cd96132 | |||
| 771a2b6ce2 | |||
| 8e644f655a | |||
| 6903f6eb63 | |||
| b05ca8707c |
@@ -22,6 +22,10 @@ Maya is mostly built around the headaches and use-cases around parsing datetime
|
||||
|
||||
.. image:: https://farm4.staticflickr.com/3702/33288285996_5b69d2b8f7_k_d.jpg
|
||||
|
||||
Art by `Sam Flores
|
||||
<https://www.instagram.com/samagram12/>`_ (Photo by `Kenneth Reitz
|
||||
<https://www.instagram.com/kennethreitz/>`_).
|
||||
|
||||
|
||||
☤ Basic Usage of Maya
|
||||
---------------------
|
||||
|
||||
+1
-1
@@ -1 +1 @@
|
||||
__version__ = '0.3.4'
|
||||
__version__ = '0.4.2'
|
||||
|
||||
+17
-5
@@ -152,7 +152,7 @@ class MayaDT(object):
|
||||
|
||||
def subtract_date(self, **kwargs):
|
||||
"""Returns a timedelta object with the duration between the dates"""
|
||||
return timedelta(self.epoch - kwargs['dt'].epoch)
|
||||
return timedelta(seconds=self.epoch - kwargs['dt'].epoch)
|
||||
|
||||
def snap(self, instruction):
|
||||
"""
|
||||
@@ -257,6 +257,14 @@ class MayaDT(object):
|
||||
dt = dt.replace(tzinfo=self._tz)
|
||||
return dt
|
||||
|
||||
def local_datetime(self):
|
||||
"""Returns a local timezone-aware datetime object
|
||||
|
||||
It's the same as:
|
||||
mayaDt.datetime(to_timezone=mayaDt.local_timezone)
|
||||
"""
|
||||
return self.datetime(to_timezone=self.local_timezone, naive=False)
|
||||
|
||||
def iso8601(self):
|
||||
"""Returns an ISO 8601 representation of the MayaDT."""
|
||||
# Get a timezone-naive datetime.
|
||||
@@ -675,7 +683,7 @@ def now():
|
||||
return MayaDT(epoch=epoch)
|
||||
|
||||
|
||||
def when(string, timezone='UTC', prefer_past=False):
|
||||
def when(string, timezone='UTC', prefer_dates_from='current_period'):
|
||||
""""Returns a MayaDT instance for the human moment specified.
|
||||
|
||||
Powered by dateparser. Useful for scraping websites.
|
||||
@@ -686,16 +694,20 @@ def when(string, timezone='UTC', prefer_past=False):
|
||||
Keyword Arguments:
|
||||
string -- string to be parsed
|
||||
timezone -- timezone referenced from (default: 'UTC')
|
||||
prefer_past -- prefer parsing ambiguous date as in the past
|
||||
prefer_dates_from -- what dates are prefered when `string` is ambigous.
|
||||
options are 'past', 'future', and 'current_period'
|
||||
(default: 'current_period'). see: [1]
|
||||
|
||||
Reference:
|
||||
[1] dateparser.readthedocs.io/en/latest/usage.html#handling-incomplete-dates
|
||||
"""
|
||||
settings = {
|
||||
'TIMEZONE': timezone,
|
||||
'RETURN_AS_TIMEZONE_AWARE': True,
|
||||
'TO_TIMEZONE': 'UTC',
|
||||
'PREFER_DATES_FROM': prefer_dates_from,
|
||||
}
|
||||
if prefer_past:
|
||||
settings['PREFER_DATES_FROM'] = 'past'
|
||||
|
||||
dt = dateparser.parse(string, settings=settings)
|
||||
if dt is None:
|
||||
raise ValueError('invalid datetime input specified.')
|
||||
|
||||
@@ -25,7 +25,7 @@ required = [
|
||||
'pytz',
|
||||
'dateparser>=0.7.0',
|
||||
'tzlocal',
|
||||
'pendulum>=1.0',
|
||||
'pendulum>=1.0, <=1.5.1',
|
||||
'snaptime'
|
||||
]
|
||||
|
||||
|
||||
+65
-12
@@ -1,5 +1,6 @@
|
||||
import copy
|
||||
import time
|
||||
import calendar
|
||||
from datetime import timedelta, datetime as Datetime
|
||||
|
||||
import pytz
|
||||
@@ -191,16 +192,46 @@ def test_parse(string, kwds, expected):
|
||||
|
||||
@pytest.mark.usefixtures("frozen_now")
|
||||
def test_when_past():
|
||||
next_month = str(maya.now().add(months=1).month)
|
||||
this_year = maya.now().year
|
||||
last_year = this_year - 1
|
||||
future_date = maya.when(next_month)
|
||||
past_date = maya.when(next_month, prefer_past=True)
|
||||
assert future_date.year == this_year
|
||||
if next_month == '1':
|
||||
assert past_date.year == this_year
|
||||
else:
|
||||
assert past_date.year == last_year
|
||||
two_days_away = maya.now().add(days=2)
|
||||
|
||||
past_date = maya.when(
|
||||
two_days_away.slang_date(),
|
||||
prefer_dates_from='past')
|
||||
|
||||
assert past_date < maya.now()
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("frozen_now")
|
||||
def test_when_future():
|
||||
two_days_away = maya.now().add(days=2)
|
||||
|
||||
future_date = maya.when(
|
||||
two_days_away.slang_date(),
|
||||
prefer_dates_from='future')
|
||||
|
||||
assert future_date > maya.now()
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("frozen_now")
|
||||
def test_when_past_day_name():
|
||||
two_days_away = maya.now().add(days=2)
|
||||
|
||||
past_date = maya.when(
|
||||
calendar.day_name[two_days_away.weekday],
|
||||
prefer_dates_from='past')
|
||||
|
||||
assert past_date < maya.now()
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("frozen_now")
|
||||
def test_when_future_day_name():
|
||||
two_days_away = maya.now().add(days=2)
|
||||
|
||||
future_date = maya.when(
|
||||
calendar.day_name[two_days_away.weekday],
|
||||
prefer_dates_from='future')
|
||||
|
||||
assert future_date > maya.now()
|
||||
|
||||
|
||||
def test_datetime_to_timezone():
|
||||
@@ -289,8 +320,8 @@ def test_dunder_sub():
|
||||
def test_mayaDT_sub():
|
||||
now = maya.now()
|
||||
then = now.add(days=1)
|
||||
assert then - now == timedelta(24 * 60 * 60)
|
||||
assert now - then == timedelta(-24 * 60 * 60)
|
||||
assert then - now == timedelta(seconds=24 * 60 * 60)
|
||||
assert now - then == timedelta(seconds=-24 * 60 * 60)
|
||||
|
||||
|
||||
def test_core_local_timezone(monkeypatch):
|
||||
@@ -311,6 +342,28 @@ def test_core_local_timezone(monkeypatch):
|
||||
assert mdt.local_timezone == 'UTC'
|
||||
|
||||
|
||||
def test_getting_datetime_for_local_timezone(monkeypatch):
|
||||
|
||||
@property
|
||||
def mock_local_tz(self):
|
||||
class StaticTzInfo(object):
|
||||
zone = 'Europe/Zurich'
|
||||
|
||||
def __repr__(self):
|
||||
return "<StaticTzInfo 'Europe/Zurich'>"
|
||||
|
||||
return StaticTzInfo()
|
||||
|
||||
monkeypatch.setattr(maya.MayaDT, '_local_tz', mock_local_tz)
|
||||
|
||||
d = maya.parse('1994-02-21T12:00:00+05:30')
|
||||
|
||||
dt = pytz.timezone('Europe/Zurich').localize(
|
||||
Datetime(1994, 2, 21, 7, 30))
|
||||
|
||||
assert d.local_datetime() == dt
|
||||
|
||||
|
||||
@pytest.mark.parametrize("when_str,snap_str,expected_when", [
|
||||
('Mon, 21 Feb 1994 21:21:42 GMT', '@d',
|
||||
'Mon, 21 Feb 1994 00:00:00 GMT'),
|
||||
|
||||
Reference in New Issue
Block a user