Time and Timezones in Python
Processing of time
information is often a critical part in a Python application. In this
post, we are going to solve the following problem:
Given the departure
time and the duration of a flight from New York to London, find the arrival time in London local time.
Departure New York:
Jul 23 at 19:30, 2018
Flight time: 7h 15min
The final output will be
Task: Find the
arrival time in London local time.
To solve this problem we need to consider time zones. And that can be a challenge. Time zones are not fixed, they change depending on political decisions. For example, during the summer in New York the time zone respected is EDT (Eastern Daylight Time), but during the winter the time zone EST (Eastern Standard Time) is followed. The situation is similar in London, during summer the BST (British Summer Time) is used and in the winter GMT (Greenwich Mean Time) is followed. When does the summer end and the winter start? Well, that’s not based on meteorological considerations, but on political decisions. And as a consequence, doing time calculation can rapidly escalate into an unmanageable mess of code.
Fortunately, in Python, we have the pytz module which makes time calculations
involving time zones a breeze. Pytz can be installed with pip, e.g.
in the terminal write pip install pytz.
Specifying a time in
Python is a two step process, first we construct a naive datetime
object, naive in the sense that it is not aware of the time zone.
Next, we make the datetime object timezone aware by using the localize
method in the pytz module.The code below shows the calculations with comments.
# Python 3.6.3
import datetime, pytz
# A naive datetime object is constructed by specifying the departure time
departure_time_clock_NewYork = datetime.datetime(2018,7,19,19,30,0)
# To make the datetime object time zone aware, use the localize method
tz_NewYork = pytz.timezone('America/New_York')
departure_time_clock_NewYork = tz_NewYork.localize(departure_time_clock_NewYork)
# The flight duration is
flight_time = datetime.timedelta(hours=7, minutes=15)
# The arival time in London can now be calculated
# Note, this is the arrivale time on the clock in New York
arrival_time_clock_NewYork = departure_time_clock_NewYork + flight_time
# To find the local time we first need the time zone in London
tz_London = pytz.timezone('Europe/London')
# Nex, use the astimezone method to find the local arrival time
arrival_time_clock_London = arrival_time_clock_NewYork.astimezone(tz_London)
# Print the itinerary. We here use of the strftime method
# to clean up the presentation
fmt = '%B %d, %Y %H:%M'
print("Flight BA 176 New York (JFK) to London (LHR)")
print('Depature (JFK):\t{}'.format(departure_time_clock_NewYork.strftime(fmt)))
print('Arrival (LHR):\t{}'.format(arrival_time_clock_London.strftime(fmt)))
print('*** All times are local ***')
And that is all that is needed. Without comments this code is only a few lines long.
import datetime, pytz
# A naive datetime object is constructed by specifying the departure time
departure_time_clock_NewYork = datetime.datetime(2018,7,19,19,30,0)
# To make the datetime object time zone aware, use the localize method
tz_NewYork = pytz.timezone('America/New_York')
departure_time_clock_NewYork = tz_NewYork.localize(departure_time_clock_NewYork)
# The flight duration is
flight_time = datetime.timedelta(hours=7, minutes=15)
# The arival time in London can now be calculated
# Note, this is the arrivale time on the clock in New York
arrival_time_clock_NewYork = departure_time_clock_NewYork + flight_time
# To find the local time we first need the time zone in London
tz_London = pytz.timezone('Europe/London')
# Nex, use the astimezone method to find the local arrival time
arrival_time_clock_London = arrival_time_clock_NewYork.astimezone(tz_London)
# Print the itinerary. We here use of the strftime method
# to clean up the presentation
fmt = '%B %d, %Y %H:%M'
print("Flight BA 176 New York (JFK) to London (LHR)")
print('Depature (JFK):\t{}'.format(departure_time_clock_NewYork.strftime(fmt)))
print('Arrival (LHR):\t{}'.format(arrival_time_clock_London.strftime(fmt)))
print('*** All times are local ***')
And that is all that is needed. Without comments this code is only a few lines long.
Thankyou for posting this
ReplyDeletepython course in Delhi
python course in Noida
nice post
ReplyDeletepython training institute in south delhi
python training institute in Noida
Thankyou so much for sharing this blog.
ReplyDeletepython training institute in south delhi
python training institute in Noida
Thankyou for sharing this post.
ReplyDeletepython training institute in south delhi
python training institute in Noida
nice post
ReplyDeletepython training institute in south delhi
python training institute in Noida
Thankyou so much for sharing this informative blog
ReplyDeletepython training institute in south delhi
python training institute in Noida
High technologies solutions provides the best tally training in Delhi with 100% placement.Trainers are subject specialist and corporate professionals providing in-depth study. 100% placement guaranteed.For free demo class call at +919311002620.
ReplyDeleteTally training institute delhi
Tally training institute in Noida
This blog is very informative!
ReplyDeleteTyre Repair Canning Vale
This blog is very useful it include very knowledgeable information. Thankyou for sharing this blog with us. If anyone want to experience certificate in bangalore can call at 9599119376 or can visit https://experiencecertificates.com/experience-certificate-provider-in-chennai.html
ReplyDelete
ReplyDeleteYour blog is very nice and interesting. Your way of writing this blog forced me to read the full blog. Being a new reader, your blog increased my interest in reading. If anyone is interested for Fake Experience Certificate in Pune here we have the chance for you, Dreamsoft is providing is Fake experience certificate in Pune. To get you experience certificate in Pune you can contact at 9599119376. or can visit our website at https://experiencecertificates.com/experience-certificate-provider-in-pune.html
Slot Games and Casino in NC - JTR Hub
ReplyDeleteIn December, Slot Games and 구리 출장안마 Casino 구리 출장샵 North Carolina 제주도 출장마사지 became the first 태백 출장안마 legal online gambling establishment in the state 양주 출장마사지 with no laws Jan 18, 2020 · Uploaded by JTVH
I always look forward read blogs about python & I want to learn python. Keep sharing this kind of content and write more about python because Python training
ReplyDeleteis one of the best ways to get leads in the current time.
Here's how you can take your possibilities 점보카지노 to win at roulette doing the same. Now, to make issues even simpler for you, here is a list the most well-liked online games of European Roulette. The main distinction between the European and the American roulette is the order of the numbers on the roulette wheel. You don't need to|need not|needn't} deposit anything to play free games with your welcome bonus.
ReplyDeleteYour content ingeniously combines deep insights with a fresh perspective, delivering a captivating narrative that challenges conventional thinking and inspires new ideas.
ReplyDeleteEnvironmental Management