mirror of
https://github.com/kennethreitz/maya.git
synced 2026-06-05 23:00:18 +00:00
Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 52ca9a1cc4 |
@@ -22,4 +22,3 @@ In chronological order:
|
||||
- Joshua Li <joshua.r.li.98@gmail.com> (`@JoshuaRLi <https://github.com/JoshuaRLi>`_)
|
||||
- Sébastien Eustace <sebastien@eustace.io> (`@sdispater <https://github.com/sdispater>`_)
|
||||
- Evan Mattiza <emattiza@gmail.com> (`@emattiza <https://github.com/emattiza>`_)
|
||||
- Dima Spivak <dima@spivak.ch> (`@dimaspivak <https://github.com/dimaspivak>`_)
|
||||
|
||||
+7
-17
@@ -10,6 +10,7 @@ Maya: Datetimes for Humans™
|
||||
.. image:: https://img.shields.io/badge/SayThanks-!-1EAEDB.svg
|
||||
:target: https://saythanks.io/to/kennethreitz
|
||||
|
||||
test
|
||||
|
||||
Datetimes are very frustrating to work with in Python, especially when dealing
|
||||
with different locales on different systems. This library exists to make the
|
||||
@@ -20,8 +21,6 @@ Datetimes should be interacted with via an API written for humans.
|
||||
|
||||
Maya is mostly built around the headaches and use-cases around parsing datetime data from websites.
|
||||
|
||||
.. image:: https://farm4.staticflickr.com/3702/33288285996_5b69d2b8f7_k_d.jpg
|
||||
|
||||
|
||||
☤ Basic Usage of Maya
|
||||
---------------------
|
||||
@@ -65,15 +64,6 @@ Behold, datetimes for humans!
|
||||
>>> rand_day = maya.when('2011-02-07', timezone='US/Eastern')
|
||||
<MayaDT epoch=1297036800.0>
|
||||
|
||||
# Maya speaks Python.
|
||||
>>> m = maya.MayaDT.from_datetime(datetime.utcnow())
|
||||
>>> print(m)
|
||||
Wed, 20 Sep 2017 17:24:32 GMT
|
||||
|
||||
>>> m = maya.MayaDT.from_struct(time.gmtime())
|
||||
>>> print(m)
|
||||
Wed, 20 Sep 2017 17:24:32 GMT
|
||||
|
||||
>>> rand_day.day
|
||||
7
|
||||
|
||||
@@ -85,13 +75,13 @@ Behold, datetimes for humans!
|
||||
UTC
|
||||
|
||||
# Range of hours in a day:
|
||||
>>> maya.intervals(start=maya.now(), end=maya.now().add(days=1), interval=60*60)
|
||||
>>> maya.interval(start=maya.now(), end=maya.now().add(days=1), interval=60*60)
|
||||
<generator object intervals at 0x105ba5820>
|
||||
|
||||
☤ Advanced Usage of Maya
|
||||
------------------------
|
||||
|
||||
In addition to timestamps, Maya also includes a wonderfuly powerful ``MayaInterval`` class, which represents a range of time (e.g. an event). With this class, you can perform a multitude of advanced calendar calculations with finesse and ease.
|
||||
In addition to timestamps, Maya also includes a wonderfuly powerful ``MayaInterval`` class, which represents a range of time (e.g. an event). With this class, you can perform a multitude of advanced calendar calculations with finese and ease.
|
||||
|
||||
For example:
|
||||
|
||||
@@ -105,7 +95,7 @@ For example:
|
||||
|
||||
>>> event = MayaInterval(start=event_start, end=event_end)
|
||||
|
||||
From here, there are a number of methods available to you, which you can use to compare this event to another event.
|
||||
From here, there a a number of methods available to you, which you can use to compare this event to another event.
|
||||
|
||||
|
||||
|
||||
@@ -124,7 +114,7 @@ From here, there are a number of methods available to you, which you can use to
|
||||
☤ What about Delorean, Arrow, & Pendulum?
|
||||
-----------------------------------------
|
||||
|
||||
All these project complement each other, and are friends. Pendulum, for example, helps power Maya's parsing.
|
||||
All these project complement eachother, 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.
|
||||
|
||||
@@ -136,9 +126,9 @@ I think these projects complement each-other, personally. Maya is great for pars
|
||||
☤ Installing Maya
|
||||
-----------------
|
||||
|
||||
Installation is easy, with `pipenv <http://pipenv.org/>`_::
|
||||
Installation is easy, with pip::
|
||||
|
||||
$ pipenv install maya
|
||||
$ pip install maya
|
||||
|
||||
✨🍰✨
|
||||
|
||||
|
||||
+9
-19
@@ -114,14 +114,14 @@ class MayaDT(object):
|
||||
return hash(int(self.epoch))
|
||||
|
||||
def __add__(self, duration):
|
||||
return self.add(seconds=_seconds_or_timedelta(duration).total_seconds())
|
||||
return self.add(seconds=seconds_or_timedelta(duration).total_seconds())
|
||||
|
||||
def __radd__(self, duration):
|
||||
return self + duration
|
||||
|
||||
def __sub__(self, duration):
|
||||
return self.subtract(
|
||||
seconds=_seconds_or_timedelta(duration).total_seconds())
|
||||
seconds=seconds_or_timedelta(duration).total_seconds())
|
||||
|
||||
def add(self, **kwargs):
|
||||
""""Returns a new MayaDT object with the given offsets."""
|
||||
@@ -147,9 +147,7 @@ class MayaDT(object):
|
||||
@property
|
||||
def local_timezone(self):
|
||||
"""Returns the name of the local timezone, for informational purposes."""
|
||||
if self._local_tz.zone in pytz.all_timezones:
|
||||
return self._local_tz.zone
|
||||
return self.timezone
|
||||
return self._local_tz.zone
|
||||
|
||||
@property
|
||||
def _local_tz(self):
|
||||
@@ -176,14 +174,6 @@ class MayaDT(object):
|
||||
def from_datetime(klass, dt):
|
||||
"""Returns MayaDT instance from datetime."""
|
||||
return klass(klass.__dt_to_epoch(dt))
|
||||
|
||||
@classmethod
|
||||
@validate_arguments_type_of_function(time.struct_time)
|
||||
def from_struct(klass, struct, timezone=pytz.UTC):
|
||||
"""Returns MayaDT instance from a 9-tuple struct"""
|
||||
struct_time = time.mktime(struct)
|
||||
dt = Datetime.fromtimestamp(struct_time, timezone)
|
||||
return klass(klass.__dt_to_epoch(dt))
|
||||
|
||||
@classmethod
|
||||
def from_iso8601(klass, iso8601_string):
|
||||
@@ -341,7 +331,7 @@ class MayaInterval(object):
|
||||
|
||||
# Convert duration to timedelta if seconds were provided.
|
||||
if duration:
|
||||
duration = _seconds_or_timedelta(duration)
|
||||
duration = seconds_or_timedelta(duration)
|
||||
|
||||
if not start:
|
||||
start = end - duration
|
||||
@@ -360,7 +350,7 @@ class MayaInterval(object):
|
||||
|
||||
def iso8601(self):
|
||||
"""Returns an ISO 8601 representation of the MayaInterval."""
|
||||
return '{0}/{1}'.format(self.start.iso8601(), self.end.iso8601())
|
||||
return '{0}/{1}'.format(self.start.iso6801, self.end.iso8601)
|
||||
|
||||
@classmethod
|
||||
def from_iso8601(cls, s):
|
||||
@@ -458,7 +448,7 @@ class MayaInterval(object):
|
||||
def split(self, duration, include_remainder=True):
|
||||
|
||||
# Convert seconds to timedelta, if appropriate.
|
||||
duration = _seconds_or_timedelta(duration)
|
||||
duration = seconds_or_timedelta(duration)
|
||||
|
||||
if duration <= timedelta(seconds=0):
|
||||
raise ValueError('cannot call split with a non-positive timedelta')
|
||||
@@ -475,7 +465,7 @@ class MayaInterval(object):
|
||||
"""Returns a quanitzed interval."""
|
||||
|
||||
# Convert seconds to timedelta, if appropriate.
|
||||
duration = _seconds_or_timedelta(duration)
|
||||
duration = seconds_or_timedelta(duration)
|
||||
timezone = pytz.timezone(timezone)
|
||||
|
||||
|
||||
@@ -613,7 +603,7 @@ def parse(string, day_first=False):
|
||||
return MayaDT.from_datetime(dt)
|
||||
|
||||
|
||||
def _seconds_or_timedelta(duration):
|
||||
def seconds_or_timedelta(duration):
|
||||
"""Returns `datetime.timedelta` object for the passed duration.
|
||||
|
||||
Keyword Arguments:
|
||||
@@ -632,7 +622,7 @@ def _seconds_or_timedelta(duration):
|
||||
def intervals(start, end, interval):
|
||||
"""Yields MayaDT objects between the start and end MayaDTs given, at a given interval (seconds or timedelta)."""
|
||||
|
||||
interval = _seconds_or_timedelta(interval)
|
||||
interval = seconds_or_timedelta(interval)
|
||||
|
||||
current_timestamp = start
|
||||
while current_timestamp.epoch < end.epoch:
|
||||
|
||||
@@ -39,7 +39,7 @@ packages = [
|
||||
|
||||
# About dict to store version and package info
|
||||
about = dict()
|
||||
with open(os.path.join(here, 'maya', '__version__.py'), 'r', encoding='utf-8') as f:
|
||||
with open(os.path.join(here, 'maya', '__version__.py'), 'r', 'utf-8') as f:
|
||||
exec(f.read(), about)
|
||||
|
||||
setup(
|
||||
|
||||
+4
-32
@@ -1,11 +1,8 @@
|
||||
import pytest
|
||||
import pytz
|
||||
import copy
|
||||
import time
|
||||
from datetime import date, timedelta, datetime as Datetime
|
||||
from datetime import timedelta
|
||||
|
||||
import maya
|
||||
from maya.core import _seconds_or_timedelta # import private function
|
||||
|
||||
|
||||
def test_rfc2822():
|
||||
@@ -65,18 +62,6 @@ def test_parse_iso8601():
|
||||
|
||||
assert expected == d.iso8601()
|
||||
|
||||
def test_struct():
|
||||
ts = time.gmtime()
|
||||
m = maya.MayaDT.from_struct(ts)
|
||||
dt = Datetime.fromtimestamp(time.mktime(ts), pytz.UTC)
|
||||
assert m._epoch != None
|
||||
assert m.datetime() == dt
|
||||
|
||||
ts = time.localtime()
|
||||
m = maya.MayaDT.from_struct(ts)
|
||||
dt = Datetime.fromtimestamp(time.mktime(ts), pytz.UTC)
|
||||
assert m._epoch != None
|
||||
assert m.datetime() == dt
|
||||
|
||||
def test_human_when():
|
||||
r1 = maya.when('yesterday')
|
||||
@@ -222,12 +207,12 @@ def test_comparison_operations():
|
||||
|
||||
def test_seconds_or_timedelta():
|
||||
# test for value in seconds
|
||||
assert _seconds_or_timedelta(1234) == timedelta(0, 1234)
|
||||
assert maya.seconds_or_timedelta(1234) == timedelta(0, 1234)
|
||||
# test for value as `datetime.timedelta`
|
||||
assert _seconds_or_timedelta(timedelta(0, 1234)) == timedelta(0, 1234)
|
||||
assert maya.seconds_or_timedelta(timedelta(0, 1234)) == timedelta(0, 1234)
|
||||
# test for invalid value
|
||||
with pytest.raises(TypeError):
|
||||
_seconds_or_timedelta('invalid interval')
|
||||
maya.seconds_or_timedelta('invalid interval')
|
||||
|
||||
|
||||
def test_intervals():
|
||||
@@ -253,16 +238,3 @@ def test_dunder_sub():
|
||||
now = maya.now()
|
||||
assert now - 1 == now.subtract(seconds=1)
|
||||
assert now - timedelta(seconds=1) == now.subtract(seconds=1)
|
||||
|
||||
|
||||
def test_core_local_timezone(monkeypatch):
|
||||
@property
|
||||
def mock_local_tz(self):
|
||||
class StaticTzInfo(object):
|
||||
zone = 'local'
|
||||
def __repr__(self):
|
||||
return "<StaticTzInfo 'local'>"
|
||||
return StaticTzInfo()
|
||||
monkeypatch.setattr(maya.MayaDT, '_local_tz', mock_local_tz)
|
||||
mdt = maya.MayaDT(0)
|
||||
assert mdt.local_timezone == 'UTC'
|
||||
|
||||
@@ -582,9 +582,3 @@ def test_interval_from_datetime():
|
||||
)
|
||||
assert interval3.start == start
|
||||
assert interval3.end == end
|
||||
|
||||
|
||||
def test_interval_iso8601():
|
||||
start = maya.when('11-17-11 08:09:10')
|
||||
interval = maya.MayaInterval(start=start, duration=1)
|
||||
assert interval.iso8601() == '2011-11-17T08:09:10Z/2011-11-17T08:09:11Z'
|
||||
|
||||
Reference in New Issue
Block a user