mirror of
https://github.com/kennethreitz/maya.git
synced 2026-06-05 23:00:18 +00:00
Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| f9d257876b |
@@ -3,6 +3,9 @@ python:
|
||||
- "2.7"
|
||||
- "3.6"
|
||||
- "3.7-dev"
|
||||
# Test PyPy versions because PyPy is awesome :)
|
||||
- "pypy2.7"
|
||||
- "pypy3.6"
|
||||
|
||||
matrix:
|
||||
allow_failures:
|
||||
|
||||
@@ -24,4 +24,3 @@ In chronological order:
|
||||
- Evan Mattiza <emattiza@gmail.com> (`@emattiza <https://github.com/emattiza>`_)
|
||||
- Dima Spivak <dima@spivak.ch> (`@dimaspivak <https://github.com/dimaspivak>`_)
|
||||
- Tom Barron <tusculum@gmail.com> (`@dtbarron <https://github.com/tbarron>`_)
|
||||
- Alex Ward <alxwrd@gmail.com> (`@alxwrd <https://github.com/alxwrd>`_)
|
||||
|
||||
@@ -1,8 +1,3 @@
|
||||
[[source]]
|
||||
name = "pypi"
|
||||
url = "https://pypi.org/simple"
|
||||
verify_ssl = true
|
||||
|
||||
[dev-packages]
|
||||
freezegun = "*"
|
||||
pytest = "*"
|
||||
|
||||
+1
-6
@@ -26,7 +26,6 @@ Art by `Sam Flores
|
||||
<https://www.instagram.com/samagram12/>`_ (Photo by `Kenneth Reitz
|
||||
<https://www.instagram.com/kennethreitz/>`_).
|
||||
|
||||
If you're interested in financially supporting Kenneth Reitz open source, consider `visiting this link <https://cash.me/$KennethReitz>`_. Your support helps tremendously with sustainability of motivation, as Open Source is no longer part of my day job.
|
||||
|
||||
☤ Basic Usage of Maya
|
||||
---------------------
|
||||
@@ -78,10 +77,6 @@ Behold, datetimes for humans!
|
||||
>>> m = maya.MayaDT.from_struct(time.gmtime())
|
||||
>>> print(m)
|
||||
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
|
||||
7
|
||||
@@ -138,7 +133,7 @@ From here, there are a number of methods available to you, which you can use to
|
||||
☤ What about Delorean, Arrow, & Pendulum?
|
||||
-----------------------------------------
|
||||
|
||||
All these projects complement each other, and are friends. Pendulum, for example, helps power Maya's parsing.
|
||||
All these project complement each other, and are friends. Pendulum, for example, helps power Maya's parsing.
|
||||
|
||||
Arrow, for example, is a fantastic library, but isn't what I wanted in a datetime library. In many ways, it's better than Maya for certain things. In some ways, in my opinion, it's not.
|
||||
|
||||
|
||||
+1
-1
@@ -1 +1 @@
|
||||
__version__ = '0.6.0'
|
||||
__version__ = '0.4.1'
|
||||
|
||||
+11
-56
@@ -14,7 +14,6 @@ import pendulum
|
||||
import snaptime
|
||||
from tzlocal import get_localzone
|
||||
from dateutil.relativedelta import relativedelta
|
||||
from dateparser.languages.loader import default_loader
|
||||
|
||||
from .compat import cmp, comparable
|
||||
|
||||
@@ -258,14 +257,6 @@ 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.
|
||||
@@ -278,7 +269,7 @@ class MayaDT(object):
|
||||
|
||||
def rfc3339(self):
|
||||
"""Returns an RFC 3339 representation of the MayaDT."""
|
||||
return self.datetime().strftime("%Y-%m-%dT%H:%M:%S.%f")[:-5] + "Z"
|
||||
return self.datetime().strftime("%Y-%m-%dT%H:%M:%S.%f")[:-4] + "Z"
|
||||
|
||||
# Properties
|
||||
# ----------
|
||||
@@ -332,38 +323,15 @@ class MayaDT(object):
|
||||
|
||||
# Human Slang Extras
|
||||
# ------------------
|
||||
def slang_date(self, locale="en"):
|
||||
""""Returns human slang representation of date.
|
||||
def slang_date(self):
|
||||
""""Returns human slang representation of date."""
|
||||
dt = self.datetime(naive=True, to_timezone=self.local_timezone)
|
||||
return humanize.naturaldate(dt)
|
||||
|
||||
Keyword Arguments:
|
||||
locale -- locale to translate to, e.g. 'fr' for french.
|
||||
(default: 'en' - English)
|
||||
"""
|
||||
dt = pendulum.instance(self.datetime())
|
||||
|
||||
try:
|
||||
return _translate(dt, locale)
|
||||
except KeyError:
|
||||
pass
|
||||
|
||||
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 slang_time(self):
|
||||
""""Returns human slang representation of time."""
|
||||
dt = self.datetime(naive=True, to_timezone=self.local_timezone)
|
||||
return humanize.naturaltime(dt)
|
||||
|
||||
|
||||
def utc_offset(time_struct=None):
|
||||
@@ -496,7 +464,7 @@ class MayaInterval(object):
|
||||
|
||||
try:
|
||||
end = parse(end)
|
||||
except (pendulum.parsing.exceptions.ParserError, TypeError) as e:
|
||||
except pendulum.parsing.exceptions.ParserError as e:
|
||||
end = cls.parse_iso8601_duration(end, start=start)
|
||||
|
||||
return cls(start=start, end=end)
|
||||
@@ -739,7 +707,7 @@ def when(string, timezone='UTC', prefer_dates_from='current_period'):
|
||||
return MayaDT.from_datetime(dt)
|
||||
|
||||
|
||||
def parse(string, timezone='UTC', day_first=False, year_first=True, strict=False):
|
||||
def parse(string, timezone='UTC', day_first=False, year_first=True):
|
||||
""""Returns a MayaDT instance for the machine-produced moment specified.
|
||||
|
||||
Powered by pendulum.
|
||||
@@ -754,14 +722,11 @@ def parse(string, timezone='UTC', day_first=False, year_first=True, strict=False
|
||||
between YDM and YMD. (default: False)
|
||||
year_first -- if true, the first value (e.g. 2016/05/01)
|
||||
is parsed as year (default: True)
|
||||
strict -- if False, allow pendulum to fall back on datetime parsing
|
||||
if pendulum's own parsing fails
|
||||
"""
|
||||
options = {}
|
||||
options['tz'] = timezone
|
||||
options['day_first'] = day_first
|
||||
options['year_first'] = year_first
|
||||
options['strict'] = strict
|
||||
|
||||
dt = pendulum.parse(str(string), **options)
|
||||
return MayaDT.from_datetime(dt)
|
||||
@@ -786,16 +751,6 @@ def _seconds_or_timedelta(duration):
|
||||
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):
|
||||
"""
|
||||
Yields MayaDT objects between the start and end MayaDTs given,
|
||||
|
||||
@@ -25,7 +25,7 @@ required = [
|
||||
'pytz',
|
||||
'dateparser>=0.7.0',
|
||||
'tzlocal',
|
||||
'pendulum>=2.0.2',
|
||||
'pendulum>=1.0, <=1.5.1',
|
||||
'snaptime'
|
||||
]
|
||||
|
||||
@@ -98,6 +98,7 @@ setup(
|
||||
'Programming Language :: Python :: 3.6',
|
||||
'Programming Language :: Python :: Implementation',
|
||||
'Programming Language :: Python :: Implementation :: CPython',
|
||||
'Programming Language :: Python :: Implementation :: PyPy',
|
||||
'Topic :: Software Development :: Libraries :: Python Modules'
|
||||
),
|
||||
cmdclass={
|
||||
|
||||
+2
-41
@@ -158,19 +158,9 @@ def test_slang_date():
|
||||
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():
|
||||
d = maya.when('1 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'
|
||||
assert d.slang_time() == 'an hour ago'
|
||||
|
||||
|
||||
@pytest.mark.parametrize("string,kwds,expected", [
|
||||
@@ -249,20 +239,13 @@ def test_datetime_to_timezone():
|
||||
assert dt.tzinfo.zone == 'US/Eastern'
|
||||
|
||||
|
||||
def test_rfc3339_epoch():
|
||||
def test_rfc3339():
|
||||
mdt = maya.when('2016-01-01')
|
||||
out = mdt.rfc3339()
|
||||
mdt2 = maya.MayaDT.from_rfc3339(out)
|
||||
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")
|
||||
def test_comparison_operations():
|
||||
now = maya.now()
|
||||
@@ -359,28 +342,6 @@ 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