Skip to content

Instantly share code, notes, and snippets.

@nekoya
Created June 27, 2013 11:40
Show Gist options
  • Save nekoya/5875817 to your computer and use it in GitHub Desktop.
Save nekoya/5875817 to your computer and use it in GitHub Desktop.
from datetime import datetime
import pytz
def dump(dt):
print '1. strftime : %s' % dt.strftime('%Y-%m-%d %H:%M')
print '2. str : %s' % dt
print '3. as utc : %s' % dt.astimezone(pytz.utc)
print '4. 3 as local : %s' % dt.astimezone(pytz.utc).astimezone(ny)
print ''
ny = pytz.timezone('America/New_York')
print '=== create datetime with tzinfo ==='
dump(datetime(2013, 4, 10, 8, 0, tzinfo=ny))
print '=== localize naive datetime ==='
dump(ny.localize(datetime(2013, 4, 10, 8, 0)))
print '=== nomalize tzinfo ==='
dump(ny.normalize(datetime(2013, 4, 10, 8, 0, tzinfo=ny)))
@nekoya
Copy link
Author

nekoya commented Jun 27, 2013

=== create datetime with tzinfo ===
1. strftime   : 2013-04-10 08:00
2. str        : 2013-04-10 08:00:00-05:00
3. as utc     : 2013-04-10 13:00:00+00:00
4. 3 as local : 2013-04-10 09:00:00-04:00

=== localize naive datetime ===
1. strftime   : 2013-04-10 08:00
2. str        : 2013-04-10 08:00:00-04:00
3. as utc     : 2013-04-10 12:00:00+00:00
4. 3 as local : 2013-04-10 08:00:00-04:00

=== nomalize tzinfo ===
1. strftime   : 2013-04-10 09:00
2. str        : 2013-04-10 09:00:00-04:00
3. as utc     : 2013-04-10 13:00:00+00:00
4. 3 as local : 2013-04-10 09:00:00-04:00

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment