{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Maya: Datetimes for Humans™" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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 (timezones doubly so).\n", "\n", "Datetimes should be interacted with via an API written for humans.\n", "\n", "Maya is mostly built around the headaches and use-cases around parsing datetime data from websites." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Basic Usage of Maya" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [], "source": [ "import maya" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "maya.now()" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "tomorrow = maya.when('tomorrow')\n", "tomorrow" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'tomorrow'" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "tomorrow.slang_date()" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'in 23 hours'" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "tomorrow.slang_time()" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'2018-12-15T17:45:14.872356Z'" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Also: MayaDT.from_iso8601(...)\n", "tomorrow.iso8601()" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Sat, 15 Dec 2018 17:45:14 GMT'" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Also: MayaDT.from_rfc2822(...)\n", "tomorrow.rfc2822()" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'2018-12-15T17:45:14.8Z'" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Also: MayaDT.from_rfc3339(...)\n", "tomorrow.rfc3339()" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "datetime.datetime(2018, 12, 15, 17, 45, 14, 872356, tzinfo=)" ] }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "tomorrow.datetime()" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "datetime.datetime(2016, 12, 16, 13, 23, 45, 423992)" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Automatically parse datetime strings and generate naive datetimes.\n", "scraped = '2016-12-16 18:23:45.423992+00:00'\n", "maya.parse(scraped).datetime(to_timezone='US/Eastern', naive=True)" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [], "source": [ "rand_day = maya.when('2011-02-07', timezone='US/Eastern')" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Maya speaks Python.\n", "from datetime import datetime\n", "\n", "maya.MayaDT.from_datetime(datetime.utcnow())" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 45, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import time\n", "\n", "maya.MayaDT.from_struct(time.gmtime())" ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 46, "metadata": {}, "output_type": "execute_result" } ], "source": [ "maya.MayaDT(time.time())" ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "7" ] }, "execution_count": 47, "metadata": {}, "output_type": "execute_result" } ], "source": [ "rand_day.day" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "17" ] }, "execution_count": 48, "metadata": {}, "output_type": "execute_result" } ], "source": [ "rand_day.add(days=10).day" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'UTC'" ] }, "execution_count": 49, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Always.\n", "rand_day.timezone" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 50, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Range of hours in a day:\n", "maya.intervals(start=maya.now(), end=maya.now().add(days=1), interval=60*60)" ] }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Mon, 21 Feb 1994 03:00:00 GMT'" ] }, "execution_count": 51, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# snap modifiers\n", "dt = maya.when('Mon, 21 Feb 1994 21:21:42 GMT')\n", "dt.snap('@d+3h').rfc2822()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Advanced Usage of Maya" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In addition to timestamps, Maya also includes a wonderfully 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.\n", "\n", "For example:" ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [], "source": [ "from maya import MayaInterval" ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [], "source": [ "# Create an event that is one hour long, starting now.\n", "event_start = maya.now()\n", "event_end = event_start.add(hours=1)" ] }, { "cell_type": "code", "execution_count": 54, "metadata": {}, "outputs": [ { "data": { "text/plain": [ " end=>" ] }, "execution_count": 54, "metadata": {}, "output_type": "execute_result" } ], "source": [ "MayaInterval(start=event_start, end=event_end)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "From here, there are a number of methods available to you, which you can use to compare this event to another event." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Do your own experiments here...\n", "\n", "Try `maya` youself by adding your code below and running your own experiments 👇" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import maya\n", "\n", "# your code here\n", "maya." ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.5" } }, "nbformat": 4, "nbformat_minor": 2 }