Skip to main content

Posts

Showing posts with the label datetime

Python: simple recipe to measure your function's execution time

We always write something unusual while doing basic things. Our own bicycles and crutches to work out some unusual situation. Here is another recipe to do a thing like so. I have tried several libraries and readymade decisions. But it assumes you have them installed. And you often try things in console, don't you? Anyway the recipe is simple and quite straightforward. import datetime # Getting first timestamp t1 = datetime . datetime . now ( ) # Your function e.g.: data = [ g . name for g in request . user . groups . all ( ) ] # Second timestamp t2 = datetime . datetime . now ( ) print "Execution time: %s" % ( t1 - t2 ) It is rude and quite simple but may often suit you well to measure execution time in a simple and straightforward manner. Also nice idea to write down this function into logs. It may be handy on refactoring of your core app for e.g.; import datetime import logging log = logging . getLogger ( 'mylogger' ) # Getting ...

Timezones. New in Django 1.4.

What and Why? Under Django version 1.4 all dates stored and displayed for a single timezone. The one that is specified in project.settings.TIME_ZONE . Now you gain ability to store dates in UTC and render it with timezone correction. Problems with localtime  bypass is an additional plus. They can happen once a year. For e.g. 31 of November 2012 in Russia. Time from 2:00am to 3:00am in fact goes by twice. It may not be a problem for 99% of users. But it can become a nightmare for billing systems. So it's better to store time in UTC and display with user Time Zone correction. So "02:15am 31 November 2012" will become " 2012-10-30T22:15:00+04:00" and "2010-10-30T23:15:00+03:00" that is so handy for programmers ;). Concepts datetime objects in Python support timezones with attribute tzinfo . If this attribute is filled out it is called "timezone-aware", otherwise it's "naive" date. Django uses timezone-aware date...