{ "cells": [ { "cell_type": "markdown", "id": "occasional-tennessee", "metadata": {}, "source": [ "[Home](Home.ipynb)\n", "\n", "# Calendar\n", "\n", "There's much to learn about dates and times, irrespective on any computer language. Our timekeeping systems are among the most complex of measuring devices, multi-base and tied to astronomical phenomena at every turn.\n", "\n", "For now this page is a Student Sandbox, meaning I encourage my students to replace it with a Jupyter Notebook of their own creation, taking up some aspect of the calendar, using Python.\n", "\n", "*Proposed project*:\n", "\n", "How many days have you been alive, starting from your birthday until today? \n", "\n", "Write a function to figure that out for any birthday, with a second argument defaulting to today.\n", "\n", "Hint: start with [the Python docs](https://docs.python.org/3/library/datetime.html) on the ```datetime``` module and look for ways to convert a calendar date to an integer and back. Also, what's a ```timedelta```?" ] }, { "cell_type": "code", "execution_count": 1, "id": "peripheral-bargain", "metadata": {}, "outputs": [], "source": [ "from datetime import date" ] }, { "cell_type": "code", "execution_count": 3, "id": "whole-behavior", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "737815" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "date.today().toordinal()" ] }, { "cell_type": "code", "execution_count": 4, "id": "defined-valuation", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\u001b[0;31mDocstring:\u001b[0m Return proleptic Gregorian ordinal. January 1 of year 1 is day 1.\n", "\u001b[0;31mType:\u001b[0m method_descriptor\n" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "? date.toordinal" ] }, { "cell_type": "markdown", "id": "amazing-transsexual", "metadata": {}, "source": [ "What does \"proleptic Gregorian ordinal\" mean? Pretend our current calendar goes forward and backward in time, uninterrupted. \n", "\n", "In actual fact, the current calendar has not been used all that long and the labels we give to days over a thousand years ago would not correspond to any labels actually applied then. \n", "\n", "People back then had a different calendar, or actually several as we've had many civilizations going in parallel.\n", "\n", "So \"proleptic\" means if we count backward from today, we can label each day we come to, all the way back to the semi-fictitious 01/01/0001.\n", "\n", "How many days have gone by since then, until today? You'll see an answer above, that's the number up to whenever this Notebook was last run." ] }, { "cell_type": "code", "execution_count": 11, "id": "laden-stewart", "metadata": {}, "outputs": [], "source": [ "from datetime import datetime" ] }, { "cell_type": "code", "execution_count": 12, "id": "electoral-strike", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\u001b[0;31mDocstring:\u001b[0m Return POSIX timestamp as float.\n", "\u001b[0;31mType:\u001b[0m method_descriptor\n" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "? datetime.timestamp" ] }, { "cell_type": "code", "execution_count": 9, "id": "electoral-stand", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1611613273.579586" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "datetime.now().timestamp()" ] }, { "cell_type": "code", "execution_count": 10, "id": "reasonable-mattress", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "datetime.datetime(2021, 1, 25, 14, 21, 13, 579586)" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "datetime.fromtimestamp(1611613273.579586)" ] }, { "cell_type": "markdown", "id": "oriented-crisis", "metadata": {}, "source": [ "The timestamp and fromtimestamp functions, inverses of one another, have a millisecond resolution. Instead of 01/01/0001 as the origin, we use what's called The UNIX epoch, which begins 01/01/1970." ] }, { "cell_type": "markdown", "id": "mathematical-truck", "metadata": {}, "source": [ "## For Further Reading\n", "\n", "[Python docs for datetime module](https://docs.python.org/3/library/datetime.html)\n", "\n", "[Computing Easter](https://github.com/4dsolutions/Python5/blob/master/easter.py)\n", "\n", "[Chinese Lunar and Solar Calendar](https://pypi.org/project/lunar_python/)" ] } ], "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.9.1" } }, "nbformat": 4, "nbformat_minor": 5 }