mirror of
https://github.com/kennethreitz/maya.git
synced 2026-06-05 23:00:18 +00:00
Compare commits
16 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| cc698817b4 | |||
| 9964e9305e | |||
| d87236a30d | |||
| d44a1dd2f6 | |||
| ed5d72b34c | |||
| 9c1e94f956 | |||
| 6755968f69 | |||
| 3760af2c41 | |||
| df09625e74 | |||
| 2d848bd62a | |||
| a99cddf76f | |||
| 1b432d6626 | |||
| f09d6eec63 | |||
| 62a6283200 | |||
| 9ef43a29d9 | |||
| a2bff42439 |
@@ -24,3 +24,4 @@ In chronological order:
|
|||||||
- Evan Mattiza <emattiza@gmail.com> (`@emattiza <https://github.com/emattiza>`_)
|
- Evan Mattiza <emattiza@gmail.com> (`@emattiza <https://github.com/emattiza>`_)
|
||||||
- Dima Spivak <dima@spivak.ch> (`@dimaspivak <https://github.com/dimaspivak>`_)
|
- Dima Spivak <dima@spivak.ch> (`@dimaspivak <https://github.com/dimaspivak>`_)
|
||||||
- Tom Barron <tusculum@gmail.com> (`@dtbarron <https://github.com/tbarron>`_)
|
- Tom Barron <tusculum@gmail.com> (`@dtbarron <https://github.com/tbarron>`_)
|
||||||
|
- Alex Ward <alxwrd@gmail.com> (`@alxwrd <https://github.com/alxwrd>`_)
|
||||||
|
|||||||
@@ -78,6 +78,10 @@ Behold, datetimes for humans!
|
|||||||
>>> print(m)
|
>>> print(m)
|
||||||
Wed, 20 Sep 2017 17:24:32 GMT
|
Wed, 20 Sep 2017 17:24:32 GMT
|
||||||
|
|
||||||
|
>>> m = maya.MayaDT(time.time())
|
||||||
|
>>> print(m)
|
||||||
|
Wed, 20 Sep 2017 17:24:32 GMT
|
||||||
|
|
||||||
>>> rand_day.day
|
>>> rand_day.day
|
||||||
7
|
7
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -1 +1 @@
|
|||||||
__version__ = '0.4.2'
|
__version__ = '0.5.0'
|
||||||
|
|||||||
+44
-9
@@ -14,6 +14,7 @@ import pendulum
|
|||||||
import snaptime
|
import snaptime
|
||||||
from tzlocal import get_localzone
|
from tzlocal import get_localzone
|
||||||
from dateutil.relativedelta import relativedelta
|
from dateutil.relativedelta import relativedelta
|
||||||
|
from dateparser.languages.loader import default_loader
|
||||||
|
|
||||||
from .compat import cmp, comparable
|
from .compat import cmp, comparable
|
||||||
|
|
||||||
@@ -277,7 +278,7 @@ class MayaDT(object):
|
|||||||
|
|
||||||
def rfc3339(self):
|
def rfc3339(self):
|
||||||
"""Returns an RFC 3339 representation of the MayaDT."""
|
"""Returns an RFC 3339 representation of the MayaDT."""
|
||||||
return self.datetime().strftime("%Y-%m-%dT%H:%M:%S.%f")[:-4] + "Z"
|
return self.datetime().strftime("%Y-%m-%dT%H:%M:%S.%f")[:-5] + "Z"
|
||||||
|
|
||||||
# Properties
|
# Properties
|
||||||
# ----------
|
# ----------
|
||||||
@@ -331,15 +332,39 @@ class MayaDT(object):
|
|||||||
|
|
||||||
# Human Slang Extras
|
# Human Slang Extras
|
||||||
# ------------------
|
# ------------------
|
||||||
def slang_date(self):
|
def slang_date(self, locale="en"):
|
||||||
""""Returns human slang representation of date."""
|
""""Returns human slang representation of date.
|
||||||
dt = self.datetime(naive=True, to_timezone=self.local_timezone)
|
|
||||||
return humanize.naturaldate(dt)
|
|
||||||
|
|
||||||
def slang_time(self):
|
Keyword Arguments:
|
||||||
""""Returns human slang representation of time."""
|
locale -- locale to translate to, e.g. 'fr' for french.
|
||||||
dt = self.datetime(naive=True, to_timezone=self.local_timezone)
|
(default: 'en' - English)
|
||||||
return humanize.naturaltime(dt)
|
"""
|
||||||
|
dt = pendulum.instance(self.datetime())
|
||||||
|
|
||||||
|
try:
|
||||||
|
return _translate(dt, locale)
|
||||||
|
except KeyError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
dt.set_formatter("alternative")
|
||||||
|
delta = humanize.time.abs_timedelta(
|
||||||
|
timedelta(seconds=(self.epoch - now().epoch)))
|
||||||
|
|
||||||
|
format_string = "DD MMM"
|
||||||
|
if delta.days >= 365:
|
||||||
|
format_string += " YYYY"
|
||||||
|
|
||||||
|
return dt.format(format_string, locale=locale).title()
|
||||||
|
|
||||||
|
def slang_time(self, locale="en"):
|
||||||
|
""""Returns human slang representation of time.
|
||||||
|
|
||||||
|
Keyword Arguments:
|
||||||
|
locale -- locale to translate to, e.g. 'fr' for french.
|
||||||
|
(default: 'en' - English)
|
||||||
|
"""
|
||||||
|
dt = self.datetime()
|
||||||
|
return pendulum.instance(dt).diff_for_humans(locale=locale)
|
||||||
|
|
||||||
|
|
||||||
def utc_offset(time_struct=None):
|
def utc_offset(time_struct=None):
|
||||||
@@ -759,6 +784,16 @@ def _seconds_or_timedelta(duration):
|
|||||||
return dt_timedelta
|
return dt_timedelta
|
||||||
|
|
||||||
|
|
||||||
|
def _translate(dt, target_locale):
|
||||||
|
en = default_loader.get_locale("en")
|
||||||
|
target = default_loader.get_locale(target_locale)
|
||||||
|
naturaldate = humanize.naturaldate(dt)
|
||||||
|
|
||||||
|
base = en.translate(naturaldate, settings=dateparser.conf.settings)
|
||||||
|
|
||||||
|
return target.info["relative-type"][base][-1]
|
||||||
|
|
||||||
|
|
||||||
def intervals(start, end, interval):
|
def intervals(start, end, interval):
|
||||||
"""
|
"""
|
||||||
Yields MayaDT objects between the start and end MayaDTs given,
|
Yields MayaDT objects between the start and end MayaDTs given,
|
||||||
|
|||||||
+19
-2
@@ -158,9 +158,19 @@ def test_slang_date():
|
|||||||
assert d.slang_date() == 'tomorrow'
|
assert d.slang_date() == 'tomorrow'
|
||||||
|
|
||||||
|
|
||||||
|
def test_slang_date_locale():
|
||||||
|
d = maya.when('tomorrow')
|
||||||
|
assert d.slang_date(locale='fr') == 'demain'
|
||||||
|
|
||||||
|
|
||||||
def test_slang_time():
|
def test_slang_time():
|
||||||
d = maya.when('1 hour ago')
|
d = maya.when('1 hour ago')
|
||||||
assert d.slang_time() == 'an hour ago'
|
assert d.slang_time() == '1 hour ago'
|
||||||
|
|
||||||
|
|
||||||
|
def test_slang_time_locale():
|
||||||
|
d = maya.when('1 hour ago')
|
||||||
|
assert d.slang_time(locale='de') == 'vor 1 Stunde'
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize("string,kwds,expected", [
|
@pytest.mark.parametrize("string,kwds,expected", [
|
||||||
@@ -239,13 +249,20 @@ def test_datetime_to_timezone():
|
|||||||
assert dt.tzinfo.zone == 'US/Eastern'
|
assert dt.tzinfo.zone == 'US/Eastern'
|
||||||
|
|
||||||
|
|
||||||
def test_rfc3339():
|
def test_rfc3339_epoch():
|
||||||
mdt = maya.when('2016-01-01')
|
mdt = maya.when('2016-01-01')
|
||||||
out = mdt.rfc3339()
|
out = mdt.rfc3339()
|
||||||
mdt2 = maya.MayaDT.from_rfc3339(out)
|
mdt2 = maya.MayaDT.from_rfc3339(out)
|
||||||
assert mdt.epoch == mdt2.epoch
|
assert mdt.epoch == mdt2.epoch
|
||||||
|
|
||||||
|
|
||||||
|
def test_rfc3339_format():
|
||||||
|
rfc3339 = maya.MayaDT.rfc3339(maya.when('2016-01-01T12:03:03Z'))
|
||||||
|
# it's important that the string has got a "max 1-digit millis" fragment
|
||||||
|
# as per https://tools.ietf.org/html/rfc3339#section-5.6
|
||||||
|
assert rfc3339 == '2016-01-01T12:03:03.0Z'
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.usefixtures("frozen_now")
|
@pytest.mark.usefixtures("frozen_now")
|
||||||
def test_comparison_operations():
|
def test_comparison_operations():
|
||||||
now = maya.now()
|
now = maya.now()
|
||||||
|
|||||||
Reference in New Issue
Block a user