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.

 

Comments

  1. Replies
    1. 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

      Delete
  2. 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.
    advanced excel course in Delhi
    advanced excel course in Noida

    ReplyDelete
  3. 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.
    Tally training institute in delhi
    Tally training institute in Noida

    ReplyDelete
  4. 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.
    Python with machine learning training in delhi
    Python with machine learning training in Noida

    ReplyDelete
  5. 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.
    Tally training institute delhi
    Tally training institute in Noida

    ReplyDelete
  6. 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.
    Tally training institute delhi
    Tally training institute in Noida

    ReplyDelete
  7. 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.
    autocad training institute in Delhi
    autocad training institute in Noida

    ReplyDelete
  8. 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

    ReplyDelete
  9. Thanks for the information, I agree with this information!

    open commodity trading account

    ReplyDelete
  10. 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

  11. Great 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

    ReplyDelete
  12. 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

  13. 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 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

    ReplyDelete

Post a Comment

Popular posts from this blog

WinPython - Portable Python that you can run from a usb drive on any Windows machine

Setting up a virtual environment in Python