Popular recipes tagged "datetime"http://code.activestate.com/recipes/tags/datetime/2014-06-19T08:48:23-07:00ActiveState Code RecipesPython get shamsi Date time (Python) 2014-06-19T08:48:23-07:00Hassan Sajedihttp://code.activestate.com/recipes/users/4190234/http://code.activestate.com/recipes/578896-python-get-shamsi-date-time/ <p style="color: grey"> Python recipe 578896 by <a href="/recipes/users/4190234/">Hassan Sajedi</a> (<a href="/recipes/tags/datetime/">datetime</a>, <a href="/recipes/tags/python/">python</a>). </p> <p>Hey guys, Below code generate shamsi code for iran datetime and is useful for iranian developer. I hope suitable for all.</p> Flexible datetime parsing (Python) 2012-08-21T07:35:34-07:00Glenn Hutchingshttp://code.activestate.com/recipes/users/4175415/http://code.activestate.com/recipes/578245-flexible-datetime-parsing/ <p style="color: grey"> Python recipe 578245 by <a href="/recipes/users/4175415/">Glenn Hutchings</a> (<a href="/recipes/tags/datetime/">datetime</a>, <a href="/recipes/tags/parsing/">parsing</a>). </p> <p>The strptime() method of datetime accepts a format string that you have to specify in advance. What if you want to be more flexible in the kinds of date your program accepts? Here's a recipe for a function that tries many different formats until it finds one that works.</p> Function that supports dividing timedelta by float (Python) 2012-05-17T13:47:07-07:00Ben Hoythttp://code.activestate.com/recipes/users/4170919/http://code.activestate.com/recipes/578136-function-that-supports-dividing-timedelta-by-float/ <p style="color: grey"> Python recipe 578136 by <a href="/recipes/users/4170919/">Ben Hoyt</a> (<a href="/recipes/tags/datetime/">datetime</a>, <a href="/recipes/tags/division/">division</a>, <a href="/recipes/tags/float/">float</a>). </p> <p>Python 2.x doesn't support dividing a timedelta by a float (only works with int). This function adds support for that.</p> subtract or add a month to a datetime.date or datetime.datetime (Python) 2010-06-25T18:41:19-07:00Trent Mickhttp://code.activestate.com/recipes/users/4173505/http://code.activestate.com/recipes/577274-subtract-or-add-a-month-to-a-datetimedate-or-datet/ <p style="color: grey"> Python recipe 577274 by <a href="/recipes/users/4173505/">Trent Mick</a> (<a href="/recipes/tags/date/">date</a>, <a href="/recipes/tags/datetime/">datetime</a>, <a href="/recipes/tags/month/">month</a>). </p> <p>Adding or subtracting a month to a Python <code>datetime.date</code> or <code>datetime.datetime</code> is a little bit of a pain. Here is the code I use for that. These functions return the same datetime type as given. They preserve time of day data (if that is at all important to you).</p> <p>See also: </p> <ul> <li><a href="http://code.activestate.com/recipes/476197/">Recipe 476197</a>: First / Last Day of the Month.</li> <li><a href="http://packages.python.org/MonthDelta/">monthdelta module</a></li> </ul> Convert strings like '5d' and '60s' to timedelta objects (Python) 2011-10-06T14:06:54-07:00Dan McDougallhttp://code.activestate.com/recipes/users/4169722/http://code.activestate.com/recipes/577894-convert-strings-like-5d-and-60s-to-timedelta-objec/ <p style="color: grey"> Python recipe 577894 by <a href="/recipes/users/4169722/">Dan McDougall</a> (<a href="/recipes/tags/datetime/">datetime</a>, <a href="/recipes/tags/timedelta/">timedelta</a>). </p> <p>I wrote this little function for <code>[Gate One](http://vimeo.com/24857127)</code> (a web-based terminal emulator/SSH client)... It converts strings in the format of &lt;num&gt;&lt;character&gt; into timedelta objects. It's not rocket science but maybe it'll save someone a few keystrokes :). Besides that, it comes with a really nice Sphinx-ready (reStructuredText) docstring with complete doctests.</p> Printing current, next, or previous day (Python) 2011-09-29T21:39:22-07:00Andy Barbourhttp://code.activestate.com/recipes/users/4174351/http://code.activestate.com/recipes/577883-printing-current-next-or-previous-day/ <p style="color: grey"> Python recipe 577883 by <a href="/recipes/users/4174351/">Andy Barbour</a> (<a href="/recipes/tags/datetime/">datetime</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/time/">time</a>). Revision 3. </p> <p>simply prints the previous, current, or next day. Inputs may be <em>year month month-day</em> or <em>year julian-day</em> with the output in the same format.</p> Redate source files using SVN info from $Id:$ (Python) 2010-07-30T10:00:25-07:00Michal Niklashttp://code.activestate.com/recipes/users/186902/http://code.activestate.com/recipes/577341-redate-source-files-using-svn-info-from-id/ <p style="color: grey"> Python recipe 577341 by <a href="/recipes/users/186902/">Michal Niklas</a> (<a href="/recipes/tags/date/">date</a>, <a href="/recipes/tags/datetime/">datetime</a>, <a href="/recipes/tags/file/">file</a>, <a href="/recipes/tags/files/">files</a>, <a href="/recipes/tags/svn/">svn</a>). </p> <p>Iterates through a directory, reading the data from svn info that looks like:</p> <pre class="prettyprint"><code>$Id: svn_redater.py 747 2010-07-30 09:56:08Z mn $ </code></pre> <p>from source files.</p> <p>Parses the datetime from svn info and if it differs from file modification datetime then changes file datetime</p> parse a date/time string to a `datetime` instance (Python) 2010-04-02T07:32:17-07:00Trent Mickhttp://code.activestate.com/recipes/users/4173505/http://code.activestate.com/recipes/577135-parse-a-datetime-string-to-a-datetime-instance/ <p style="color: grey"> Python recipe 577135 by <a href="/recipes/users/4173505/">Trent Mick</a> (<a href="/recipes/tags/date/">date</a>, <a href="/recipes/tags/datetime/">datetime</a>, <a href="/recipes/tags/parse/">parse</a>, <a href="/recipes/tags/time/">time</a>). Revision 2. </p> <pre class="prettyprint"><code>&gt;&gt;&gt; import datetime &gt;&gt;&gt; str(datetime.datetime.now()) '2010-03-21 21:33:32.750246' &gt;&gt;&gt; str(datetime.date.today()) '2010-03-21' </code></pre> <p>This function goes the other way for date and datetime strings of this format.</p> Parse HTTP date-time string (Python) 2010-01-20T13:47:50-08:00Sridhar Ratnakumarhttp://code.activestate.com/recipes/users/4169511/http://code.activestate.com/recipes/577015-parse-http-date-time-string/ <p style="color: grey"> Python recipe 577015 by <a href="/recipes/users/4169511/">Sridhar Ratnakumar</a> (<a href="/recipes/tags/datetime/">datetime</a>, <a href="/recipes/tags/http/">http</a>, <a href="/recipes/tags/parsing/">parsing</a>). </p> <p>This recipe will help you parse datetime strings returned by HTTP servers following the RFC 2616 standard (which <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.3">supports three datetime formats</a>). Credit for this recipe goes to <a href="http://stackoverflow.com/questions/1471987/how-do-i-parse-an-http-date-string-in-python/1472336#1472336">ΤΖΩΤΖΙΟΥ</a>.</p> Convert datetime in python to user friendly representation. (Python) 2009-08-15T01:00:03-07:00Jai Vikram Singh Vermahttp://code.activestate.com/recipes/users/4171450/http://code.activestate.com/recipes/576880-convert-datetime-in-python-to-user-friendly-repres/ <p style="color: grey"> Python recipe 576880 by <a href="/recipes/users/4171450/">Jai Vikram Singh Verma</a> (<a href="/recipes/tags/ago/">ago</a>, <a href="/recipes/tags/datetime/">datetime</a>, <a href="/recipes/tags/parse/">parse</a>, <a href="/recipes/tags/representation/">representation</a>, <a href="/recipes/tags/string/">string</a>, <a href="/recipes/tags/user_friendly/">user_friendly</a>). </p> <p>A small contribution to the developer community.</p> <p>This module caters to the need of developers who want to put date &amp; time of post in terms like <br /> "X days, Y hrs ago", "A hours B mins ago", etc. in their applications rather then a basic timestamp <br /> like "2009-08-15 03:03:00". Additionally it also <br /> provides since epoch for a given datetime. </p> <p>It takes in a Python datetime object as an input <br /> and provides a fancy datetime (as I call it) and <br /> the seconds since epoch. </p> Compare passed day of week to today's day of week (Python) 2009-01-17T20:51:26-08:00Samuel Huckinshttp://code.activestate.com/recipes/users/4168906/http://code.activestate.com/recipes/576619-compare-passed-day-of-week-to-todays-day-of-week/ <p style="color: grey"> Python recipe 576619 by <a href="/recipes/users/4168906/">Samuel Huckins</a> (<a href="/recipes/tags/date/">date</a>, <a href="/recipes/tags/datetime/">datetime</a>, <a href="/recipes/tags/dayofweek/">dayofweek</a>). </p> <p>This function compares the day of the week of today to the day of the week passed.</p> Get a posix timestamp from a type 1 uuid (Python) 2008-08-13T18:56:29-07:00Kent Tenneyhttp://code.activestate.com/recipes/users/4166394/http://code.activestate.com/recipes/576420-get-a-posix-timestamp-from-a-type-1-uuid/ <p style="color: grey"> Python recipe 576420 by <a href="/recipes/users/4166394/">Kent Tenney</a> (<a href="/recipes/tags/datetime/">datetime</a>, <a href="/recipes/tags/time/">time</a>, <a href="/recipes/tags/timestamp/">timestamp</a>, <a href="/recipes/tags/uuid/">uuid</a>). </p> <p>The uuid timestamp is 60 bits, the number of 100 nanosecond increments since Oct. 15, 1582 This simple function returns a value which makes datetime.datetime.fromtimestamp() happy.</p> <p>It simply rewinds the code in the standard library's uuid1 function:</p> <pre class="prettyprint"><code>nanoseconds = int(time() * 1e9) # 0x01b21dd213814000 is the number of 100-ns intervals between the # UUID epoch 1582-10-15 00:00:00 and the Unix epoch 1970-01-01 00:00:00. timestamp = int(nanoseconds/100) + 0x01b21dd213814000 </code></pre>