# 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
# 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)
2014-08-11 02:18:30
# Example 2
from time import mktime, strptime
time_tuple = strptime(time_str, time_format)
utc_now = mktime(time_tuple)
print(utc_now)
1407694710.0
# 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)
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-4-bf69e8d4935d> in <module>() 2 parse_format = '%Y-%m-%d %H:%M:%S %Z' 3 depart_sfo = '2014-05-01 15:45:16 PDT' ----> 4 time_tuple = strptime(depart_sfo, parse_format) 5 time_str = strftime(time_format, time_tuple) 6 print(time_str) /Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/_strptime.py in _strptime_time(data_string, format) 492 """Return a time struct based on the input string and the 493 format string.""" --> 494 tt = _strptime(data_string, format)[0] 495 return time.struct_time(tt[:time._STRUCT_TM_ITEMS]) 496 /Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/_strptime.py in _strptime(data_string, format) 335 if not found: 336 raise ValueError("time data %r does not match format %r" % --> 337 (data_string, format)) 338 if len(data_string) != found.end(): 339 raise ValueError("unconverted data remains: %s" % ValueError: time data '2014-05-01 15:45:16 PDT' does not match format '%Y-%m-%d %H:%M:%S %Z'
# 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
# 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)
2014-08-11 02:18:30+08:00
# 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)
1407640710.0
# 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)
--------------------------------------------------------------------------- ImportError Traceback (most recent call last) <ipython-input-8-0baf51d4c684> in <module>() 1 # Example 7 ----> 2 import pytz 3 arrival_nyc = '2014-05-01 23:33:24' 4 nyc_dt_naive = datetime.strptime(arrival_nyc, time_format) 5 eastern = pytz.timezone('US/Eastern') ImportError: No module named 'pytz'
# Example 8
pacific = pytz.timezone('US/Pacific')
sf_dt = pacific.normalize(utc_dt.astimezone(pacific))
print(sf_dt)
--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-9-4d91cc1e9f4f> in <module>() 1 # Example 8 ----> 2 pacific = pytz.timezone('US/Pacific') 3 sf_dt = pacific.normalize(utc_dt.astimezone(pacific)) 4 print(sf_dt) NameError: name 'pytz' is not defined
# Example 9
nepal = pytz.timezone('Asia/Katmandu')
nepal_dt = nepal.normalize(utc_dt.astimezone(nepal))
print(nepal_dt)
--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-10-4b2043796446> in <module>() 1 # Example 9 ----> 2 nepal = pytz.timezone('Asia/Katmandu') 3 nepal_dt = nepal.normalize(utc_dt.astimezone(nepal)) 4 print(nepal_dt) NameError: name 'pytz' is not defined