Compare commits

...

24 Commits

Author SHA1 Message Date
kennethreitz 79d017fdcf Merge pull request #100 from amalmurali47/patch-1
Fix typos and correct spelling.
2017-09-21 10:15:42 -04:00
Amal Murali 7331d0d855 Fix typos and correct spelling. 2017-09-21 18:02:15 +05:30
kennethreitz 9c90c0534a Merge pull request #99 from Miserlou/master
Fixes #98 - add support for time struct with test and docs
2017-09-20 13:45:03 -04:00
Rich Jones b9c501b0e4 Fixes #98 - add support for time struct with test and docs 2017-09-20 19:32:37 +02:00
kennethreitz 3a71ff0174 Update README.rst 2017-09-05 13:01:06 -04:00
kennethreitz 43cc1d5946 Merge pull request #97 from dimaspivak/master
Fix MayaInterval.iso8601()
2017-09-02 15:14:51 -04:00
Dima Spivak aecb643beb Add Dima Spivak to AUTHORS.rst 2017-09-02 11:50:27 -07:00
Dima Spivak fa966900e1 Fix MayaInterval.iso8601() 2017-09-02 11:47:32 -07:00
kennethreitz 892e589ef2 Update README.rst 2017-08-31 03:13:35 -04:00
kennethreitz 0f795738c0 Update README.rst 2017-08-31 03:13:16 -04:00
kennethreitz b333489081 Merge pull request #96 from stsievert/patch-1
Add XKCD #1883 to README
2017-08-30 13:12:11 -04:00
Scott Sievert 17a450eb5d Add XKCD #1883 to README 2017-08-30 12:06:08 -05:00
kennethreitz 8cd2158567 Merge pull request #87 from endast/patch-1
Update README.rst
2017-08-26 15:32:11 -04:00
kennethreitz 883b1b9b92 Merge pull request #90 from ibigpapa/master
Fix for issue #89
2017-08-26 15:31:15 -04:00
kennethreitz 414df5f3f5 Merge pull request #93 from timofurrer/patch-1
Fix typo in README. Closes #92
2017-07-28 13:46:33 -04:00
Timo Furrer 1694ed7cf8 Fix typo in README. Closes #92 2017-07-28 10:00:06 +02:00
Troy Harrison cbe9f6bae7 Adds test for issue #89 2017-06-28 14:27:57 -05:00
Troy Harrison 8bdd5c65fa Fixes issue #89 2017-06-28 14:26:54 -05:00
Magnus Wahlberg 4d96d06d70 Update README.rst
The example for intervals is missing an "s"

>>> import maya
>>> maya.interval(start=maya.now(), end=maya.now().add(days=1), interval=60*60)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'maya' has no attribute 'interval'
>>> maya.intervals(start=maya.now(), end=maya.now().add(days=1), interval=60*60)
<generator object intervals at 0x106acc0a0>
2017-06-23 08:44:20 +02:00
kennethreitz bbdb9b8762 Merge pull request #79 from robcarrington/fix-setup
Fixed setup.py bug by turning encoding into a keyword argument. Encod…
2017-05-30 22:12:00 -04:00
Robert Carrington 28ecad81bd Fixed setup.py bug by turning encoding into a keyword argument. Encoding parameter was being passed in the position belonging to the buffering parameter 2017-05-30 21:03:35 -05:00
kennethreitz c56c552184 Merge pull request #78 from moin18/seconds_or_timedelta_private
"seconds_or_timedelta" as private
2017-05-30 10:51:31 -04:00
Moin d5e4853886 updated test to use private _seconds_or_timedelta 2017-05-30 20:03:20 +05:30
Moin 28b3a849a9 declare seconds_or_timedelta as private function 2017-05-30 19:58:33 +05:30
6 changed files with 76 additions and 20 deletions
+1
View File
@@ -22,3 +22,4 @@ 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>`_)
+17 -6
View File
@@ -20,6 +20,8 @@ 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
---------------------
@@ -63,6 +65,15 @@ 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
@@ -74,13 +85,13 @@ Behold, datetimes for humans!
UTC
# Range of hours in a day:
>>> maya.interval(start=maya.now(), end=maya.now().add(days=1), interval=60*60)
>>> maya.intervals(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 finese 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 finesse and ease.
For example:
@@ -94,7 +105,7 @@ For example:
>>> event = MayaInterval(start=event_start, end=event_end)
From here, there a a number of methods available to you, which you can use to compare this event to another event.
From here, there are a number of methods available to you, which you can use to compare this event to another event.
@@ -113,7 +124,7 @@ From here, there a a number of methods available to you, which you can use to co
☤ What about Delorean, Arrow, & Pendulum?
-----------------------------------------
All these project complement eachother, 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.
@@ -125,9 +136,9 @@ I think these projects complement each-other, personally. Maya is great for pars
☤ Installing Maya
-----------------
Installation is easy, with pip::
Installation is easy, with `pipenv <http://pipenv.org/>`_::
$ pip install maya
$ pipenv install maya
✨🍰✨
+19 -9
View File
@@ -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,7 +147,9 @@ class MayaDT(object):
@property
def local_timezone(self):
"""Returns the name of the local timezone, for informational purposes."""
return self._local_tz.zone
if self._local_tz.zone in pytz.all_timezones:
return self._local_tz.zone
return self.timezone
@property
def _local_tz(self):
@@ -174,6 +176,14 @@ 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):
@@ -331,7 +341,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
@@ -350,7 +360,7 @@ class MayaInterval(object):
def iso8601(self):
"""Returns an ISO 8601 representation of the MayaInterval."""
return '{0}/{1}'.format(self.start.iso6801, self.end.iso8601)
return '{0}/{1}'.format(self.start.iso8601(), self.end.iso8601())
@classmethod
def from_iso8601(cls, s):
@@ -448,7 +458,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')
@@ -465,7 +475,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)
@@ -603,7 +613,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:
@@ -622,7 +632,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:
+1 -1
View File
@@ -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', 'utf-8') as f:
with open(os.path.join(here, 'maya', '__version__.py'), 'r', encoding='utf-8') as f:
exec(f.read(), about)
setup(
+32 -4
View File
@@ -1,8 +1,11 @@
import pytest
import pytz
import copy
from datetime import timedelta
import time
from datetime import date, timedelta, datetime as Datetime
import maya
from maya.core import _seconds_or_timedelta # import private function
def test_rfc2822():
@@ -62,6 +65,18 @@ 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')
@@ -207,12 +222,12 @@ def test_comparison_operations():
def test_seconds_or_timedelta():
# test for value in seconds
assert maya.seconds_or_timedelta(1234) == timedelta(0, 1234)
assert _seconds_or_timedelta(1234) == timedelta(0, 1234)
# test for value as `datetime.timedelta`
assert maya.seconds_or_timedelta(timedelta(0, 1234)) == timedelta(0, 1234)
assert _seconds_or_timedelta(timedelta(0, 1234)) == timedelta(0, 1234)
# test for invalid value
with pytest.raises(TypeError):
maya.seconds_or_timedelta('invalid interval')
_seconds_or_timedelta('invalid interval')
def test_intervals():
@@ -238,3 +253,16 @@ 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'
+6
View File
@@ -582,3 +582,9 @@ 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'