Note that there are some explanatory texts on larger screens.

plurals
  1. POPython pytz Converting a timestamp (string format) from one timezone to another
    primarykey
    data
    text
    <p>I have a timestamp with timezone information in string format and I would like to convert this to display the correct date/time using my local timezone. So for eg... I have</p> <pre><code>timestamp1 = 2011-08-24 13:39:00 +0800 </code></pre> <p>and I would like to convert this to say timezone offset +1000 to dsiplay</p> <pre><code>timestamp2 = 2011-08-24 15:39:00 +1000 </code></pre> <p>I have tried using pytz but couldnt find many examples showing how to use the offset information. One other link that I found on stackoverflow which depicts this exact problem is <a href="https://stackoverflow.com/questions/79797/how-do-i-convert-local-time-to-utc-in-python">here</a>. I was hoping there was some better way I could handle this using pytz. Thanks for all suggestions in advance :).</p> <p><strong>UPDATE</strong></p> <p>Thanks Cixate. I just found the solution which is very similar to yours. Found these links helpful - <a href="https://stackoverflow.com/questions/79797/how-do-i-convert-local-time-to-utc-in-python">LINK1</a> and <a href="https://stackoverflow.com/questions/6729902/python-dateutil-parser-fails">LINK2</a></p> <p>Posting the solution for everyones benefit</p> <pre><code>from datetime import datetime import sys, os import pytz from dateutil.parser import parse datestr = "2011-09-09 13:20:00 +0800" dt = parse(datestr) print dt localtime = dt.astimezone (pytz.timezone('Australia/Melbourne')) print localtime.strftime ("%Y-%m-%d %H:%M:%S") 2011-09-09 15:20:00 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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