Compare commits

...

94 Commits

Author SHA1 Message Date
kennethreitz 77bfce0bae rfc 3339, v0.1.7 2017-02-09 17:03:55 -05:00
kennethreitz 8aaa741793 v0.1.6 2017-01-24 12:46:15 -05:00
kennethreitz 5a442b3876 fix failing test 2017-01-01 17:36:42 -05:00
kennethreitz 4822487efe Merge pull request #46 from JoshuaRLi/update-authors
Update AUTHORS
2017-01-01 17:32:55 -05:00
Joshua Li e0ee61fd43 update AUTHORS 2017-01-01 16:42:18 -05:00
kennethreitz 08a654d523 v0.1.5 2016-12-30 18:59:32 -05:00
kennethreitz 0e260d761e Merge pull request #37 from moin18/decorator_remove
Type checks in comparison operators
2016-12-30 18:58:20 -05:00
Moin c9888a48e5 validate_class_type_arguments decorator 2016-12-29 16:23:53 +05:30
Moin 21743de790 test for AttributeError in MayaDT comparison operators 2016-12-29 14:42:59 +05:30
Moin 09c3e352e4 Merge branch 'master' of https://github.com/kennethreitz/maya into decorator_remove 2016-12-29 14:38:47 +05:30
Moin ba9e35525c modified maya_dt obj check decorator 2016-12-29 14:36:05 +05:30
kennethreitz 562403da0f Merge pull request #38 from moin18/slang_date
use local timezone to get the slang date
2016-12-28 15:07:07 -05:00
kennethreitz e96db15c0e Merge pull request #43 from timofurrer/python3
Support Python 3.3, 3.4, 3.5, 3.6
2016-12-28 15:06:11 -05:00
kennethreitz a3b86b6025 Merge pull request #44 from timofurrer/strproto
Implement str protocol for human readable datetime printing
2016-12-28 15:05:36 -05:00
Timo Furrer 2f47366cbf Implement str protocol for human readable datetime printing 2016-12-28 09:12:48 +01:00
Timo Furrer c17a358c66 Support Python 3.3, 3.4, 3.5, 3.6
ruamel.ordereddict is only needed for python 2.7 anyway.
So we should not depend on it in requirements.txt
2016-12-28 08:59:30 +01:00
kennethreitz b8300192c9 Merge pull request #39 from moin18/test_change
properties test at single place
2016-12-28 02:24:48 -05:00
kennethreitz d8f01a7789 Merge pull request #41 from JoshuaRLi/pip-win-fix
Fix file encoding issues preventing successful pip installation on Windows
2016-12-28 02:24:21 -05:00
kennethreitz 199a290b1d Merge pull request #42 from JoshuaRLi/test-dt-tz-fix
Fix erroneous test logic in test_dt_tz_translation, test_dt_tz_naive
2016-12-28 02:23:53 -05:00
Joshua Li 82d20ad1dc fix erroneous test logic with dt_tz_naive and dt_tz_translation 2016-12-27 22:32:39 -05:00
Joshua Li d6b8ac54ad README.rst is now read correctly with utf-8 encoding into the setup long_description, fixes python3 windows cmd installs via pip 2016-12-27 21:24:02 -05:00
Moin edfb9baae4 use local timezone to get the slang date 2016-12-27 04:56:40 +05:30
Moin 4fb95666ba properties test at single place 2016-12-27 04:51:43 +05:30
Moin 03243e2194 Type checks in comparison operators
Related to issue #33
2016-12-27 01:24:49 +05:30
kennethreitz 6420cc91d9 Merge pull request #30 from timofurrer/cleanup/fix-authors
Fix rst headings in AUTHORS file
2016-12-26 11:47:38 -05:00
kennethreitz 40eeea252d Merge pull request #31 from timofurrer/cleanup/remove-exec-flag
Remove executable flag from python files
2016-12-26 11:47:27 -05:00
kennethreitz 2452b30d96 Merge pull request #32 from timofurrer/cleanup/remove-substraction
Remove sub protocol
2016-12-26 11:47:08 -05:00
kennethreitz 4e009dc51a Merge pull request #34 from timofurrer/coverage
Add unit tests for weekday and week
2016-12-26 11:46:02 -05:00
kennethreitz d535739413 Merge pull request #35 from timofurrer/cleanup/tests
Make unit tests more valuable
2016-12-26 11:45:32 -05:00
Timo Furrer 299951bd0b Make unit tests more valuable 2016-12-26 12:01:54 +01:00
Timo Furrer 84555b1f5c Remove sub protocol 2016-12-26 11:17:46 +01:00
Timo Furrer 915337d2b4 Add unit tests for weekday and week 2016-12-26 11:10:02 +01:00
Timo Furrer e31f018a23 Remove executable flag from python files 2016-12-26 10:59:35 +01:00
Timo Furrer 0b64766b62 Fix rst headings in AUTHORS file 2016-12-26 10:56:13 +01: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
kennethreitz 2fce84195e v0.1.4 2016-12-21 13:24:53 -05:00
kennethreitz 7ab9e48d45 Merge pull request #25 from Factr/master
Added .gitignore
2016-12-20 22:19:15 -05:00
Adam Nelson ee1a8e2438 Added .gitignore
From https://github.com/github/gitignore/blob/master/Python.gitignore with Pycharm exclusion for .idea/
2016-12-20 16:08:29 -05:00
kennethreitz b0e91d6c0e Merge pull request #24 from Factr/master
Added tzlocal to setup.py
2016-12-20 14:30:11 -05:00
Adam Nelson bce5d13401 Merge branch 'master' of github.com:Factr/maya into HEAD 2016-12-20 13:36:19 -05:00
Adam Nelson b4a23d668d Added tzlocal as setup requirement 2016-12-20 13:34:49 -05:00
kennethreitz 90051da96d fix maya link to travis 2016-12-20 13:09:48 -05:00
kennethreitz 8f0ff0d68c skip 3 builds 2016-12-20 13:08:55 -05:00
kennethreitz fd1262a8e8 shields 2016-12-20 12:57:11 -05:00
kennethreitz 7558261dc8 basic travis file 2016-12-20 12:49:56 -05:00
kennethreitz 1407e688fd improve test name 2016-12-20 12:29:31 -05:00
kennethreitz 0c2b936111 cleanup tests 2016-12-20 12:27:16 -05:00
kennethreitz 9e72ef3d2f test for #19 2016-12-20 12:25:19 -05:00
kennethreitz eabba0b79e v0.1.3 2016-12-20 12:25:01 -05:00
kennethreitz 8af92bc33c fixes #19 2016-12-20 12:20:50 -05:00
kennethreitz 917420071f v0.1.2 2016-12-20 12:15:01 -05:00
kennethreitz 2bb430b91d make setup.py python 2/3 compatible 2016-12-20 12:14:48 -05:00
kennethreitz 967638228a -e . 2016-12-20 12:12:59 -05:00
kennethreitz 1ba2dac284 Merge pull request #23 from aaronjeline/master
setup.py fix
2016-12-20 12:12:04 -05:00
Aaron Eline 89a54abf1f Added fix from @joaoleveiga 2016-12-20 09:56:00 -05:00
Aaron Eline f119fe927b Fixed missing comma in setup.py 2016-12-20 09:53:51 -05:00
Adam Nelson 0efc489ba1 Added tzlocal as a requirement as well 2016-12-19 17:15:35 -05:00
kennethreitz 8b6ca639cb Merge pull request #21 from Factr/master
Python files executable, ruamel.yaml required
2016-12-19 13:29:32 -08:00
Adam Nelson 48c245a8ad Python files executable, ruamel.yaml required 2016-12-19 16:22:22 -05:00
kennethreitz 29a5b990fd day_first in tests 2016-12-19 13:31:03 -05:00
kennethreitz 69c88552d7 day_first 2016-12-19 13:02:47 -05:00
kennethreitz 2126c8f33e Merge pull request #20 from jerry2yu/master
Add datafirst parameter to parse method. Set datafirst true to parse …
2016-12-19 10:01:31 -08:00
kennethreitz dbc20b3026 Merge pull request #18 from harshit98/master
Update README.rst
2016-12-19 09:49:19 -08:00
Jerry Yu c521672f0e Add datafirst parameter to parse method. Set datafirst true to parse the first value in date (e.g. 01/05/2016) as the day 2016-12-19 13:00:18 -04:00
Harshit Prasad f756b13d4d Update README.rst 2016-12-19 21:01:07 +05:30
kennethreitz 2ce5a23e38 Merge pull request #16 from andrewthehan/typoReadme
Fix typo in README.rst
2016-12-18 21:37:03 -08:00
Andrew Han 605e15ae27 Fix typo in README.rst 2016-12-18 20:10:00 -08:00
kennethreitz 07a3e814a2 Update README.rst 2016-12-18 16:49:37 -05:00
kennethreitz 2e811d8689 Update README.rst 2016-12-18 16:47:28 -05:00
kennethreitz cbbd7d92c1 Update README.rst 2016-12-18 16:45:59 -05:00
kennethreitz d9e08034dd Update README.rst 2016-12-18 16:29:14 -05:00
kennethreitz e3b1eacb77 fix datetime formatting 2016-12-18 13:09:21 -05:00
kennethreitz be29d01160 Merge branch 'master' of github.com:kennethreitz/maya 2016-12-18 12:43:02 -05:00
kennethreitz 5e4853936b Merge pull request #8 from timofurrer/bugfix/format
Fix formatting MayaDT object
2016-12-18 09:41:58 -08:00
kennethreitz 7dbe450c28 Merge pull request #12 from djrobstep/bugfix/py-modules
my_modules -> py_modules
2016-12-18 09:41:29 -08:00
kennethreitz 3ce6734a5e Merge pull request #13 from grigouze/add_tests_for_coverage
Just add some tests to have full coverage
2016-12-18 09:41:12 -08:00
kennethreitz 6e2f530207 Merge pull request #7 from Kraymer/patch-1
Update README.rst (typo)
2016-12-18 09:40:04 -08:00
Grigouze c46d9874d4 Just add some tests to have full coverage
Now it's 💯 % coverage, i don't if it's good or bad but it was easy :)
2016-12-18 15:12:02 +01:00
Robert Lechte 87013d71e0 my_modules -> py_modules 2016-12-18 22:38:40 +11:00
Timo Furrer a562f48e24 Fix formatting MayaDT object 2016-12-18 10:59:22 +01:00
Fabrice Laporte 8fc07b3ab3 Update README.rst
Typo tomorrrow => tomorrow
2016-12-18 10:39:43 +01:00
kennethreitz 59064a5c50 Merge pull request #5 from uranusjr/master
Fix typos in README and add pip install instruction
2016-12-18 00:58:43 -08:00
Tzu-ping Chung 7ae84f3266 Add pip install instruction 2016-12-18 16:04:23 +08:00
Tzu-ping Chung cc668974b6 Fix typos 2016-12-18 16:03:35 +08:00
kennethreitz 6a4ddff215 Update README.rst 2016-12-18 02:48:45 -05:00
kennethreitz a0ab4fc3fb Update README.rst 2016-12-18 02:48:29 -05:00
kennethreitz c80ecad67b Update README.rst 2016-12-18 02:48:12 -05:00
kennethreitz 97842c01b3 Update README.rst 2016-12-18 02:47:57 -05:00
9 changed files with 392 additions and 41 deletions
+94
View File
@@ -0,0 +1,94 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
# C extensions
*.so
# Distribution / packaging
.Python
env/
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
*.egg-info/
.installed.cfg
*.egg
# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec
# Installer logs
pip-log.txt
pip-delete-this-directory.txt
# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*,cover
.hypothesis/
# Translations
*.mo
*.pot
# Django stuff:
*.log
local_settings.py
# Flask stuff:
instance/
.webassets-cache
# Scrapy stuff:
.scrapy
# Sphinx documentation
docs/_build/
# PyBuilder
target/
# Jupyter Notebook
.ipynb_checkpoints
# pyenv
.python-version
# celery beat schedule file
celerybeat-schedule
# dotenv
.env
# virtualenv
.venv/
venv/
ENV/
# Spyder project settings
.spyderproject
# Rope project settings
.ropeproject
# PyCharm
.idea/
+12
View File
@@ -0,0 +1,12 @@
language: python
python:
- "2.7"
- "3.3"
- "3.4"
- "3.5"
- "3.6"
# command to install dependencies
install: pip install -r requirements.txt
# command to run tests
script: make
+22
View File
@@ -0,0 +1,22 @@
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>`_)
- Joshua Li <joshua.r.li.98@gmail.com> (`@JoshuaRLi <https://github.com/JoshuaRLi>`_)
+11
View File
@@ -0,0 +1,11 @@
[packages]
humanize = "*"
pytz = "*"
dateparser = "*"
iso8601 = "*"
python-dateutil = "*"
"ruamel.yaml" = "*"
tzlocal = "*"
[dev-packages]
pytest = "*"
+44 -5
View File
@@ -1,6 +1,16 @@
Maya: Datetime for Humans™
==========================
.. image:: https://img.shields.io/pypi/v/maya.svg
:target: https://pypi.python.org/pypi/maya
.. image:: https://travis-ci.org/kennethreitz/maya.svg?branch=master
:target: https://travis-ci.org/kennethreitz/maya
.. image:: https://img.shields.io/badge/SayThanks.io-☼-1EAEDB.svg
:target: https://saythanks.io/to/kennethreitz
Datetimes are very frustrating to work with in Python, especially when dealing
with different locales on different systems. This library exists to make the
simple things **much** easier, while admitting that time is an illusion
@@ -8,6 +18,9 @@ simple things **much** easier, while admitting that time is an illusion
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.
☤ Basic Usage of Maya
---------------------
@@ -30,7 +43,7 @@ Behold, datetimes for humans!
>>> tomorrow.iso8601()
'2016-12-16T15:11:30.263350Z'
>>> tomorrrow.rfc2822()
>>> tomorrow.rfc2822()
'Fri, 16 Dec 2016 20:11:30 -0000'
>>> tomorrow.datetime()
@@ -52,8 +65,6 @@ Behold, datetimes for humans!
>>> rand_day.timezone
UTC
☤ Why is this useful?
---------------------
@@ -61,9 +72,20 @@ Behold, datetimes for humans!
- Complete symmetric import and export of both ISO 8601 and RFC 2822 datetime stamps.
- Fantastic parsing of both dates written for/by humans and machines (``maya.when()`` vs ``maya.parse()``).
- Support for human slang, both import and export (e.g. `an hour ago`).
- Datetimes can very easily be generated, with our without tzinfo attached.
- Datetimes can very easily be generated, with or without tzinfo attached.
- This library is based around epoch time, but dates before Jan 1 1970 are indeed supported, via negative integers.
- Maya never panics, and always carrys a towel.
- Maya never panics, and always carries a towel.
☤ What about Delorean, Arrow, & Pendulum?
-----------------------------------------
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.
I simply desire a sane API for datetimes that made sense to me for all the things I'd ever want to do—especially when dealing with timezone algebra. Arrow doesn't do all of the things I need (but it does a lot more!). Maya does do exactly what I need.
I think these projects complement each-other, personally. Maya is great for parsing websites. For example- Arrow supports floors and ceilings and spans of dates, which Maya does not at all.
☤ Installing Maya
-----------------
@@ -73,3 +95,20 @@ Installation is easy, with pip::
$ pip install maya
✨🍰✨
☤ Like it?
----------
`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
+82 -8
View File
@@ -20,7 +20,28 @@ import iso8601
import dateutil.parser
from tzlocal import get_localzone
EPOCH_START = (1970, 1, 1)
_EPOCH_START = (1970, 1, 1)
def validate_class_type_arguments(operator):
"""
Decorator to validate all the arguments to function
are of the type of calling class
"""
def inner(function):
def wrapper(self, *args, **kwargs):
for arg in args + tuple(kwargs.values()):
if not isinstance(arg, self.__class__):
raise TypeError('unorderable types: {}() {} {}()'.format(
type(self).__name__, operator, type(arg).__name__))
return function(self, *args, **kwargs)
return wrapper
return inner
class MayaDT(object):
"""The Maya Datetime object."""
@@ -32,9 +53,38 @@ class MayaDT(object):
def __repr__(self):
return '<MayaDT epoch={}>'.format(self._epoch)
def __str__(self):
return self.rfc2822()
def __format__(self, *args, **kwargs):
"""Return's the datetime's format"""
return self.datetime(*args, **kwargs)
return format(self.datetime(), *args, **kwargs)
@validate_class_type_arguments('==')
def __eq__(self, maya_dt):
return self._epoch == maya_dt._epoch
@validate_class_type_arguments('!=')
def __ne__(self, maya_dt):
return self._epoch != maya_dt._epoch
@validate_class_type_arguments('<')
def __lt__(self, maya_dt):
return self._epoch < maya_dt._epoch
@validate_class_type_arguments('<=')
def __le__(self, maya_dt):
return self._epoch <= maya_dt._epoch
@validate_class_type_arguments('>')
def __gt__(self, maya_dt):
return self._epoch > maya_dt._epoch
@validate_class_type_arguments('>=')
def __ge__(self, maya_dt):
return self._epoch >= maya_dt._epoch
# Timezone Crap
# -------------
@@ -67,7 +117,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
@@ -89,6 +139,11 @@ class MayaDT(object):
"""Returns MayaDT instance from rfc2822 string."""
return parse(string)
@staticmethod
def from_rfc3339(string):
"""Returns MayaDT instance from rfc3339 string."""
return parse(string)
# Exporters
# ---------
@@ -104,12 +159,16 @@ class MayaDT(object):
dt = self.datetime().astimezone(pytz.timezone(to_timezone))
else:
dt = Datetime.utcfromtimestamp(self._epoch)
dt.replace(tzinfo=self._tz)
# Strip the timezone info if requested to do so.
if naive:
return dt.replace(tzinfo=None)
else:
if dt.tzinfo is None:
dt = dt.replace(tzinfo=self._tz)
return dt.replace(tzinfo=self._tz)
return dt
def iso8601(self):
"""Returns an ISO 8601 representation of the MayaDT."""
@@ -121,6 +180,10 @@ class MayaDT(object):
"""Returns an RFC 2822 representation of the MayaDT."""
return email.utils.formatdate(self.epoch, usegmt=True)
def rfc3339(self):
"""Returns an RFC 3339 representation of the MayaDT."""
return self.datetime().strftime("%Y-%m-%dT%H:%M:%S.%f")[:-4]+"Z"
# Properties
# ----------
@@ -136,6 +199,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
@@ -161,7 +233,8 @@ class MayaDT(object):
def slang_date(self):
""""Returns human slang representation of date."""
return humanize.naturaldate(self.datetime())
dt = self.datetime(naive=True, to_timezone=self.local_timezone)
return humanize.naturaldate(dt)
def slang_time(self):
""""Returns human slang representation of time."""
@@ -196,13 +269,14 @@ def when(string, timezone='UTC'):
return MayaDT.from_datetime(dt)
def parse(string):
def parse(string, day_first=False):
""""Returns a MayaDT instance for the machine-produced moment specified.
Powered by dateutil. Accepts most known formats. Useful for working with data.
Keyword Arguments:
string -- string to be parsed
day_first -- if true, the first value (e.g. 01/05/2016) is parsed as day (default: False)
"""
dt = dateutil.parser.parse(string)
return MayaDT.from_datetime(dt)
dt = dateutil.parser.parse(string, dayfirst=day_first)
return MayaDT.from_datetime(dt)
-15
View File
@@ -1,15 +0,0 @@
dateparser==0.5.0
humanize==0.5.1
iso8601==0.1.11
jdatetime==1.8.1
py==1.4.32
pytest==3.0.5
python-dateutil==2.6.0
pytz==2016.10
regex==2016.11.21
ruamel.ordereddict==0.4.9
ruamel.yaml==0.13.4
six==1.10.0
typing==3.5.2.2
tzlocal==1.3
umalqurra==0.2
Regular → Executable
+19 -4
View File
@@ -3,9 +3,22 @@
import os
import sys
import codecs
from setuptools import setup
try:
# Python 3
from os import dirname
except ImportError:
# Python 2
from os.path import dirname
here = os.path.abspath(dirname(__file__))
with codecs.open(os.path.join(here, 'README.rst'), encoding='utf-8') as f:
long_description = '\n' + f.read()
if sys.argv[-1] == "publish":
os.system("python setup.py sdist bdist_wheel upload")
@@ -16,18 +29,20 @@ required = [
'pytz',
'dateparser',
'iso8601',
'python-dateutil'
'python-dateutil',
'ruamel.yaml',
'tzlocal'
]
setup(
name='maya',
version='0.1.0',
version='0.1.7',
description='Datetimes for Humans.',
long_description=open('README.rst').read(),
long_description=long_description,
author='Kenneth Reitz',
author_email='me@kennethreitz.com',
url='https://github.com/kennethreitz/maya',
my_modules=['maya'],
py_modules=['maya'],
install_requires=required,
license='MIT',
classifiers=(
+108 -9
View File
@@ -1,15 +1,20 @@
import pytest
import copy
import maya
def test_rfc2822():
r = maya.now().rfc2822()
r = maya.parse('February 21, 1994').rfc2822()
d = maya.MayaDT.from_rfc2822(r)
assert r == 'Mon, 21 Feb 1994 00:00:00 GMT'
assert r == d.rfc2822()
def test_iso8601():
r = maya.now().iso8601()
r = maya.parse('February 21, 1994').iso8601()
d = maya.MayaDT.from_iso8601(r)
assert r == '1994-02-21T00:00:00Z'
assert r == d.iso8601()
@@ -17,7 +22,8 @@ def test_human_when():
r1 = maya.when('yesterday')
r2 = maya.when('today')
assert r2.day - r1.day == 1
assert (r2.day - r1.day) in (1, -30, -29, -28, -27)
def test_machine_parse():
r1 = maya.parse('August 14, 2015')
@@ -30,7 +36,7 @@ def test_machine_parse():
def test_dt_tz_translation():
d1 = maya.now().datetime()
d2 = maya.now().datetime(to_timezone='US/Eastern')
assert d1.hour - d2.hour == 5
assert (d1.hour - d2.hour) % 24 == 5
def test_dt_tz_naive():
@@ -39,14 +45,49 @@ def test_dt_tz_naive():
d2 = maya.now().datetime(to_timezone='US/Eastern', naive=True)
assert d2.tzinfo is None
assert d1.hour - d2.hour == 5
assert (d1.hour - d2.hour) % 24 == 5
def test_random_date():
# Test properties for maya.when()
d1 = maya.when('11-17-11 08:09:10')
assert d1.year == 2011
assert d1.month == 11
assert d1.day == 17
assert d1.week == 46
assert d1.weekday == 4
assert d1.hour == 8
assert d1.minute == 9
assert d1.second == 10
assert d1.microsecond == 0
# Test properties for maya.parse()
d2 = maya.parse('February 29, 1992 13:12:34')
assert d2.year == 1992
assert d2.month == 2
assert d2.day == 29
assert d2.week == 9
assert d2.weekday == 6
assert d2.hour == 13
assert d2.minute == 12
assert d2.second == 34
assert d2.microsecond == 0
def test_print_date(capsys):
d = maya.when('11-17-11')
assert d.year == 2011
assert d.month == 11
assert d.day == 17
print(d)
out, err = capsys.readouterr()
assert out == 'Thu, 17 Nov 2011 00:00:00 GMT\n'
assert repr(d) == '<MayaDT epoch=1321488000.0>'
def test_invalid_date():
with pytest.raises(ValueError):
maya.when('another day')
def test_slang_date():
@@ -58,4 +99,62 @@ def test_slang_time():
d = maya.when('one hour ago')
assert d.slang_time() == 'an hour ago'
# rand_day = maya.when('2011-02-07', timezone='US/Eastern')
def test_parse():
d = maya.parse('February 21, 1994')
assert format(d) == '1994-02-21 00:00:00+00:00'
d = maya.parse('01/05/2016')
assert format(d) == '2016-01-05 00:00:00+00:00'
d = maya.parse('01/05/2016', day_first=True)
assert format(d) == '2016-05-01 00:00:00+00:00'
def test_datetime_to_timezone():
dt = maya.when('2016-01-01').datetime(to_timezone='US/Eastern')
assert dt.tzinfo.zone == 'US/Eastern'
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_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
# Check Exceptions
with pytest.raises(TypeError):
now == 1
with pytest.raises(TypeError):
now != 1
with pytest.raises(TypeError):
now < 1
with pytest.raises(TypeError):
now <= 1
with pytest.raises(TypeError):
now > 1
with pytest.raises(TypeError):
now >= 1