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

  3. 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
  4. Slot Games and Casino in NC - JTR Hub
    In 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

    ReplyDelete
  5. 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
    is one of the best ways to get leads in the current time.

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

    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