Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You seem to be asking a few separate questions here.</p> <p>First, if you only care about your own machine's current local timezone, you don't need to know what it is. Just use the local-to-UTC functions. There are a few holes in the API, but even if you can't find the function you need, you can always just get from local to UTC or vice-versa by going through the POSIX timestamp and the <a href="http://docs.python.org/2/library/datetime.html#datetime.datetime.fromtimestamp" rel="nofollow"><code>fromtimestamp</code></a> and <code>utcfromtimestamp</code> methods.</p> <p>If you want to be able to deal with <em>any</em> timezone, see the top of <a href="http://docs.python.org/2/library/datetime.html" rel="nofollow">the docs</a> for the difference between aware and naive objects, but basically: an aware object is one that knows its timezone. So, that's what you need. The problem is that, as the docs say:</p> <blockquote> <p>Note that no concrete tzinfo classes are supplied by the datetime module. Supporting timezones at whatever level of detail is required is up to the application.</p> </blockquote> <p>The easiest way to support timezones is to install and use the third-party library <a href="http://pytz.sourceforge.net" rel="nofollow"><code>pytz</code></a>.</p> <p>Meanwhile, as <a href="http://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior" rel="nofollow"><code>strftime()</code> and <code>strptime()</code> Behavior</a> sort-of explains, <code>strptime</code> always returns a naive object. You then have to call <a href="http://docs.python.org/2/library/datetime.html#datetime.datetime.replace" rel="nofollow"><code>replace</code></a> and/or <code>astimezone</code> (depending on whether the string was a UTC time or a local time) to get an aware object imbued with the right timezone.</p> <p>But, even with all this, you still need to know what local timezone you're in, which means you still need a constant. In other words:</p> <pre><code>TIMEZONE = pytz.timezone('Europe/London') def RFC3339_to_localHHMM(input): # Take an XML date (2013-04-08T22:35:00Z) # return e.g. 08/04 23:35 utc_naive = datetime.datetime.strptime(input, '%Y-%m-%dT%H:%M:%SZ') utc = utc_naive.replace(pytz.utc) bst = utc.astimezone(TIMEZONE) return bst.strftime('%d/%m %H:%M') </code></pre> <p>So, how do you get the OS to give you the local timezone? Well, that's different for different platforms, and Python has nothing built in to help. But there are a few different third-party libraries that do, such as <a href="http://labix.org/python-dateutil" rel="nofollow"><code>dateutil</code></a>. For example:</p> <pre><code>def RFC3339_to_localHHMM(input): # Take an XML date (2013-04-08T22:35:00Z) # return e.g. 08/04 23:35 utc = datetime.datetime.strptime(input, '%Y-%m-%dT%H:%M:%SZ') bst = utc.astimezone(dateutil.tz.tzlocal()) return bst.strftime('%d/%m %H:%M') </code></pre> <p>But now we've come full circle. If all you wanted was the local timezone, you didn't really need the timezone at all (at least for your simple use case). So, this is only necessary if you need to support any timezone, and <em>also</em> want to be able to, e.g., default to your local timezone (without having to write two copies of all of your code for the aware and naive cases).</p> <p>(Also, if you're going to use <code>dateutil</code> in the first place, you might want to use it for more than just getting the timezone—it can basically replacing everything you're doing with both <code>datetime</code> and <code>pytz</code>.)</p> <p>Of course there are other options besides these libraries—search PyPI, Google, and/or the ActiveState recipes.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload