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
I am glad that I saw this post. It is informative blog for us and we need this type of blog thanks for share this blog, Keep posting such instructional blogs and I am looking forward for your future posts.
DeletePython Projects for Students
Data analytics is the study of dissecting crude data so as to make decisions about that data. Data analytics advances and procedures are generally utilized in business ventures to empower associations to settle on progressively Python Training in Chennai educated business choices. In the present worldwide commercial center, it isn't sufficient to assemble data and do the math; you should realize how to apply that data to genuine situations such that will affect conduct. In the program you will initially gain proficiency with the specialized skills, including R and Python dialects most usually utilized in data analytics programming and usage; Python Training in Chennai at that point center around the commonsense application, in view of genuine business issues in a scope of industry segments, for example, wellbeing, promoting and account.
Project Center in Chennai
nice post
ReplyDeletepython training institute in south delhi
python training institute in Noida
Thankyou for sharing this informative blog.
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
very informative 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
Best Advanced excel institute in Delhi/Noida is High Technologies Soloutions which provides best training in advanced excel. All the trainers have 5+ years experience in their fields. HTS focus more on practical knowledge than theory which makes training sessions more interesting and trainers focus on individual training as well.HTS provides 100% placement help. Join now!! Call at +919311002620 or visit our website.
ReplyDeleteadvanced excel course in Delhi
advanced excel course in Noida
Learn tally from a renowned institute. High technologies Solutions provides the best tally training in Delhi and Noida with 100% placement help. Trainers are subject specialist having 5+ years experience. For free demo class call at +919311002620.
ReplyDeleteTally training institute in delhi
Tally training institute in Noida
Grow your career with Python in machine learning. High technologies solutions is the best Python training institute in Delhi and Noida with 100% placement help. 5+ years experienced trainers.Join now!! Call at +919311002620.
ReplyDeletePython with machine learning training in delhi
Python with machine learning training 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
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
High technologies solutions offers the best Autocad training in Delhi and Noida with 100% placement help. Trainers at HTS are friendly, cooperative and subject specialist having more than 5 years of experience.For free demo class Call +919311002620 or visit our website.
ReplyDeleteautocad training institute in Delhi
autocad training institute in Noida
Nice post. Thanks for sharing.
ReplyDeletepython course london
I am glad that I saw this post. It is informative blog for us and we need this type of blog thanks for share this blog, Keep posting such instructional blogs and I am looking forward for your future posts. Python Projects for Students Data analytics is the study of dissecting crude data so as to make decisions about that data. Data analytics advances and procedures are generally utilized in business ventures to empower associations to settle on progressively Python Training in Chennai educated business choices. In the present worldwide commercial center, it isn't sufficient to assemble data and do the math; you should realize how to apply that data to genuine situations such that will affect conduct. In the program you will initially gain proficiency with the specialized skills, including R and Python dialects most usually utilized in data analytics programming and usage; Python Training in Chennai at that point center around the commonsense application, in view of genuine business issues in a scope of industry segments, for example, wellbeing, promoting and account. Project Center in Chennai
ReplyDeleteThanks for the information, I agree with this information!
ReplyDeleteopen commodity trading account
This blog is very informative!
ReplyDeleteTyre Repair Canning Vale
Blog has informative contents and thanks for sharing this.
ReplyDeletePython Classes in Chennai
Best Python Training in Bangalore
Your post is really good thanks for sharing these kind of post but if anyone looking for Best Consulting Firm for Fake Experience Certificate Providers in bangalore, India with Complete Documents So Dreamsoft Consultancy is the Best Place.Further Details Here- 9599119376 or VisitWebsite-https://experiencecertificates.com/experience-certificate-provider-in-bangalore.html
ReplyDelete
ReplyDeleteGreat Info, Your blog is very informative and interesting, I really liked while reading your article, the information you have mentioned in this post is really good. I am waiting for your upcoming post your ,post is really good . Otherwise if anyone want to Learn python Course So Contact Us-9311002620 Or check my website- https://www.htsindia.com/Courses/python/python-training-course
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
Your 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 Mumbai here we have the chance for you, Dreamsoft is providing is Fake experience certificate in Mumbai. To get you experience certificate in Mumbai you can contact at 9599119376. or can visit our website at https://experiencecertificates.com/experience-certificate-provider-in-mumbai.html
ReplyDeleteSlot 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.
Thank you for sharing an amazing & wonderful blog. This content is very useful, informative and valuable in order to enhance knowledge. Keep sharing this type of content with us & keep updating us with new blogs. Apart from this, if anyone who wants to join the C++ Training institute in Delhi, can contact 9311002620 or visit our website-
ReplyDeletehttps://htsindia.com/Courses/modular-courses/c-plus-plus-training-course