Specify which timezone a datetime is in, in python -


i have datetime database, datetime utc datetime. when pull db, unaware of timezone. need do, convert datetime "seconds epoch" time function. problem this, system's time in pst , not able change specific reasons.

so, want is, take datetime database, , tell python datetime utc datetime. every way have done that, results in losing time or gaining time due timezone conversions. again, not trying convert time, trying specify utc.

if can great.

thanks!

example

assume database_function() returns datetime data type '2013-06-01 01:06:18'

datetime = database_function() epoch = datetime.strftime('%s')  pytz.utc.localize(database_function()).datetime.strftime('%s') datetime.replace(tzinfo=pytz.utc).datetime.strftime('%s') 

both of these return epoch timestamp of 1370077578 but, should return timestamp of 1370048778 per http://www.epochconverter.com/ remember, timestamp utc timestamp

using fabolous pytz:

import datetime, pytz dt = datetime.datetime(...) utc_dt = pytz.utc.localize(dt) 

this creates tz-aware datetime object, in utc.


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -