Skip to content

Instantly share code, notes, and snippets.

@arthuralvim
Created August 2, 2016 11:53
Show Gist options
  • Save arthuralvim/a356bcee3ff4b3e113a2d53ed27e6c99 to your computer and use it in GitHub Desktop.
Save arthuralvim/a356bcee3ff4b3e113a2d53ed27e6c99 to your computer and use it in GitHub Desktop.
Basic datetime manipulation in Python.
# -*- coding: utf-8 -*-
import datetime
from pytz import timezone
from pytz import utc
fmt = '%d/%m/%Y %H:%M:%S %Z'
date_utc_now = datetime.datetime.utcnow()
print date_utc_now.strftime(fmt)
date_string = '02/10/2015 10:05:10 BRT'
date_datetime = datetime.datetime.strptime(date_string, fmt)
america = timezone('America/Recife')
loc_datetime = america.localize(date_datetime)
print loc_datetime.strftime(fmt)
loc_utc_datetime = loc_datetime.astimezone(utc)
print loc_utc_datetime.strftime(fmt)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment