Skip to content

Instantly share code, notes, and snippets.

@lossurdo
Last active February 20, 2020 18:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lossurdo/955768a74a8ae0a4fc1886486914c693 to your computer and use it in GitHub Desktop.
Save lossurdo/955768a74a8ae0a4fc1886486914c693 to your computer and use it in GitHub Desktop.
Convert UTC to your time zone
#!/usr/bin/python3
# coding: utf-8
from datetime import datetime
# pip3 install pytz
import pytz
def convert(posix_timestamp, your_timezone='America/Sao_Paulo'):
utc_dt = datetime.utcfromtimestamp(posix_timestamp).replace(tzinfo=pytz.utc)
tz = pytz.timezone(your_timezone)
dt = utc_dt.astimezone(tz)
return dt.strftime('%Y-%m-%d %H:%M:%S') # Also print TZ: '%Y-%m-%d %H:%M:%S %Z%z'
if __name__=='__main__':
# confirmation on: https://www.epochconverter.com/
# BR time zone: Sexta-feira, 18 de Janeiro de 2013 às 18:00:00 GMT-02:00 DST
print(convert(1358539200))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment