Compare commits

...

8 Commits

Author SHA1 Message Date
Kenneth Reitz 4fd8baf3eb v0.1.5 2016-12-25 19:47:20 -05:00
kennethreitz 5c255d2682 Merge pull request #29 from moin18/maya_operations
comparision operation support and subtraction of MayaDT object
2016-12-24 14:14:33 -05:00
kennethreitz 702eaab906 Merge pull request #28 from moin18/contributors
contributor firendly repository
2016-12-24 13:26:40 -05:00
Moin 5bbe383061 comparision operation support and subtraction of MayaDT object 2016-12-24 22:39:47 +05:30
Moin d40e698fb9 contributor firendly repository 2016-12-24 21:22:47 +05:30
kennethreitz 327f057e63 Merge pull request #27 from moin18/master
'week' and 'weekday' as properties to 'MayaDT' object
2016-12-24 00:45:38 -05:00
Moin 470516146a 'week' and 'weekday' as properties to 'MayaDT' object 2016-12-24 03:33:37 +05:30
kennethreitz dbed0555db _EPOCH_START 2016-12-23 13:11:25 -05:00
5 changed files with 111 additions and 3 deletions
+19
View File
@@ -0,0 +1,19 @@
# Contributions to the maya project
## Creator & Maintainer
- Kenneth Reitz <me@kennethreitz.org> `@kennethreitz <https://github.com/kennethreitz>`_
## Contributors
In chronological order:
- Adam Nelson <adam@varud.com> (`@adamn <https://github.com/adamn>`_)
- Timo Furrer <tuxtimo@gmail.com> (`@timofurrer <https://github.com/timofurrer>`_)
- Moinuddin Quadri <moin18@gmail.com> (`@moin18 <https://github.com/moin18>`_)
- Grigouze <grigouze@yahoo.fr> (`@grigouze <https://github.com/grigouze>`_)
- Tzu-ping Chung <uranusjr@gmail.com> (`@uranusjr <https://github.com/uranusjr>`_)
- aaronjeline (`@aaronjeline <https://github.com/aaronjeline>`_)
- jerry2yu (`@jerry2yu <https://github.com/jerry2yu>`_)
+12
View File
@@ -100,3 +100,15 @@ Installation is easy, with pip::
----------
`Say Thanks <https://saythanks.io/to/kennethreitz>`_!
How to Contribute
-----------------
#. Check for open issues or open a fresh issue to start a discussion around a feature idea or a bug.
#. Fork `the repository`_ on GitHub to start making your changes to the **master** branch (or branch off of it).
#. Write a test which shows that the bug was fixed or that the feature works as expected.
#. Send a pull request and bug the maintainer until it gets merged and published. :) Make sure to add yourself to AUTHORS_.
.. _`the repository`: http://github.com/kennethreitz/maya
.. _AUTHORS: https://github.com/kennethreitz/maya/blob/master/AUTHORS.rst
+54 -2
View File
@@ -20,7 +20,21 @@ import iso8601
import dateutil.parser
from tzlocal import get_localzone
EPOCH_START = (1970, 1, 1)
_EPOCH_START = (1970, 1, 1)
def validate_type_mayadt(func):
"""
Decorator to validate all the arguments to function
are of type `MayaDT`
"""
def inner(*args, **kwargs):
for arg in args + tuple(kwargs.values()):
if not isinstance(arg, MayaDT):
raise ValueError("Operation allowed only on object of type '{}'".format(MayaDT.__name__))
return func(*args, **kwargs)
return inner
class MayaDT(object):
"""The Maya Datetime object."""
@@ -36,6 +50,35 @@ class MayaDT(object):
"""Return's the datetime's format"""
return format(self.datetime(), *args, **kwargs)
@validate_type_mayadt
def __sub__(self, maya_dt):
return MayaDT(self._epoch - maya_dt._epoch)
@validate_type_mayadt
def __eq__(self, maya_dt):
return self._epoch == maya_dt._epoch
@validate_type_mayadt
def __ne__(self, maya_dt):
return not self.__eq__(maya_dt)
@validate_type_mayadt
def __lt__(self, maya_dt):
return self._epoch < maya_dt._epoch
@validate_type_mayadt
def __le__(self, maya_dt):
return self.__lt__(maya_dt) or self.__eq__(maya_dt)
@validate_type_mayadt
def __gt__(self, maya_dt):
return self._epoch > maya_dt._epoch
@validate_type_mayadt
def __ge__(self, maya_dt):
return self.__gt__(maya_dt) or self.__eq__(maya_dt)
# Timezone Crap
# -------------
@@ -67,7 +110,7 @@ class MayaDT(object):
if dt.tzinfo is None:
dt = dt.replace(tzinfo=pytz.utc)
epoch_start = Datetime(*EPOCH_START, tzinfo=pytz.timezone('UTC'))
epoch_start = Datetime(*_EPOCH_START, tzinfo=pytz.timezone('UTC'))
return (dt - epoch_start).total_seconds()
# Importers
@@ -140,6 +183,15 @@ class MayaDT(object):
def day(self):
return self.datetime().day
@property
def week(self):
return self.datetime().isocalendar()[1]
@property
def weekday(self):
"""Return the day of the week as an integer. Monday is 1 and Sunday is 7"""
return self.datetime().isoweekday()
@property
def hour(self):
return self.datetime().hour
+1 -1
View File
@@ -35,7 +35,7 @@ required = [
setup(
name='maya',
version='0.1.4',
version='0.1.5',
description='Datetimes for Humans.',
long_description= '\n' + read('README.rst'),
author='Kenneth Reitz',
+25
View File
@@ -1,5 +1,6 @@
import pytest
from datetime import datetime
import copy
import maya
@@ -94,3 +95,27 @@ def test_datetime_to_timezone():
dt = maya.when('2016-01-01').datetime(to_timezone='US/Eastern')
assert dt.tzinfo.zone == 'US/Eastern'
def test_comparison_operations():
now = maya.now()
now_copy = copy.deepcopy(now)
tomorrow = maya.when('tomorrow')
assert (now == now_copy) is True
assert (now == tomorrow) is False
assert (now != now_copy) is False
assert (now != tomorrow) is True
assert (now < now_copy) is False
assert (now < tomorrow) is True
assert (now <= now_copy) is True
assert (now <= tomorrow) is True
assert (now > now_copy) is False
assert (now > tomorrow) is False
assert (now >= now_copy) is True
assert (now >= tomorrow) is False