{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "#datetime\n", "\n", "Python has the datetime module to help deal with timestamps in your code. Time values are represented with the time class. Times have attributes for hour, minute, second, and microsecond. They can also include time zone information. The arguments to initialize a time instance are optional, but the default of 0 is unlikely to be what you want.\n", "\n", "##time\n", "Lets take a look at how we can extract time information from the datetime module. We can create a time-stamp by specifying datetime.time(hour,minute,second,microsecond)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "04:20:01\n", "hour : 4\n", "minute: 20\n", "second: 1\n", "microsecond: 0\n", "tzinfo: None\n" ] } ], "source": [ "import datetime\n", "\n", "t = datetime.time(4, 20, 1)\n", "# Lets show the different compoenets\n", "\n", "print t\n", "print 'hour :', t.hour\n", "print 'minute:', t.minute\n", "print 'second:', t.second\n", "print 'microsecond:', t.microsecond\n", "print 'tzinfo:', t.tzinfo" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note: A time instance only holds values of time, and not a date associated with the time. \n", "\n", "We can also check the min and max values a time of day can have in the module:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Earliest : 00:00:00\n", "Latest : 23:59:59.999999\n", "Resolution: 0:00:00.000001\n" ] } ], "source": [ "print 'Earliest :', datetime.time.min\n", "print 'Latest :', datetime.time.max\n", "print 'Resolution:', datetime.time.resolution" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The min and max class attributes reflect the valid range of times in a single day." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##Dates\n", "datetime (as you might suspect) also allows us to work with date timestamps. Calendar date values are represented with the date class. Instances have attributes for year, month, and day. It is easy to create a date representing today’s date using the today() class method.\n", "\n", "Lets see some examples:" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2015-09-18\n", "ctime: Fri Sep 18 00:00:00 2015\n", "tuple: time.struct_time(tm_year=2015, tm_mon=9, tm_mday=18, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=4, tm_yday=261, tm_isdst=-1)\n", "ordinal: 735859\n", "Year: 2015\n", "Mon : 9\n", "Day : 18\n" ] } ], "source": [ "today = datetime.date.today()\n", "print today\n", "print 'ctime:', today.ctime()\n", "print 'tuple:', today.timetuple()\n", "print 'ordinal:', today.toordinal()\n", "print 'Year:', today.year\n", "print 'Mon :', today.month\n", "print 'Day :', today.day" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As with time, the range of date values supported can be determined using the min and max attributes." ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Earliest : 0001-01-01\n", "Latest : 9999-12-31\n", "Resolution: 1 day, 0:00:00\n" ] } ], "source": [ "print 'Earliest :', datetime.date.min\n", "print 'Latest :', datetime.date.max\n", "print 'Resolution:', datetime.date.resolution" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Another way to create new date instances uses the replace() method of an existing date. For example, you can change the year, leaving the day and month alone." ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "d1: 2015-03-11\n", "d2: 1990-03-11\n" ] } ], "source": [ "d1 = datetime.date(2015, 3, 11)\n", "print 'd1:', d1\n", "\n", "d2 = d1.replace(year=1990)\n", "print 'd2:', d2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Arithmetic\n", "We can perform arithmetic on date objects to check for time differences. For example:" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "datetime.date(2015, 3, 11)" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d1" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "datetime.date(1990, 3, 11)" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d2" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "datetime.timedelta(9131)" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d1-d2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This give us the difference in days between the two dates. You can use the timedelta method to specify various units of times (day,minutes,hours,etc...)\n", "\n", "Great! You should now have a basic understanding of how to use datetime with Python to work with timestamps in your code!" ] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.10" } }, "nbformat": 4, "nbformat_minor": 0 }