#!/usr/bin/env python # coding: utf-8 # In[1]: # Copyright 2014 Brett Slatkin, Pearson Education Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # Preamble to mimick book environment import logging from pprint import pprint from sys import stdout as STDOUT # In[2]: # Example 1 from time import localtime, strftime now = 1407694710 local_tuple = localtime(now) time_format = '%Y-%m-%d %H:%M:%S' time_str = strftime(time_format, local_tuple) print(time_str) # In[3]: # Example 2 from time import mktime, strptime time_tuple = strptime(time_str, time_format) utc_now = mktime(time_tuple) print(utc_now) # In[4]: # Example 3 parse_format = '%Y-%m-%d %H:%M:%S %Z' depart_sfo = '2014-05-01 15:45:16 PDT' time_tuple = strptime(depart_sfo, parse_format) time_str = strftime(time_format, time_tuple) print(time_str) # In[5]: # Example 4 try: arrival_nyc = '2014-05-01 23:33:24 EDT' time_tuple = strptime(arrival_nyc, time_format) except: logging.exception('Expected') else: assert False # In[6]: # Example 5 from datetime import datetime, timezone now = datetime(2014, 8, 10, 18, 18, 30) now_utc = now.replace(tzinfo=timezone.utc) now_local = now_utc.astimezone() print(now_local) # In[7]: # Example 6 time_str = '2014-08-10 11:18:30' now = datetime.strptime(time_str, time_format) time_tuple = now.timetuple() utc_now = mktime(time_tuple) print(utc_now) # In[8]: # Example 7 import pytz arrival_nyc = '2014-05-01 23:33:24' nyc_dt_naive = datetime.strptime(arrival_nyc, time_format) eastern = pytz.timezone('US/Eastern') nyc_dt = eastern.localize(nyc_dt_naive) utc_dt = pytz.utc.normalize(nyc_dt.astimezone(pytz.utc)) print(utc_dt) # In[9]: # Example 8 pacific = pytz.timezone('US/Pacific') sf_dt = pacific.normalize(utc_dt.astimezone(pacific)) print(sf_dt) # In[10]: # Example 9 nepal = pytz.timezone('Asia/Katmandu') nepal_dt = nepal.normalize(utc_dt.astimezone(nepal)) print(nepal_dt)