<?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>scienceoss.com &#187; timezone</title>
	<atom:link href="http://scienceoss.com/tags/timezone/feed/" rel="self" type="application/rss+xml" />
	<link>http://scienceoss.com</link>
	<description>useful tidbits for using open source software in science</description>
	<lastBuildDate>Wed, 26 May 2010 03:34:19 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Timezones in Python</title>
		<link>http://scienceoss.com/timezones-in-python/</link>
		<comments>http://scienceoss.com/timezones-in-python/#comments</comments>
		<pubDate>Thu, 10 Jan 2008 18:24:06 +0000</pubDate>
		<dc:creator>ryan</dc:creator>
				<category><![CDATA[matplotlib]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[daylight savings]]></category>
		<category><![CDATA[eastern]]></category>
		<category><![CDATA[timezone]]></category>
		<category><![CDATA[tz]]></category>
		<category><![CDATA[utc]]></category>

		<guid isPermaLink="false">http://scienceoss.com/?p=59</guid>
		<description><![CDATA[I had a Python datetime object, T. I knew it was in Eastern time and wanted to convert to UTC. I also wanted it to keep track of daylight savings time, for when I have dates later in the year. Python datetime objects have &#8220;support&#8221; for timezone info, but you have to implement it yourself. [...]]]></description>
			<content:encoded><![CDATA[<ul>
<li>I had a Python datetime object, <span class="c">T</span>.</li>
<li>I knew it was in Eastern time and wanted to convert to UTC.  </li>
<li>I also wanted it to keep track of daylight savings time, for when I have dates later in the year.</li>
<li>Python datetime objects have &#8220;support&#8221; for timezone info, but you have to implement it yourself.  Read: more coding overhead that I&#8217;m trying to avoid.
</ul>
<p>Here&#8217;s how to do it the easy way.<span id="more-59"></span></p>
<p>There&#8217;s a module called <a href="http://pytz.sourceforge.net/">pytz</a> to make things easy.</p>
<h3>Import</h3>
<p>First, import the goodies.</p>
<pre class = "prettyprint"><code class = "code">from pytz import timezone
import pytz
import datetime
</code></pre>
<h3>Create timezone objects</h3>
<p>Then, create two <span class="c">timezone</span> objects: one for eastern time, and one for utc.</p>
<pre class = "prettyprint"><code class = "code">eastern_tz = timezone('US/Eastern')
utc_tz = pytz.utc
</code></pre>
<h3>Localize the time</h3>
<p>Next, &#8220;localize&#8221; the time, T.  This is a way of telling the datetime object what timezone it should be.</p>
<pre class = "prettyprint"><code class = "code">T = datetime.datetime(2008, 1, 10, 7, 20, 46, 613195)
eastern_datetime = eastern_tz.localize(T)
    # datetime.datetime(2008, 1, 10, 7, 20, 46, 613195,
    # tzinfo=<dstTzInfo 'US/Eastern' EST-1 day, 19:00:00 STD>)
</code></pre>
<h3>Convert the localized time</h3>
<p>Now convert <span class="c">eastern_datetime</span> into UTC using its <span class="c">astimezone</span> method.  Feed it the utc_tz timezone object to tell it how to convert.</p>
<pre class = "prettyprint"><code class = "code">utc_datetime = eastern_datetime.astimezone(utc_tz)
</code></pre>
<h2>Plotting time series with timezones</h2>
<p>The documentation for matplotlib says you can give plot_date() a string like &#8216;US/Eastern&#8217; for the tz keyword.  This didn&#8217;t work for me; I got errors.</p>
<pre class = "prettyprint"><code class = "code"><type 'exceptions.TypeError'>: astimezone() argument 1 must be datetime.tzinfo, not str
WARNING: Failure executing file: <sun_to_db.py></code></pre>
<p>To fix this, I found my matplotlibrc file (the Linux command <span class="c">locate matplotlibrc</span> gave me the file <span class="c">/usr/lib/python2.5/site-packages/matplotlib/mpl-data/matplotlibrc</span>) and changed the timezone to &#8220;US/Eastern&#8221;.</p>
<p>date2num converts dates into number of days (fraction part represents hours, minutes, seconds) since 0001-01-01 00:00:00 UTC.</p>
<p>But plot_date pulls from matplotlibrc file to determine which timezone to show in.</p>
<p>That means, if you have a list of datetime objects that are in fact Eastern, you have to make sure they are converted via pytz, then date2num&#8217;d, then plotted.</p>
<p>You&#8217;ll also need to set the xdata formatter ax.fmt_xdata = DateFormatter(&#8216;%x %H:%M %Z&#8217;)</p>
<p>In sum, assume you have a list of datetime objects that are really in Eastern time.  This list is called timeline.</p>
<pre class = "prettyprint"><code class = "code">timeline # a list of datetime objects that are really in Eastern
values # a list of values, one for each time in timeline
import pytz
from pytz import timezone
from pylab import figure, show, DateFormatter

eastern = timezone('US/Eastern')
utc = pytz.utc
timeline_eastern = [eastern.localize(i) for i in timeline]
timeline_utc = [i.astimezone(utc) for i in timeline_eastern]

# Both of these are the same! Why? Cause date2num is smart and
# keeps track of timezones.  Nevertheless, these are both in "days from
# 0001-01-01 00:00 UTC"
mpl_dates_1 = [date2num(i) for i in timeline_eastern]
mpl_dates_2 = [date2num(i) for i in timeline_utc]

fig = figure()
ax = fig.add_subplot(111)
ax.plot_date(timeline,values)

# This formats numbers in the lower right to be proper dates
# that change when you mouse over the plot.
ax.fmt_xdata = DateFormatter('%x %H:%M %Z')

show()

</code></pre>
<p>The coolest part is if you plot a list of dates year round, the timezones change appropriately between EDT and EST!</p>
]]></content:encoded>
			<wfw:commentRss>http://scienceoss.com/timezones-in-python/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

