<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>The Usware Blog - Django Web Development &#187; datetime</title>
	<atom:link href="http://uswaretech.com/blog/category/datetime/feed/" rel="self" type="application/rss+xml" />
	<link>http://uswaretech.com/blog</link>
	<description>Building Amazing Webapps</description>
	<lastBuildDate>Tue, 08 Jun 2010 14:59:46 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Understanding DateTime, tzinfo, timedelta &amp; TimeZone Conversions in python</title>
		<link>http://uswaretech.com/blog/2009/02/understanding-datetime-tzinfo-timedelta-timezone-conversions-python/</link>
		<comments>http://uswaretech.com/blog/2009/02/understanding-datetime-tzinfo-timedelta-timezone-conversions-python/#comments</comments>
		<pubDate>Thu, 19 Feb 2009 03:24:06 +0000</pubDate>
		<dc:creator>Rama Vadakattu</dc:creator>
				<category><![CDATA[datetime]]></category>

		<guid isPermaLink="false">http://uswaretech.com/blog/2009/02/understanding-datetimetzinfotimedelta-timezone-conversions-in-python-2/</guid>
		<description><![CDATA[In this post we will look at the common operations that are being performed on dates &#38; time ,&#160; converting&#160; a datetime from one timzone to another timezone. Common Operations:&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; How to get the present datetime? In [189]: cdatetime = datetime.now() In [190]: cdatetime Out[190]: datetime.datetime(2009, 2, 17, 17, 34, 58, 407806) It returns the [...]


Related posts:<ol><li><a href='http://uswaretech.com/blog/2009/06/understanding-decorators/' rel='bookmark' title='Permanent Link: Understanding decorators'>Understanding decorators</a></li>
<li><a href='http://uswaretech.com/blog/2009/03/finding-keywords-using-python/' rel='bookmark' title='Permanent Link: Finding keywords using Python'>Finding keywords using Python</a></li>
<li><a href='http://uswaretech.com/blog/2009/11/the-magic-of-metaclasses-in-python/' rel='bookmark' title='Permanent Link: The magic of metaclasses in Python'>The magic of metaclasses in Python</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p></p><p><a href="http://uswaretech.com/blog/wp-content/uploads/2009/02/pythonlogo.png"><img title="python-logo" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; margin-left: 0px; margin-right: 0px; border-right-width: 0px" height="71" alt="python-logo" src="http://uswaretech.com/blog/wp-content/uploads/2009/02/pythonlogo-thumb.png" width="211" align="right" border="0" /></a> <a href="http://uswaretech.com/blog/wp-content/uploads/2009/02/3289799486-fd7df39a73.jpg"><img title="3289799486_fd7df39a73" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; margin-left: 0px; margin-right: 0px; border-right-width: 0px" height="180" alt="3289799486_fd7df39a73" src="http://uswaretech.com/blog/wp-content/uploads/2009/02/3289799486-fd7df39a73-thumb.jpg" width="240" align="right" border="0" /></a></p>

<p>In this post we will look at the common operations that are being performed on dates &amp; time ,&#160; converting&#160; a datetime from one timzone to another timezone.</p>

<p><strong>Common Operations:&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Ho</strong><strong>w to get the present datetime? </strong></p>

<p><code>In [189]: cdatetime = datetime.now()      <br />In [190]: cdatetime       <br />Out[190]: datetime.datetime(2009, 2, 17, 17, 34, 58, 407806)<strong> </strong>      <br /></code>It returns the current localdate and time.When i print cdatetime it&#160; printed as a tuple of nine elements     <br />(2009=year, 2=month, 17=day, 17=hour, 34=minutes, 58=seconds, 407806=microseconds, tzinfo is None and is not printed)     <br />what each element in tuple represents is mentioned adjacent to that number.9-tuple is the basic&#160; notation in python using which we create datetime objects as     <br />dt = datetime(2009, 2, 17, 17, 34, 58, 407806).MOre details     <br /><a href="http://docs.python.org/library/datetime.html#datetime.datetime">http://docs.python.org/library/datetime.html#datetime.datetime</a></p>

<p><strong>How to convert datetime into a well formatted string? </strong></p>

<p><strong></strong><code>In [194]:&#160; curdate.strftime(&quot;%d %b %Y %I:%M:%S %p&quot;)      <br />.....:       <br />Out[194]: '17 Feb 2009 05:18:27 PM' </code>    <br />printing the date in tuple format is not a good idea because it is not understood by most of the people.we want to convert that into a readable string.     <br /><strong>strftime()</strong> is function we need to use to format the datetime into string.     <br /><strong>&quot;%d %b %Y %I:%M:%S %p&quot;&#160; is the format string each { %character} mentions how to print the repective components of datetime.      <br />%M in format string specifies </strong>Locale’s abbreviated month name     <br />(means print Feb instead of 2)     <br /><strong>%I in format string specifies</strong> Hour (12-hour clock) as a decimal number     <br />(means print the time in 12hour format instead of 24 hr format)     <br />you can find out various other directives ({%character} is called a directive) at the bottom of datetime module docs. <a href="http://docs.python.org/library/datetime.html">http://docs.python.org/library/datetime.html</a></p>

<p><strong>How to convert string to datetime object? </strong></p>

<p><strong></strong><code>In [195]: dt = datetime.<strong>strptime</strong>('17 Feb 2009 04:22:11 PM','%d %b %Y %I:%M:%S %p')       <br />In [196]: dt       <br />Out[196]: datetime.datetime(2009, 2, 17, 16, 22, 11)       <br />In [197]: dt.strftime(&quot;%d %b %Y %I:%M:%S %p&quot;)       <br />Out[197]: '17 Feb 2009 04:22:11 PM' </code></p>

<p>most of time we recieve datetime ( &#8217;17 Feb 2009 04:22:11 PM&#8217;) as a string&#160; from the user we need to make datetime object from that string.    <br /><strong>strptime</strong>() is the function which does this task of converting string into datetime.     <br />Format String &quot;%d %b %Y %I:%M:%S %p&quot; specifies the order in which the components of datetime will appear&#160; and way in which they appear (i.e first day will appear as decimal representing day of the month,second month&#160; will appear as&#160; Locale’s abbreviated month name etc&#8230;)</p>

<p><strong>How to convert a tuple into datetime object? </strong></p>

<p><strong></strong><code>Out[87]: k =&#160; (2009, 2, 17, 16, 22, 11)      <br />In [89]: dt = datetime.datetime(*k[0:6])       <br />In [206]: dt       <br />Out[206]: datetime.datetime(2009, 2, 17, 16, 22, 11)       <br />In [207]: dt.strftime(&quot;%d %b %Y %I:%M:%S %p&quot;)       <br />Out[207]: '17 Feb 2009 04:22:11 PM' </code></p>

<p>Many times you encounter a situation where you need to form a datetime object from a tuple as shown above&#160; (2009, 2, 17, 16, 22, 11).For example Universal feed parser gets blog entry date in 9 tuple format.    <br />we use the&#160; code dt = datetime.datetime(*k[0:6]) to convert it into datetime object.</p>

<p><strong>Airthmetic with datetime: </strong><strong>How to get two days back/after datetime from now ? </strong></p>

<p><strong></strong><code>In [3]: curdate = datetime.now()      <br />In [5]: curdate.strftime(&quot;%d %b %Y %I:%M:%S %p&quot;)       <br />Out[5]: '18 Feb 2009 09:56:59 AM'       <br />#gives you the two days back date       <br />In [14]: curdate = curdate-timedelta(days=2)       <br />In [15]: curdate.strftime(&quot;%d %b %Y %I:%M:%S %p&quot;)       <br />Out[15]: '16 Feb 2009 09:56:59 AM'       <br />#gives you the date after 3 days etc..       <br />In [17]: curdate = curdate+timedelta(days=3,hours=5,minutes=5,milliseconds=1000)       <br />In [18]: curdate.strftime(&quot;%d %b %Y %I:%M:%S %p&quot;)       <br />Out[18]: '20 Feb 2009 03:07:51 PM' </code>    <br />1) timedelta object is being used to represent the duration. please&#160; see in[14],in[17] for the various ways in which you can intialize timedelta objects.     <br />MOre details at : http://docs.python.org/library/datetime.html#datetime.timedelta     <br />2) use this timedelta object in airthmetic wih datetime <strong>(in[14]) to get the datetime before and after specified duration.)      <br /></strong>With this timdelta object you can such answer questions such as 1) get the datetime after 45 days,before 1month etc..</p>

<p><strong>How to check the timezone associated with datetime object? </strong></p>

<p>The timezones in python are represented through an abstract base class called tzinfo. To assign a timezone to a datetime object&#160; we need to create a subclass of <a href="http://docs.python.org/library/datetime.html#datetime.tzinfo">tzinfo</a> and attach this object to the datetime.tzinfo attribute as shown in the below examples.<strong> </strong></p>

<p><strong></strong><code>In [211]: curdate = datetime.now()      <br />In [212]:print&#160; curdate.tzinfo       <br />None </code></p>

<p>For the above object the timezone is null/None.These datetime objects are called naive datetime objects (i.e datetime objects who do not know their own timzone).We can&#8217;t perform timezone related operations on such naive objects.    <br />if tzinfo is not null those objects are called Timezone aware objects we can perform timzone related operations on such objects.Below i will show you an example which clearly illustrates the process.     <br /><code>In [162]: class GMT5(tzinfo):      <br />.....:&#160;&#160;&#160;&#160; def utcoffset(self,dt):       <br /><strong>#51/2 hours ahead of GMT</strong>       <br />.....:&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; return timedelta(hours=5,minutes=30)       <br />.....:&#160;&#160;&#160;&#160; def tzname(self,dt):       <br />.....:&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; return &quot;GMT +5&quot;       <br />.....:&#160;&#160;&#160;&#160; def dst(self,dt):       <br />.....:&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; return timedelta(0)       <br />In [171]: gmt5 = GMT5()       <br />In [220]: curdate = datetime(2009,2,17,19,10,2,tzinfo=gmt5)       <br />In [221]: curdate.tzinfo       <br />Out[221]: &lt;__main__.GMT5 object at 0x8a4b7cc&gt;       <br />In [222]: </code></p>

<p>Please follow the above code where i have shown you the&#160; way to assign timzone object (tzinfo) to datetime object.    <br />1) create an object which is a subclass of tzinfo.     <br />2) overide the tzname,utcoffset,dst (Daylight Saving Time) methods to provide the offset,dst that this time zone has from GMT.(Every timezone is represented as an offset from Greenwhich mean time)     <br />As the tzinfo is not null for the curdate.It is&#160; timezone aware object.we can perform operations on such objects.     <br />Below is another way to assign the timezone to datetime object.</p>

<p><code>In [172]: curdate = datetime.now()      <br />In [173]: curdate = curdate.replace(tzinfo=gmt5)       <br />In [174]: curdate       <br />Out[174]: datetime.datetime(2009, 2, 17, 17, 18, 27, 593447, tzinfo=&lt;__main__.GMT5 object at 0x8a4b7cc&gt;)       <br />In [176]: curdate.utcoffset()       <br />Out[176]: datetime.timedelta(0, 19800) </code></p>

<p>curdate.utcoffset&#160; will give you the&#160; offset from GMT in seconds.THis method general used to intialize local timezone to naive datetime object which represents the local datetime.</p>

<p><strong>Converting from one timezone to another timezone</strong></p>

<p>Suppose we want to convert a datetime whose timezone&#160; is +5:30 ahead of GMT (India) to another timezone whose timezone is ( -3:30 ) 3 1/2 hours behind GMT.    <br />Below is the code which does this.</p>

<p><code>In [162]: class GMT5(tzinfo):      <br />.....:&#160;&#160;&#160;&#160; def utcoffset(self,dt):       <br /><strong>#51/2 hours ahead of GMT</strong>       <br />.....:&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; return timedelta(hours=5,minutes=30)       <br />.....:&#160;&#160;&#160;&#160; def tzname(self,dt):       <br />.....:&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; return &quot;GMT +5&quot;       <br />.....:&#160;&#160;&#160;&#160; def dst(self,dt):       <br />.....:&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; return timedelta(0)       <br />In [171]: gmt5 = GMT5()       <br />In [220]: curdate = datetime(2009,2,18,10,21,58,tzinfo=gmt5)       <br />In [69]: curdate.strftime(&quot;%d %b %Y %I:%M:%S %p %Z&quot;)       <br />Out[69]: '18 Feb 2009 10:21:58 AM GMT +5'       <br />In [57]: class GMT3(tzinfo):       <br />def utcoffset(self,dt):       <br />#31/2 hours behind&#160; GMT       <br />return timedelta(hours=-3,minutes=30)       <br />def tzname(self,dt):       <br />return &quot;GMT -3&quot;       <br />def dst(self,dt):       <br />return timedelta(0)       <br />....:       <br />In [66]: gmt_3 = GMT3()       <br />In [67]: newdate = curdate.astimezone(gmt_3)       <br />In [70]: newdate.strftime(&quot;%d %b %Y %I:%M:%S %p %Z&quot;)       <br />Out[70]: '18 Feb 2009 02:21:58 AM GMT -3' </code></p>

<p>1.create a timezone object (tzinfo) represeting the GMT-3 timezone.    <br />2.use astimezone() function to convert the present locatime datetime(GMT+5:30) to (GMT-3:30)     <br />as shown above. In next post we will discuss some more tips and tricks associated with dates and time.</p>

<p><strong>Do you know?</strong>     <br />Do you know any important tips and tricks which are helpful when working with dates and time? if yes please mention&#160; in the comments.</p>

<p>Resources:    <br />&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;     <br /><a href="http://docs.python.org/library/datetime.html#datetime.datetime">http://docs.python.org/library/datetime.html#datetime.datetime</a>     <br /><a href="http://www.saltycrane.com/blog/2008/11/python-datetime-time-conversions/">http://www.saltycrane.com/blog/2008/11/python-datetime-time-conversions/</a></p>


<p>Related posts:<ol><li><a href='http://uswaretech.com/blog/2009/06/understanding-decorators/' rel='bookmark' title='Permanent Link: Understanding decorators'>Understanding decorators</a></li>
<li><a href='http://uswaretech.com/blog/2009/03/finding-keywords-using-python/' rel='bookmark' title='Permanent Link: Finding keywords using Python'>Finding keywords using Python</a></li>
<li><a href='http://uswaretech.com/blog/2009/11/the-magic-of-metaclasses-in-python/' rel='bookmark' title='Permanent Link: The magic of metaclasses in Python'>The magic of metaclasses in Python</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://uswaretech.com/blog/2009/02/understanding-datetime-tzinfo-timedelta-timezone-conversions-python/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
	</channel>
</rss>
