Top-rated recipes tagged "time"http://code.activestate.com/recipes/tags/time/top/2016-12-30T19:06:32-08:00ActiveState Code RecipesSimple command-line alarm clock in Python (Python) 2015-10-25T18:27:27-07:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/579117-simple-command-line-alarm-clock-in-python/ <p style="color: grey"> Python recipe 579117 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/alarm/">alarm</a>, <a href="/recipes/tags/clock/">clock</a>, <a href="/recipes/tags/commandline/">commandline</a>, <a href="/recipes/tags/linux/">linux</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/time/">time</a>, <a href="/recipes/tags/unix/">unix</a>, <a href="/recipes/tags/windows/">windows</a>). </p> <p>This recipe shows how to create a simple alarm clock in Python, that can be run from the command line in a terminal. It lets you specify the alarm time in minutes as a command line argument, and prints a wake-up message and beeps a few times, after that time arrives. You can use 0 for the minutes to test it immediately, including to adjust the volume using your speaker controls.</p> Benchmark code with the with statement (Python) 2011-10-08T09:53:06-07:00Steven D'Apranohttp://code.activestate.com/recipes/users/4172944/http://code.activestate.com/recipes/577896-benchmark-code-with-the-with-statement/ <p style="color: grey"> Python recipe 577896 by <a href="/recipes/users/4172944/">Steven D'Aprano</a> (<a href="/recipes/tags/benchmark/">benchmark</a>, <a href="/recipes/tags/speed/">speed</a>, <a href="/recipes/tags/time/">time</a>, <a href="/recipes/tags/timer/">timer</a>). </p> <p>Inspired by <a href="http://preshing.com/20110924/timing-your-code-using-pythons-with-statement">this post</a> I wrote this context manager to benchmark code blocks or function calls.</p> <p>Usage is incredibly simple:</p> <pre class="prettyprint"><code>with Timer(): ... # code to benchmark goes here </code></pre> <p>The time taken (in seconds) will be printed when the code block completes. To capture the time taken programmatically is almost as easy:</p> <pre class="prettyprint"><code>t = Timer() with t: ... # code to benchmark goes here time_taken = t.interval </code></pre> <p>Due to the difficulties of timing small snippets of code <em>accurately</em>, you should only use this for timing code blocks or function calls which take a significant amount of time to process. For micro-benchmarks, you should use the <code>timeit</code> module.</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> A command-line musical alarm clock (Python) 2016-12-30T19:06:32-08:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/580739-a-command-line-musical-alarm-clock/ <p style="color: grey"> Python recipe 580739 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/alarm/">alarm</a>, <a href="/recipes/tags/audio/">audio</a>, <a href="/recipes/tags/clock/">clock</a>, <a href="/recipes/tags/multimedia/">multimedia</a>, <a href="/recipes/tags/music/">music</a>, <a href="/recipes/tags/playsound/">playsound</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/time/">time</a>, <a href="/recipes/tags/utilities/">utilities</a>, <a href="/recipes/tags/utility/">utility</a>). </p> <p>This is a simple musical alarm clock in Python. You pass a command-line argument specifying the time in minutes after which the alarm should go off. When that time arrives, it plays a musical sound.</p> Monotonic local time. And sandwich. (Python) 2013-01-03T08:28:36-08:00Oren Tiroshhttp://code.activestate.com/recipes/users/2033964/http://code.activestate.com/recipes/578270-monotonic-local-time-and-sandwich/ <p style="color: grey"> Python recipe 578270 by <a href="/recipes/users/2033964/">Oren Tirosh</a> (<a href="/recipes/tags/daylight/">daylight</a>, <a href="/recipes/tags/dst/">dst</a>, <a href="/recipes/tags/time/">time</a>). </p> <p>A Mutton, Lettuce and Tomato sandwich is generally agreed to be the greatest thing in the world. But a different kind of MLT comes in at a close second: Monotonic Local Time. This is a time value in your local timezone that takes care of that annoying hour that happens once a year where time goes backwards. It works by extending the previous day by a couple of hours (24, 25 etc) until after the switch.</p> Simple curses based MySQL 'top' (Python) 2011-11-02T20:28:48-07:00Mike 'Fuzzy' Partinhttp://code.activestate.com/recipes/users/4179778/http://code.activestate.com/recipes/577936-simple-curses-based-mysql-top/ <p style="color: grey"> Python recipe 577936 by <a href="/recipes/users/4179778/">Mike 'Fuzzy' Partin</a> (<a href="/recipes/tags/curses/">curses</a>, <a href="/recipes/tags/database/">database</a>, <a href="/recipes/tags/mysql/">mysql</a>, <a href="/recipes/tags/time/">time</a>). Revision 3. </p> <p>This is little more than a modification of my previous recipe, however, I found it useful so I thought I would post it in the hopes that someone else would as well. There is color (BOLD white really) designation for 'Query' states vs others like 'Sleep'. </p> Binary BCD clock using curses (Python) 2010-10-19T10:40:36-07:00Michal Niklashttp://code.activestate.com/recipes/users/186902/http://code.activestate.com/recipes/577430-binary-bcd-clock-using-curses/ <p style="color: grey"> Python recipe 577430 by <a href="/recipes/users/186902/">Michal Niklas</a> (<a href="/recipes/tags/curses/">curses</a>, <a href="/recipes/tags/time/">time</a>). </p> <p>Converts actual time to vertical BCD digits. So actual time, 12:37:51 looks like:</p> <pre class="prettyprint"><code>...... ...**. .***.. *.**** </code></pre> Using ctypes to call C code from Python (Python) 2016-05-09T18:00:44-07:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/580660-using-ctypes-to-call-c-code-from-python/ <p style="color: grey"> Python recipe 580660 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/ctypes/">ctypes</a>, <a href="/recipes/tags/ffi/">ffi</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/time/">time</a>). </p> <p>This recipe shows basic usage of the ctypes module to call C code from Python code.</p> Stopwatch in 26 lines (Tcl) 2014-09-05T13:33:13-07:00Mike Manilonehttp://code.activestate.com/recipes/users/4190728/http://code.activestate.com/recipes/578927-stopwatch-in-26-lines/ <p style="color: grey"> Tcl recipe 578927 by <a href="/recipes/users/4190728/">Mike Manilone</a> (<a href="/recipes/tags/gui/">gui</a>, <a href="/recipes/tags/time/">time</a>). </p> <p>This stopwatch works on Tcl/Tk 8.5 and higher. It makes use of the [clock] command and reduced the amount of code by -textvariable. A simple one, indeed. Nevertheless, it is still easy-to-read and easy-to-understand and it does show the power that Tcl has.</p> Sending Email From A Python Program (Python) 2014-01-10T20:13:45-08:00Captain DeadBoneshttp://code.activestate.com/recipes/users/4184772/http://code.activestate.com/recipes/578807-sending-email-from-a-python-program/ <p style="color: grey"> Python recipe 578807 by <a href="/recipes/users/4184772/">Captain DeadBones</a> (<a href="/recipes/tags/email/">email</a>, <a href="/recipes/tags/smtp/">smtp</a>, <a href="/recipes/tags/time/">time</a>). Revision 4. </p> <p>I put together this code for one of my programs and decided to share it. You may never know when you will need you Python Program to send an email to someone. <a href="http://thelivingpearl.com/2014/01/10/sending-email-from-python-using-command-line/">Sending Email From Python</a>. </p> <p>NOTE: You will have to change the account setup variables to your own. The server is currently configured o Gmail, but you can use this script with any server. </p> Human readable format for a given time delta (Python) 2012-04-26T10:37:10-07:00Thomas Lehmannhttp://code.activestate.com/recipes/users/4174477/http://code.activestate.com/recipes/578113-human-readable-format-for-a-given-time-delta/ <p style="color: grey"> Python recipe 578113 by <a href="/recipes/users/4174477/">Thomas Lehmann</a> (<a href="/recipes/tags/back/">back</a>, <a href="/recipes/tags/date/">date</a>, <a href="/recipes/tags/human/">human</a>, <a href="/recipes/tags/readable/">readable</a>, <a href="/recipes/tags/time/">time</a>). </p> <p><strong>What is it about?</strong></p> <ul> <li>I need to say someting like <em>1 day ago</em>, <em>5 days ago</em>, <em>2 weeks ago</em>, ...</li> <li>I can control to have it with/without milliseconds and microseconds.</li> <li>I can use it automatically with current date and time or with a provide one.</li> </ul> <p><strong>Why?</strong></p> <ul> <li>I need it for next revision of my <a href="http://code.activestate.com/recipes/578111/">recipe 578111</a>.</li> <li>I found recipes here and there but often it is always assumed that a month has 30 days and that a year has 365 days; this is not true. That's why I've left away months and years.</li> </ul> Benchmark code with the with statement (Python) 2011-10-10T04:30:11-07:00vleonhttp://code.activestate.com/recipes/users/4179537/http://code.activestate.com/recipes/577900-benchmark-code-with-the-with-statement/ <p style="color: grey"> Python recipe 577900 by <a href="/recipes/users/4179537/">vleon</a> (<a href="/recipes/tags/benchmark/">benchmark</a>, <a href="/recipes/tags/speed/">speed</a>, <a href="/recipes/tags/time/">time</a>, <a href="/recipes/tags/timer/">timer</a>). </p> <p>Inspired by <a href="http://preshing.com/20110924/timing-your-code-using-pythons-with-statement">this post</a> I wrote this context manager to benchmark code blocks or function calls.</p> <p>Usage is incredibly simple:</p> <pre class="prettyprint"><code>with Timer(): ... # code to benchmark goes here </code></pre> <p>The time taken (in seconds) will be printed when the code block completes. To capture the time taken programmatically is almost as easy:</p> <pre class="prettyprint"><code>t = Timer() with t: ... # code to benchmark goes here time_taken = t.interval </code></pre> <p>Due to the difficulties of timing small snippets of code <em>accurately</em>, you should only use this for timing code blocks or function calls which take a significant amount of time to process. For micro-benchmarks, you should use the <code>timeit</code> module.</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> user and root directory logfile (Python) 2011-03-22T10:00:49-07:00justin drakehttp://code.activestate.com/recipes/users/4177409/http://code.activestate.com/recipes/577619-user-and-root-directory-logfile/ <p style="color: grey"> Python recipe 577619 by <a href="/recipes/users/4177409/">justin drake</a> (<a href="/recipes/tags/directory/">directory</a>, <a href="/recipes/tags/log/">log</a>, <a href="/recipes/tags/time/">time</a>, <a href="/recipes/tags/user/">user</a>). </p> <p>script to retrieve time, current user, and root of directory output to spam.txt</p> <p>want to add more like windows version, bios info and other useful diagnostic information </p> <p>free to use and modify welcome any advice or corrections learning python hope this turns into a script to create a organized personal report of customer's pc</p> sleep_min: Sleep for a minimum number of seconds (Python) 2011-02-08T16:44:36-08:00Drew Vogelhttp://code.activestate.com/recipes/users/4176907/http://code.activestate.com/recipes/577568-sleep_min-sleep-for-a-minimum-number-of-seconds/ <p style="color: grey"> Python recipe 577568 by <a href="/recipes/users/4176907/">Drew Vogel</a> (<a href="/recipes/tags/sleep/">sleep</a>, <a href="/recipes/tags/time/">time</a>). </p> <p><code>time.sleep(delay)</code> will sleep for, at most, <code>delay</code> seconds. This function will sleep for at least <code>delay</code> seconds.</p> Simulate number of flushes in a poker hand (Python) 2010-04-08T05:24:12-07:00Dudley G R Gentleshttp://code.activestate.com/recipes/users/4173603/http://code.activestate.com/recipes/577184-simulate-number-of-flushes-in-a-poker-hand/ <p style="color: grey"> Python recipe 577184 by <a href="/recipes/users/4173603/">Dudley G R Gentles</a> (<a href="/recipes/tags/cards/">cards</a>, <a href="/recipes/tags/flush/">flush</a>, <a href="/recipes/tags/poker/">poker</a>, <a href="/recipes/tags/process/">process</a>, <a href="/recipes/tags/time/">time</a>). </p> <p>This program calculates the number of flushes in a number of deals of a poker hand. You can tell it to deal say 10,000 hands and see how many were flushes. This is basically my first Python program. </p> Countdown to screenshot (PIL). (Python) 2009-12-03T20:07:52-08:00me mehttp://code.activestate.com/recipes/users/4172371/http://code.activestate.com/recipes/576976-countdown-to-screenshot-pil/ <p style="color: grey"> Python recipe 576976 by <a href="/recipes/users/4172371/">me me</a> (<a href="/recipes/tags/countdown/">countdown</a>, <a href="/recipes/tags/screenshot/">screenshot</a>, <a href="/recipes/tags/time/">time</a>). Revision 2. </p> <p>Tested on Linux and Windows-XP, Python 2.6 ...</p> <p>Counts down to a number and takes a screenshot. I simplified one of my long lost screenshot taking codes for linux removing sound, graphics and all other unnecessary bits.</p> Detect when system is idle (Python) 2009-05-31T01:18:03-07:00Gabriel Genellinahttp://code.activestate.com/recipes/users/924636/http://code.activestate.com/recipes/576786-detect-when-system-is-idle/ <p style="color: grey"> Python recipe 576786 by <a href="/recipes/users/924636/">Gabriel Genellina</a> (<a href="/recipes/tags/ctypes/">ctypes</a>, <a href="/recipes/tags/idle/">idle</a>, <a href="/recipes/tags/idle_time/">idle_time</a>, <a href="/recipes/tags/time/">time</a>, <a href="/recipes/tags/user_input/">user_input</a>, <a href="/recipes/tags/windows/">windows</a>). </p> <p>Detect when system is idle, globally. That is, the user is not moving the mouse nor typing anything, in any application (not just our program).</p> Class to calculate increment of variables based on time (units per seconds) (Python) 2008-08-18T16:29:08-07:00nosklohttp://code.activestate.com/recipes/users/4166478/http://code.activestate.com/recipes/576424-class-to-calculate-increment-of-variables-based-on/ <p style="color: grey"> Python recipe 576424 by <a href="/recipes/users/4166478/">nosklo</a> (<a href="/recipes/tags/increment/">increment</a>, <a href="/recipes/tags/linear/">linear</a>, <a href="/recipes/tags/number/">number</a>, <a href="/recipes/tags/time/">time</a>, <a href="/recipes/tags/units_per_second/">units_per_second</a>, <a href="/recipes/tags/variables/">variables</a>). Revision 3. </p> <p>A simple calculation of small increments to be applied to a variable, given the variable time that has passed since last update, to make a linear increase over time (in units per second).</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>