{"id":722,"date":"2014-09-29T21:56:31","date_gmt":"2014-09-30T01:56:31","guid":{"rendered":"https:\/\/sheldon-hess.org\/coral\/?p=722"},"modified":"2014-10-03T21:35:29","modified_gmt":"2014-10-04T01:35:29","slug":"dealing-with-time","status":"publish","type":"post","link":"https:\/\/www.sheldon-hess.org\/coral\/2014\/09\/dealing-with-time\/","title":{"rendered":"Dealing with time"},"content":{"rendered":"<p>Look! A short post with just one or two &#8220;things learned&#8221; in it! Follow-through! :)<\/p>\n<p>I&#8217;ve rediscovered something more annoying than Unicode (or rather ASCII; I blame ASCII, since we should all be using Unicode, always, at this point) &#8211; time.<\/p>\n<p>There&#8217;s been a time element in most of the scripts I&#8217;ve written, so far, but 1) someone else wrote it, and I&#8217;ve just been reusing it. 2) It explicitly skipped the hard parts. See?<\/p>\n<p><code>from datetime import date, timedelta<br \/>\nstart_date = str(date.today() - timedelta(days_back))<br \/>\n<\/code><\/p>\n<p>Easy!<\/p>\n<p>But if you start with a string, possibly a string that won&#8217;t be formatted the same in every chunk of data\/metadata* you&#8217;re looking at, it&#8217;s a little trickier. I wandered through numerous date and time libraries and, finally, this happened&mdash;and I think it was my coworker who came up with it, actually:<\/p>\n<p><code>from datetime import datetime<br \/>\nfrom dateutil.parser import *<br \/>\nDEFAULT = datetime(2010, 01, 01)<br \/>\na_date = parse(stringthatrepresentsadate, yearfirst=True, default=DEFAULT).isoformat()<br \/>\n<\/code><\/p>\n<p>Seems so simple, right?<\/p>\n<p><a href=\"https:\/\/twitter.com\/__get__\/status\/516601097948237824\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/sheldon-hess.org\/coral\/wp-content\/uploads\/2014\/09\/sarcasm-300x300.jpg\" alt=\"sarcasm\" width=\"300\" height=\"300\" class=\"alignright size-medium wp-image-723\" srcset=\"https:\/\/www.sheldon-hess.org\/coral\/wp-content\/uploads\/2014\/09\/sarcasm-300x300.jpg 300w, https:\/\/www.sheldon-hess.org\/coral\/wp-content\/uploads\/2014\/09\/sarcasm-150x150.jpg 150w, https:\/\/www.sheldon-hess.org\/coral\/wp-content\/uploads\/2014\/09\/sarcasm.jpg 600w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a>You&#8217;re probably wondering &#8220;why the weird default?&#8221;. Wellllll, we had some yyyy-mm dates (ex: 2011-02) that were parsing correctly as, for example, February, WhateverYear (yay!). But if only the month and the year are available, <a href=\"https:\/\/labix.org\/python-dateutil#head-a23e8ae0a661d77b89dfb3476f85b26f0b30349c\">parse()<\/a> helpfully supplies a day, so that it can generate a valid datetime for you. (OK, that&#8217;s <em>actually<\/em> helpful. Valid data is valid.) If you haven&#8217;t specified a default, the day it gives you is, oh so helpfully, the day on which you run it. Today is the 29th, which was &#8220;out of range for month,&#8221; as Python kept helpfully barking at me, every time the month was February. (Shh, pedants. Shhhh.) You get around this by having a default date set. You can&#8217;t just do a default day; you need a whole date. I found a funny mistake in my code as I was writing this post, so I&#8217;ll probably pick a funnier\/more important default date when I make that other change. :)<\/p>\n<p>So, great, now we have a datetime object. I&#8217;m sure JSON parsers will be thrilled to take that in. (Even if they would, which they <a href=\"http:\/\/weblog.west-wind.com\/posts\/2014\/Jan\/06\/JavaScript-JSON-Date-Parsing-and-real-Dates\">would not<\/a>, our schema specified an ISO 8601-formatted string, NOT a datetime object.) That&#8217;s where <a href=\"https:\/\/docs.python.org\/2\/library\/datetime.html#datetime.date.isoformat\">.isoformat()<\/a> comes in, converting your datetime object back into an ISO 8601-formatted string. It&#8217;s a convenient thing to have in your pocket.<\/p>\n<p>Fun Python fact: putting a variable in all caps is used to signify &#8220;this is a constant that we are not going to change.&#8221; You CAN change it; Python won&#8217;t stop you; but convention is that you don&#8217;t do that.<\/p>\n<p>*Metadata is in the eye of the beholder, it seems. We grab metadata with an API, and there&#8217;s actually metadata about that metadata (when it was updated, where it came from) that we also capture. And then we save the metadata and metadata-metadata as objects in a system, and there&#8217;s metadata about that. I threatened to make a song to the tune of &#8220;I am the very model of the modern major general&#8221; about the situation, but then I think we got back to work.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Look! A short post with just one or two &#8220;things learned&#8221; in it! Follow-through! :) I&#8217;ve rediscovered something more annoying than Unicode (or rather ASCII; I blame ASCII, since we should all be using Unicode, always, at this point) &#8211; time.<\/p>\n<div class=\"more-link-wrapper\"><a class=\"more-link\" href=\"https:\/\/www.sheldon-hess.org\/coral\/2014\/09\/dealing-with-time\/\">Continue reading<span class=\"screen-reader-text\">Dealing with time<\/span><\/a><\/div>\n","protected":false},"author":3,"featured_media":723,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[85,87],"tags":[],"class_list":["post-722","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-new-developer","category-python","entry"],"_links":{"self":[{"href":"https:\/\/www.sheldon-hess.org\/coral\/wp-json\/wp\/v2\/posts\/722","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sheldon-hess.org\/coral\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sheldon-hess.org\/coral\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sheldon-hess.org\/coral\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sheldon-hess.org\/coral\/wp-json\/wp\/v2\/comments?post=722"}],"version-history":[{"count":0,"href":"https:\/\/www.sheldon-hess.org\/coral\/wp-json\/wp\/v2\/posts\/722\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.sheldon-hess.org\/coral\/wp-json\/wp\/v2\/media\/723"}],"wp:attachment":[{"href":"https:\/\/www.sheldon-hess.org\/coral\/wp-json\/wp\/v2\/media?parent=722"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sheldon-hess.org\/coral\/wp-json\/wp\/v2\/categories?post=722"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sheldon-hess.org\/coral\/wp-json\/wp\/v2\/tags?post=722"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}