See More

#!/usr/bin/env PYTHONHASHSEED=1234 python3 # Copyright 2014-2019 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. # Reproduce book environment import random random.seed(1234) import logging from pprint import pprint from sys import stdout as STDOUT # Write all output to a temporary directory import atexit import gc import io import os import tempfile TEST_DIR = tempfile.TemporaryDirectory() atexit.register(TEST_DIR.cleanup) # Make sure Windows processes exit cleanly OLD_CWD = os.getcwd() atexit.register(lambda: os.chdir(OLD_CWD)) os.chdir(TEST_DIR.name) def close_open_files(): everything = gc.get_objects() for obj in everything: if isinstance(obj, io.IOBase): obj.close() atexit.register(close_open_files) # Example 1 import time now = 1552774475 local_tuple = time.localtime(now) time_format = '%Y-%m-%d %H:%M:%S' time_str = time.strftime(time_format, local_tuple) print(time_str) # Example 2 time_tuple = time.strptime(time_str, time_format) utc_now = time.mktime(time_tuple) print(utc_now) # Example 3 import os if os.name == 'nt': print("This example doesn't work on Windows") else: parse_format = '%Y-%m-%d %H:%M:%S %Z' depart_sfo = '2019-03-16 15:45:16 PDT' time_tuple = time.strptime(depart_sfo, parse_format) time_str = time.strftime(time_format, time_tuple) print(time_str) # Example 4 try: arrival_nyc = '2019-03-16 23:33:24 EDT' time_tuple = time.strptime(arrival_nyc, time_format) except: logging.exception('Expected') else: assert False # Example 5 from datetime import datetime, timezone now = datetime(2019, 3, 16, 22, 14, 35) now_utc = now.replace(tzinfo=timezone.utc) now_local = now_utc.astimezone() print(now_local) # Example 6 time_str = '2019-03-16 15:14:35' now = datetime.strptime(time_str, time_format) time_tuple = now.timetuple() utc_now = time.mktime(time_tuple) print(utc_now) # Example 7 import pytz arrival_nyc = '2019-03-16 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) # Example 8 pacific = pytz.timezone('US/Pacific') sf_dt = pacific.normalize(utc_dt.astimezone(pacific)) print(sf_dt) # Example 9 nepal = pytz.timezone('Asia/Katmandu') nepal_dt = nepal.normalize(utc_dt.astimezone(nepal)) print(nepal_dt)