mirror of
https://github.com/kennethreitz/maya.git
synced 2026-06-05 14:50:19 +00:00
Compare commits
53 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 2fce84195e | |||
| 7ab9e48d45 | |||
| ee1a8e2438 | |||
| b0e91d6c0e | |||
| bce5d13401 | |||
| b4a23d668d | |||
| 90051da96d | |||
| 8f0ff0d68c | |||
| fd1262a8e8 | |||
| 7558261dc8 | |||
| 1407e688fd | |||
| 0c2b936111 | |||
| 9e72ef3d2f | |||
| eabba0b79e | |||
| 8af92bc33c | |||
| 917420071f | |||
| 2bb430b91d | |||
| 967638228a | |||
| 1ba2dac284 | |||
| 89a54abf1f | |||
| f119fe927b | |||
| 0efc489ba1 | |||
| 8b6ca639cb | |||
| 48c245a8ad | |||
| 29a5b990fd | |||
| 69c88552d7 | |||
| 2126c8f33e | |||
| dbc20b3026 | |||
| c521672f0e | |||
| f756b13d4d | |||
| 2ce5a23e38 | |||
| 605e15ae27 | |||
| 07a3e814a2 | |||
| 2e811d8689 | |||
| cbbd7d92c1 | |||
| d9e08034dd | |||
| e3b1eacb77 | |||
| be29d01160 | |||
| 5e4853936b | |||
| 7dbe450c28 | |||
| 3ce6734a5e | |||
| 6e2f530207 | |||
| c46d9874d4 | |||
| 87013d71e0 | |||
| a562f48e24 | |||
| 8fc07b3ab3 | |||
| 59064a5c50 | |||
| 7ae84f3266 | |||
| cc668974b6 | |||
| 6a4ddff215 | |||
| a0ab4fc3fb | |||
| c80ecad67b | |||
| 97842c01b3 |
+94
@@ -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/
|
||||
@@ -0,0 +1,8 @@
|
||||
language: python
|
||||
python:
|
||||
- "2.7"
|
||||
# - "3.5" # looks like ruamel.ordereddict doesn't build for python3
|
||||
# command to install dependencies
|
||||
install: "pip install -r requirements.txt"
|
||||
# command to run tests
|
||||
script: make
|
||||
+32
-5
@@ -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,8 @@ Installation is easy, with pip::
|
||||
$ pip install maya
|
||||
|
||||
✨🍰✨
|
||||
|
||||
☤ Like it?
|
||||
----------
|
||||
|
||||
`Say Thanks <https://saythanks.io/to/kennethreitz>`_!
|
||||
|
||||
@@ -34,7 +34,7 @@ class MayaDT(object):
|
||||
|
||||
def __format__(self, *args, **kwargs):
|
||||
"""Return's the datetime's format"""
|
||||
return self.datetime(*args, **kwargs)
|
||||
return format(self.datetime(), *args, **kwargs)
|
||||
|
||||
# Timezone Crap
|
||||
# -------------
|
||||
@@ -104,12 +104,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."""
|
||||
@@ -196,13 +200,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)
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
-e .
|
||||
dateparser==0.5.0
|
||||
humanize==0.5.1
|
||||
iso8601==0.1.11
|
||||
|
||||
@@ -3,9 +3,21 @@
|
||||
|
||||
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__))
|
||||
|
||||
def read(*parts):
|
||||
return codecs.open(os.path.join(here, *parts), 'r').read()
|
||||
|
||||
if sys.argv[-1] == "publish":
|
||||
os.system("python setup.py sdist bdist_wheel upload")
|
||||
@@ -16,18 +28,20 @@ required = [
|
||||
'pytz',
|
||||
'dateparser',
|
||||
'iso8601',
|
||||
'python-dateutil'
|
||||
'python-dateutil',
|
||||
'ruamel.yaml',
|
||||
'tzlocal'
|
||||
]
|
||||
|
||||
setup(
|
||||
name='maya',
|
||||
version='0.1.0',
|
||||
version='0.1.4',
|
||||
description='Datetimes for Humans.',
|
||||
long_description=open('README.rst').read(),
|
||||
long_description= '\n' + read('README.rst'),
|
||||
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=(
|
||||
|
||||
Regular → Executable
+37
-2
@@ -1,3 +1,6 @@
|
||||
import pytest
|
||||
from datetime import datetime
|
||||
|
||||
import maya
|
||||
|
||||
|
||||
@@ -43,10 +46,27 @@ def test_dt_tz_naive():
|
||||
|
||||
|
||||
def test_random_date():
|
||||
d = maya.when('11-17-11')
|
||||
d = maya.when('11-17-11 08:09:10')
|
||||
assert d.year == 2011
|
||||
assert d.month == 11
|
||||
assert d.day == 17
|
||||
assert d.hour == 8
|
||||
assert d.minute == 9
|
||||
assert d.second == 10
|
||||
assert d.microsecond == 0
|
||||
|
||||
|
||||
def test_print_date(capsys):
|
||||
d = maya.when('11-17-11')
|
||||
|
||||
print(d)
|
||||
out, err = capsys.readouterr()
|
||||
assert out == '<MayaDT epoch=1321488000.0>\n'
|
||||
|
||||
|
||||
def test_invalid_date():
|
||||
with pytest.raises(ValueError):
|
||||
maya.when('another day')
|
||||
|
||||
|
||||
def test_slang_date():
|
||||
@@ -58,4 +78,19 @@ 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'
|
||||
|
||||
|
||||
Reference in New Issue
Block a user