{ "metadata": { "language": "Julia", "name": "" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "#3. PLEAC Julia: Dates and Times with Calendar.jl\n", "\n", "[PLEAC = Language Examples Alike Cookbook](http://pleac.sourceforge.net/) by Guillaume Cottenceau et al.\n", "PLEAC examples were drawn from the \"Perl Cookbook\" by Tom Christiansen & Nathan Torkington, published by O'Reilly, and used with the publisher's permission. They provide a nice range of examples oriented toward data munging, the type of work I tend to want to do first when learning a new language.\n", "\n", "The Julia examples below are principally translations from the [Python version](http://pleac.sourceforge.net/pleac_python/)\n", "\n", "###Caution\n", "I'm learning as I go, so the code below probably doesn't represent best practice. Your suggestions are welcome! \n", "Please file an issue or make a pull request to the [github repo](https://github.com/catawbasam/IJulia_PLEAC/).\n", "\n", "The examples are not complete. Missing items are generally noted in comments. " ] }, { "cell_type": "code", "collapsed": false, "input": [ "VERSION" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 1, "text": [ "v\"0.2.0-rc4\"" ] } ], "prompt_number": 1 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Libraries: ICU and Calendar, by @nolta\n", "\n", "Calendar.jl builds on ICU.jl to provide an internationalized set of dates and times. It knows how to query the computer for timezone and daylight savings settings and provides parsing and formatting options and how to display dates and times in a localized format. It does require ICU binaries, complicating setup a bit." ] }, { "cell_type": "code", "collapsed": false, "input": [ "using ICU\n", "using Calendar\n", "n=now()" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 2, "text": [ "Dec 6, 2013, 10:16:38 AM EST" ] } ], "prompt_number": 2 }, { "cell_type": "code", "collapsed": false, "input": [ "whos(ICU)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "ICU " ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Module\n", "ICUCalendar DataType\n", "ICUDateFormat DataType\n", "UCAL_AM_PM Int32\n", "UCAL_DATE Int32\n", "UCAL_DAY_OF_WEEK Int32\n", "UCAL_DAY_OF_WEEK_IN_MONTH Int32\n", "UCAL_DAY_OF_YEAR Int32\n", "UCAL_DOW_LOCAL Int32\n", "UCAL_DST_OFFSET Int32\n", "UCAL_ERA Int32\n", "UCAL_EXTENDED_YEAR Int32\n", "UCAL_HOUR Int32\n", "UCAL_HOUR_OF_DAY Int32\n", "UCAL_IS_LEAP_MONTH Int32\n", "UCAL_JULIAN_DAY Int32\n", "UCAL_MILLISECOND Int32\n", "UCAL_MILLISECONDS_IN_DAY Int32\n", "UCAL_MINUTE Int32\n", "UCAL_MONTH Int32\n", "UCAL_SECOND Int32\n", "UCAL_WEEK_OF_MONTH Int32\n", "UCAL_WEEK_OF_YEAR Int32\n", "UCAL_YEAR Int32\n", "UCAL_YEAR_WOY Int32\n", "UCAL_ZONE_OFFSET Int32\n", "UDAT_FULL Int32\n", "UDAT_LONG Int32\n", "UDAT_MEDIUM Int32\n", "UDAT_NONE Int32\n", "UDAT_RELATIVE Int32\n", "UDAT_SHORT Int32\n", "add Function\n", "clear Function\n", "foldcase Function\n", "format Function\n", "get Function\n", "getDefaultTimeZone Function\n", "getMillis Function\n", "getNow Function\n", "getTimeZoneDisplayName Function\n", "lowercase Function\n", "parse Function\n", "set Function\n", "setDate Function\n", "setDateTime Function\n", "setMillis Function\n", "set_locale Function\n", "titlecase Function\n", "uppercase Function\n" ] } ], "prompt_number": 3 }, { "cell_type": "code", "collapsed": false, "input": [ "ICU.getDefaultTimeZone()" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 4, "text": [ "\"America/New_York\"" ] } ], "prompt_number": 4 }, { "cell_type": "code", "collapsed": false, "input": [ "ICU.getNow() #epoch in milliseconds as a Float64" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 5, "text": [ "1.386342999171e12" ] } ], "prompt_number": 5 }, { "cell_type": "code", "collapsed": false, "input": [ "zcal=ICU.ICUCalendar(\"UTC\")\n", "dump(zcal)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "ICUCalendar" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " \n", " ptr: Ptr{None} Ptr{Void} @0x00000000037b7200\n" ] } ], "prompt_number": 6 }, { "cell_type": "code", "collapsed": false, "input": [ "lcal=ICU.ICUCalendar()\n", "dump(lcal)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "ICUCalendar \n", " ptr: Ptr{None} Ptr{Void} @0x00000000037c4bf0\n" ] } ], "prompt_number": 7 }, { "cell_type": "code", "collapsed": false, "input": [ "ICU.get(zcal, UCAL_MONTH)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 8, "text": [ "11" ] } ], "prompt_number": 8 }, { "cell_type": "code", "collapsed": false, "input": [ "ICU.get(zcal, [UCAL_MONTH, UCAL_DATE, UCAL_YEAR, UCAL_ZONE_OFFSET, UCAL_DST_OFFSET])'" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 9, "text": [ "1x5 Array{Int32,2}:\n", " 11 6 2013 0 0" ] } ], "prompt_number": 9 }, { "cell_type": "code", "collapsed": false, "input": [ "ldt = ICU.get(lcal, [UCAL_MONTH, UCAL_DATE, UCAL_YEAR, UCAL_ZONE_OFFSET, UCAL_DST_OFFSET])'" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 10, "text": [ "1x5 Array{Int32,2}:\n", " 11 6 2013 -18000000 0" ] } ], "prompt_number": 10 }, { "cell_type": "code", "collapsed": false, "input": [ "# timezone offset in hours\n", "ldt[4]/1000/3600" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 11, "text": [ "-5.0" ] } ], "prompt_number": 11 }, { "cell_type": "code", "collapsed": false, "input": [ "ICU.setDate(lcal, 2011, 11, 1)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 12 }, { "cell_type": "code", "collapsed": false, "input": [ "millis = ICU.getMillis(lcal)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 13, "text": [ "1.320156999405e12" ] } ], "prompt_number": 13 }, { "cell_type": "code", "collapsed": false, "input": [ "fmt=ICU.ICUDateFormat(\"yyyy\", \"UTC\")" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 14, "text": [ "ICUDateFormat(Ptr{Void} @0x00000000037c4e90)" ] } ], "prompt_number": 14 }, { "cell_type": "markdown", "metadata": {}, "source": [ "# [ICU Format patterns:](http://www.unicode.org/reports/tr35/tr35-dates.html#Date_Format_Patterns)\n", "```\n", "Pattern\t Result (in a particular locale)\n", "======= ===============================\n", "yyyy.MM.dd G 'at' HH:mm:ss zzz\t 1996.07.10 AD at 15:08:56 PDT\n", "EEE, MMM d, ''yy\t Wed, July 10, '96\n", "h:mm a\t 12:08 PM\n", "hh 'o''clock' a, zzzz\t 12 o'clock PM, Pacific Daylight Time\n", "K:mm a, z\t 0:00 PM, PST\n", "yyyyy.MMMM.dd GGG hh:mm aaa\t0 1996.July.10 AD 12:08 PM\n", "\n", "w=week of year; W=week of month; d=day of month; D=day of year; c=Day of week\n", "```" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#ICU.format(ICUDateFormat, millis)\n", "ifmt1 = ICU.format(fmt, millis)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 15, "text": [ "\"2011\"" ] } ], "prompt_number": 15 }, { "cell_type": "code", "collapsed": false, "input": [ "ICU.getTimeZoneDisplayName(lcal)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 16, "text": [ "\"EST\"" ] } ], "prompt_number": 16 }, { "cell_type": "code", "collapsed": false, "input": [ "whos(Calendar)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "April " ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Int64\n", "August Int64\n", "Calendar Module\n", "CalendarDuration DataType\n", "CalendarTime DataType\n", "CalendarTimeRange DataType\n", "December Int64\n", "February Int64\n", "FixedCalendarDuration DataType\n", "Friday Int64\n", "January Int64\n", "July Int64\n", "June Int64\n", "March Int64\n", "May Int64\n", "Monday Int64\n", "November Int64\n", "October Int64\n", "Saturday Int64\n", "September Int64\n", "Sunday Int64\n", "Thursday Int64\n", "Tuesday Int64\n", "Wednesday Int64\n", "am Function\n", "day Function\n", "dayofweek Function\n", "dayofyear Function\n", "days Function\n", "format Function\n", "hm Function\n", "hms Function\n", "hour Function\n", "hour12 Function\n", "hours Function\n", "isAM Function\n", "isPM Function\n", "isleap Function\n", "isleapyear Function\n", "minute Function\n", "minutes Function\n", "month Function\n", "months Function\n", "now Function\n", "parse_date Function\n", "pm Function\n", "second Function\n", "seconds Function\n", "timezone Function\n", "today Function\n", "tz Function\n", "week Function\n", "weeks Function\n", "year Function\n", "years Function\n", "ymd Function\n", "ymd_hms Function\n" ] } ], "prompt_number": 17 }, { "cell_type": "code", "collapsed": false, "input": [ "t0=Calendar.now()" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 18, "text": [ "Dec 6, 2013, 10:16:40 AM EST" ] } ], "prompt_number": 18 }, { "cell_type": "code", "collapsed": false, "input": [ "t = ymd_hms(2013, 3, 10, 1, 59, 59)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 19, "text": [ "Mar 10, 2013, 1:59:59 AM EST" ] } ], "prompt_number": 19 }, { "cell_type": "code", "collapsed": false, "input": [ "typeof(t)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 20, "text": [ "CalendarTime (constructor with 1 method)" ] } ], "prompt_number": 20 }, { "cell_type": "code", "collapsed": false, "input": [ "t + days(2)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 21, "text": [ "Mar 12, 2013, 2:59:59 AM EDT" ] } ], "prompt_number": 21 }, { "cell_type": "code", "collapsed": false, "input": [ "delta =t0-t" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 22, "text": [ "271 days + 8 hours + 16 minutes + 40.49399999901652 seconds" ] } ], "prompt_number": 22 }, { "cell_type": "code", "collapsed": false, "input": [ "t + delta" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 23, "text": [ "Dec 6, 2013, 10:16:40 AM EST" ] } ], "prompt_number": 23 }, { "cell_type": "code", "collapsed": false, "input": [ "dump(delta)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "FixedCalendarDuration" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " \n", " millis: Float64 2.3444200494e10\n" ] } ], "prompt_number": 24 }, { "cell_type": "code", "collapsed": false, "input": [ "s = Calendar.format(\"yyyy-MMMM-dd EEE HH:mm:ss x\", t)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 25, "text": [ "\"2013-March-10 Sun 01:59:59 -05\"" ] } ], "prompt_number": 25 }, { "cell_type": "code", "collapsed": false, "input": [ " t2 = Calendar.parse(\"yyyy-MMMM-dd EEE HH:mm:ss V\", s)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 26, "text": [ "Mar 10, 2013, 1:59:59 AM EST" ] } ], "prompt_number": 26 }, { "cell_type": "code", "collapsed": false, "input": [ "timezone(t2, \"UTC\")" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 27, "text": [ "Mar 10, 2013, 6:59:59 AM GMT" ] } ], "prompt_number": 27 }, { "cell_type": "markdown", "metadata": {}, "source": [ "? isdst should be 0 or 1. How do we get it from datetime? Maybe we can ignore it for formatting?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "###PLEAC 3.0: Introduction" ] }, { "cell_type": "code", "collapsed": false, "input": [ "dayofyear(today())" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 28, "text": [ "340" ] } ], "prompt_number": 28 }, { "cell_type": "code", "collapsed": false, "input": [ "println(\"Today is day $(dayofyear(today())) of the current year.\")" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Today is day 340 of the current year." ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n" ] } ], "prompt_number": 29 }, { "cell_type": "code", "collapsed": false, "input": [ "println(\"Today is day $(dayofyear(today())) of $(year(today())).\")" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Today is day 340 of 2013.\n" ] } ], "prompt_number": 30 }, { "cell_type": "markdown", "metadata": {}, "source": [ "###PLEAC 3.1: Finding Today's Date" ] }, { "cell_type": "code", "collapsed": false, "input": [ "println(\"The date is $(today()).\")" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "The date is Dec 6, 2013, 12:00:00 AM EST." ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n" ] } ], "prompt_number": 31 }, { "cell_type": "markdown", "metadata": {}, "source": [ "###PLEAC 3.2: Converting DMYHMS to epoch seconds" ] }, { "cell_type": "code", "collapsed": false, "input": [ "now()" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 32, "text": [ "Dec 6, 2013, 10:16:41 AM EST" ] } ], "prompt_number": 32 }, { "cell_type": "code", "collapsed": false, "input": [ "epoch_dt = ymd_hms(1970,1,1,0,0,0,\"UTC\")" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 33, "text": [ "Jan 1, 1970, 12:00:00 AM GMT" ] } ], "prompt_number": 33 }, { "cell_type": "code", "collapsed": false, "input": [ "now() - epoch_dt" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 34, "text": [ "16045 days + 15 hours + 16 minutes + 40.7039999961853 seconds" ] } ], "prompt_number": 34 }, { "cell_type": "code", "collapsed": false, "input": [ "(now() - epoch_dt).millis" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 35, "text": [ "1.386343000859e12" ] } ], "prompt_number": 35 }, { "cell_type": "code", "collapsed": false, "input": [ "dt=now()" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 36, "text": [ "Dec 6, 2013, 10:16:41 AM EST" ] } ], "prompt_number": 36 }, { "cell_type": "code", "collapsed": false, "input": [ "dump(dt)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "CalendarTime" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " \n", " millis: Float64 1.386343001702e12\n", " cal: ICUCalendar \n", " ptr: Ptr{None} Ptr{Void} @0x000000001e4af1f0\n" ] } ], "prompt_number": 37 }, { "cell_type": "code", "collapsed": false, "input": [ "dt.millis / 1000" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 38, "text": [ "1.386343001702e9" ] } ], "prompt_number": 38 }, { "cell_type": "code", "collapsed": false, "input": [ "function CalTime2epoch(caltime)\n", " return caltime.millis/1000\n", "end" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 39, "text": [ "CalTime2epoch (generic function with 1 method)" ] } ], "prompt_number": 39 }, { "cell_type": "code", "collapsed": false, "input": [ "CalTime2epoch(today())" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 40, "text": [ "1.386306000301e9" ] } ], "prompt_number": 40 }, { "cell_type": "markdown", "metadata": {}, "source": [ "###PLEAC 3.3: Converting epoch seconds to DMYHMS (CalendarTime)" ] }, { "cell_type": "code", "collapsed": false, "input": [ "epoch_dt" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 41, "text": [ "Jan 1, 1970, 12:00:00 AM GMT" ] } ], "prompt_number": 41 }, { "cell_type": "code", "collapsed": false, "input": [ "epoch_time = time()" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 42, "text": [ "1.386343002607e9" ] } ], "prompt_number": 42 }, { "cell_type": "code", "collapsed": false, "input": [ "function epoch2CalendarTime(epoch)\n", " epoch_dt = ymd_hms(1970,1,1,0,0,0,\"UTC\")\n", " return epoch_dt + seconds(epoch)\n", "end" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 43, "text": [ "epoch2CalendarTime (generic function with 1 method)" ] } ], "prompt_number": 43 }, { "cell_type": "code", "collapsed": false, "input": [ "epoch2CalendarTime(epoch_time)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 44, "text": [ "Dec 6, 2013, 3:16:43 PM GMT" ] } ], "prompt_number": 44 }, { "cell_type": "markdown", "metadata": {}, "source": [ "###PLEAC 3.4: Adding to or subtracting from a date" ] }, { "cell_type": "code", "collapsed": false, "input": [ "mydate = ymd(2013,1,2)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 45, "text": [ "Jan 2, 2013, 12:00:00 AM EST" ] } ], "prompt_number": 45 }, { "cell_type": "code", "collapsed": false, "input": [ "println(\"One day in the future is $(mydate + days(1))\")" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "One day in the future is Jan 3, 2013, 12:00:00 AM EST" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n" ] } ], "prompt_number": 46 }, { "cell_type": "code", "collapsed": false, "input": [ "println(\"Two weeks in the past is $(mydate + weeks(-2))\")" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Two weeks in the past is Dec 19, 2012, 12:00:00 AM EST" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n" ] } ], "prompt_number": 47 }, { "cell_type": "code", "collapsed": false, "input": [ "dt2 = ymd(2013,2,14)\n", "dt2 - mydate" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 48, "text": [ "43 days" ] } ], "prompt_number": 48 }, { "cell_type": "code", "collapsed": false, "input": [ "birthtime = ymd_hms(1973,1,18,3,45,50)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 49, "text": [ "Jan 18, 1973, 3:45:50 AM EST" ] } ], "prompt_number": 49 }, { "cell_type": "code", "collapsed": false, "input": [ "then = birthtime + seconds(5)+minutes(17)+hours(2)+days(55)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 50, "text": [ "Mar 14, 1973, 6:02:55 AM EST" ] } ], "prompt_number": 50 }, { "cell_type": "code", "collapsed": false, "input": [ "println(\"Then is $then\")" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Then is Mar 14, 1973, 6:02:55 AM EST" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n" ] } ], "prompt_number": 51 }, { "cell_type": "code", "collapsed": false, "input": [ "when = ymd(1973,1,18) + days(55)\n", "println(\"Nat was 55 days old on $when\")" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Nat was 55 days old on Mar 14, 1973, 12:00:00 AM EST" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n" ] } ], "prompt_number": 52 }, { "cell_type": "markdown", "metadata": {}, "source": [ "###PLEAC 3.5: Difference of two dates" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Date differences are given in days as a Day " ] }, { "cell_type": "code", "collapsed": false, "input": [ "diff = dt2 - mydate" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 53, "text": [ "43 days" ] } ], "prompt_number": 53 }, { "cell_type": "code", "collapsed": false, "input": [ "dump(diff)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "FixedCalendarDuration" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " \n", " millis: Float64 3.7152e9\n" ] } ], "prompt_number": 54 }, { "cell_type": "markdown", "metadata": {}, "source": [ "differences are given in milliseconds as an Float64" ] }, { "cell_type": "code", "collapsed": false, "input": [ "dt1 = now()" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 55, "text": [ "Dec 6, 2013, 10:16:44 AM EST" ] } ], "prompt_number": 55 }, { "cell_type": "code", "collapsed": false, "input": [ "dt2 = now()" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 56, "text": [ "Dec 6, 2013, 10:16:44 AM EST" ] } ], "prompt_number": 56 }, { "cell_type": "code", "collapsed": false, "input": [ "diff_dt = dt2-dt1" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 57, "text": [ "0.14100000000000001 seconds" ] } ], "prompt_number": 57 }, { "cell_type": "code", "collapsed": false, "input": [ "dump(diff_dt)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "FixedCalendarDuration" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " \n", " millis: Float64 141.0\n" ] } ], "prompt_number": 58 }, { "cell_type": "markdown", "metadata": {}, "source": [ "####Something like partition_period(date, (date parts,)) partition_period(datetime,(datetime parts,)) might be handy" ] }, { "cell_type": "code", "collapsed": false, "input": [ "bree = ymd_hms(1981,6,16,4,35,25)\n", "nat = ymd_hms(1973,1,18,3,45,50)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 59, "text": [ "Jan 18, 1973, 3:45:50 AM EST" ] } ], "prompt_number": 59 }, { "cell_type": "code", "collapsed": false, "input": [ "dtdiff=bree-nat" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 60, "text": [ "3070 days + 23 hours + 49 minutes + 35 seconds" ] } ], "prompt_number": 60 }, { "cell_type": "code", "collapsed": false, "input": [ "dump(dtdiff)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "FixedCalendarDuration" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " \n", " millis: Float64 2.65333775e11\n" ] } ], "prompt_number": 61 }, { "cell_type": "code", "collapsed": false, "input": [ "dur2seconds(dur) = int(floor(dur.millis/1000)) \n", "dur2minutes(dur) = int(floor( (dur.millis/1000) / 60))\n", "dur2hours(dur) = int(floor( (dur.millis/1000) / 3600))\n", "dur2days(dur) = int(floor( (dur.millis/1000) / 86400 ))\n", "dur2weeks(dur) = int(floor( (dur.millis/1000) / 86400 / 7 ))" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 62, "text": [ "dur2weeks (generic function with 1 method)" ] } ], "prompt_number": 62 }, { "cell_type": "code", "collapsed": false, "input": [ "dur2days( dtdiff )" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 63, "text": [ "3070" ] } ], "prompt_number": 63 }, { "cell_type": "code", "collapsed": false, "input": [ "println(\"There were $(dur2days(dtdiff)) days between Nat and Bree\")" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "There were 3070 days between Nat and Bree" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n" ] } ], "prompt_number": 64 }, { "cell_type": "code", "collapsed": false, "input": [ "wks = dur2weeks(dtdiff)\n", "days = dur2days(dtdiff)\n", "netdays = days - wks*7\n", "hrs = dur2hours(dtdiff)\n", "nethours = hrs - days*24\n", "mins = dur2minutes(dtdiff)\n", "netmins = mins - 60*hrs\n", "netsecs = dur2seconds(dtdiff) - 60*mins\n", "wks,netdays, nethours, netmins, netsecs" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 65, "text": [ "(438,4,23,49,35)" ] }, { "output_type": "stream", "stream": "stderr", "text": [ "Warning: imported binding for days overwritten in module Main\n" ] } ], "prompt_number": 65 }, { "cell_type": "code", "collapsed": false, "input": [ "println(\"$wks weeks, $netdays days, $nethours:$netmins:$netsecs\")" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "438 weeks, 4 days, 23:49:35" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n" ] } ], "prompt_number": 66 }, { "cell_type": "markdown", "metadata": {}, "source": [ "###PLEAC 3.6: Day in a Week/Month/Year or Week Number" ] }, { "cell_type": "code", "collapsed": false, "input": [ "when = ymd(1981,6,16)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 67, "text": [ "Jun 16, 1981, 12:00:00 AM EDT" ] } ], "prompt_number": 67 }, { "cell_type": "code", "collapsed": false, "input": [ "dayofweek(when) # = Tuesday" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 68, "text": [ "3" ] } ], "prompt_number": 68 }, { "cell_type": "code", "collapsed": false, "input": [ "year(when), month(when), day(when)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 69, "text": [ "(1981,6,16)" ] } ], "prompt_number": 69 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Formatted Datetime and Formatted Date" ] }, { "cell_type": "code", "collapsed": false, "input": [ "format(\"yyyy\", when)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 70, "text": [ "\"1981\"" ] } ], "prompt_number": 70 }, { "cell_type": "code", "collapsed": false, "input": [ "t = ymd_hms(2013, 3, 10, 1, 59, 59)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 71, "text": [ "Mar 10, 2013, 1:59:59 AM EST" ] } ], "prompt_number": 71 }, { "cell_type": "code", "collapsed": false, "input": [ "s = format(\"yyyy-MMMM-dd HH:mm:ss V\", t)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 72, "text": [ "\"2013-March-10 01:59:59 usnyc\"" ] } ], "prompt_number": 72 }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Calendar.format() appears to have a limitation on maximum string length or maximum substitutions.\n", "had to break up this format string into 2 parts" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "#strftime(\"Today is Day %w of the week (a %A). Day %d of the month (%B)\",Datetime.today())\n", "td = today()\n", "#('MMMM')'\", td)\n", "format(\"'Today is Day ' c ' of the week (a ' eeee'). '\",td) * format(\"'Day' d 'of the month ('MMMM').'\",td) " ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 73, "text": [ "\"Today is Day 6 of the week (a Friday). Day 6 of the month (December).\"" ] } ], "prompt_number": 73 }, { "cell_type": "code", "collapsed": false, "input": [ "# Tuesday\n", "#strftime(\"1981-06-16 was Day %w of the week (a %A). Day %d of the month (%B)\",when)\n", "format(\"'Today is Day ' c ' of the week (a ' eeee'). '\",when) * format(\"'Day' d 'of the month ('MMMM').'\",when) " ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 74, "text": [ "\"Today is Day 3 of the week (a Tuesday). Day 16 of the month (June).\"" ] } ], "prompt_number": 74 }, { "cell_type": "code", "collapsed": false, "input": [ "format(\"'Today is Day ' c ' of the week (a ' eeee'). Day' d 'of the month ('MMMM').'\",when) " ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n" ] }, { "output_type": "stream", "stream": "stderr", "text": [ "WARNING: " ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n" ] }, { "ename": "LoadError", "evalue": "BoundsError()\nat In[75]:1", "output_type": "pyerr", "traceback": [ "BoundsError()\nat In[75]:1", " in copy! at array.jl:49", " in getindex at array.jl:296" ] } ], "prompt_number": 75 }, { "cell_type": "code", "collapsed": false, "input": [ "# day of year: 168, week of year=24\n", "#strftime(\"Day %j of the year(%Y), in week %W of the year.\",when)\n", "format(\"'Day ' D ' of the year ('YYYY'), in week ' w 'of the year.'\", when)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stderr", "text": [ "backtraces on your platform are often misleading or partially incorrect\n" ] }, { "metadata": {}, "output_type": "pyout", "prompt_number": 76, "text": [ "\"Day 167 of the year (1981), in week 25 of the year.\"" ] } ], "prompt_number": 76 }, { "cell_type": "markdown", "metadata": {}, "source": [ "###PLEAC 3.7: Parsing Dates and Times from Strings" ] }, { "cell_type": "code", "collapsed": false, "input": [ "Calendar.parse(\"EEE MMM DD HH:mm:ss yyyy\", \"Tue Jun 16 20:18:03 1981\")" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 77, "text": [ "Jan 16, 1981, 8:18:03 PM EST" ] } ], "prompt_number": 77 }, { "cell_type": "code", "collapsed": false, "input": [ "#time.strptime(\"16/6/1981\", \"%d/%m/%Y\")\n", "Calendar.parse(\"DD/M/yyyy\",\"16/6/1981\")" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 78, "text": [ "Jan 16, 1981, 12:00:00 AM EST" ] } ], "prompt_number": 78 }, { "cell_type": "markdown", "metadata": {}, "source": [ "###PLEAC 3.8: Printing a Date" ] }, { "cell_type": "code", "collapsed": false, "input": [ "cdt = Calendar.now()\n", "Calendar.format(\"'The date is 'YYYY\",cdt)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 79, "text": [ "\"The date is 2013\"" ] } ], "prompt_number": 79 }, { "cell_type": "code", "collapsed": false, "input": [ "\"\"\"The date is $(Calendar.format(\" EEEE (EEE) d/MM/yyyy\",cdt))\"\"\"" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 80, "text": [ "\"The date is Friday (Fri) 6/12/2013\"" ] } ], "prompt_number": 80 }, { "cell_type": "code", "collapsed": false, "input": [ "Calendar.format(\"'The date is ' EEEE (EEE) d/MM/yyyy\",cdt)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 81, "text": [ "\"The date is Friday (Fri) 6/12/2013\"" ] } ], "prompt_number": 81 }, { "cell_type": "markdown", "metadata": {}, "source": [ "###PLEAC 3.9: High-Resolution Timers (in Base)" ] }, { "cell_type": "code", "collapsed": false, "input": [ "tic()" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 82, "text": [ "0x0004e8bc673bc904" ] } ], "prompt_number": 82 }, { "cell_type": "code", "collapsed": false, "input": [ "elapsed=toc()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "elapsed time: " ] }, { "output_type": "stream", "stream": "stdout", "text": [ "0.160737503 seconds\n" ] }, { "metadata": {}, "output_type": "pyout", "prompt_number": 83, "text": [ "0.160737503" ] } ], "prompt_number": 83 }, { "cell_type": "code", "collapsed": false, "input": [ "# from https://github.com/JuliaLang/julia/issues/4478\n", "function f1(n)\n", " sum = 0.0\n", " g1(k) = 1.0/k\n", " for i = 1:n\n", " sum += g1(i) \n", " end\n", " sum\n", "end" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 84, "text": [ "f1 (generic function with 1 method)" ] } ], "prompt_number": 84 }, { "cell_type": "code", "collapsed": false, "input": [ "f1(99)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 85, "text": [ "5.177377517639621" ] } ], "prompt_number": 85 }, { "cell_type": "code", "collapsed": false, "input": [ "@time f1(1e6)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "elapsed time: " ] }, { "output_type": "stream", "stream": "stdout", "text": [ "0.115000702 seconds (48094564 bytes allocated)\n" ] }, { "metadata": {}, "output_type": "pyout", "prompt_number": 86, "text": [ "14.392726722864989" ] } ], "prompt_number": 86 }, { "cell_type": "markdown", "metadata": {}, "source": [ "###PLEAC 3.10: Short Sleeps (in Base)" ] }, { "cell_type": "code", "collapsed": false, "input": [ "sleep(3.1) #sleep for 4 seconds" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 87 }, { "cell_type": "code", "collapsed": false, "input": [ "@time( sleep(3.1) )" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "elapsed time: " ] }, { "output_type": "stream", "stream": "stdout", "text": [ "3.101292537 seconds (408 bytes allocated)\n" ] } ], "prompt_number": 88 }, { "cell_type": "markdown", "metadata": {}, "source": [ "###PLEAC 3.11: Program hopdelta" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "will not solve" ] }, { "cell_type": "code", "collapsed": false, "input": [ ";ipython nbconvert 3_pleac_datetime-calendar.ipynb" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stderr", "text": [ "[NbConvertApp] Using existing profile dir: u'C:\\\\Users\\\\keithc\\\\.ipython\\\\profile_default'\r\n" ] }, { "output_type": "stream", "stream": "stderr", "text": [ "[NbConvertApp] Converting notebook 3_pleac_datetime-calendar.ipynb to html\r\n", "[NbConvertApp] Support files will be in 3_pleac_datetime-calendar_files\\\r\n", "[NbConvertApp] Loaded template html_full.tpl\r\n" ] }, { "output_type": "stream", "stream": "stderr", "text": [ "[NbConvertApp] Writing 276388 bytes to 3_pleac_datetime-calendar.html\r\n" ] } ], "prompt_number": 91 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }