How to do it...

We start by first installing the Python pytz time zone module, using pip. We type the following command in a command processor prompt:

    pip install pytz
In this book, we are using Python 3.6, which comes with the pip module built-in. If you are using an older version of Python, then you may have to install the pip module first.

When successful, we get the following result:

The preceding screenshot shows that the command downloaded the .whl format. If you have not done so, you might have to install the Python wheel module first.

This installed the Python pytz module into the site-packages folder, so now we can import this module from our Python GUI code.

We can list all the existing time zones by running the following code, which will display the time zones in our ScrolledText widget. First, we add a new Button widget to our GUI:

import pytz 
class OOP():
# TZ Button callback
def allTimeZones(self):
for tz in pytz.all_timezones:
self.scr.insert(tk.INSERT, tz + ' ')

def createWidgets(self):
# Adding a TZ Button
self.allTZs = ttk.Button(self.widgetFrame,
text=self.i18n.timeZones,
command=self.allTimeZones)
self.allTZs.grid(column=0, row=9, sticky='WE')

Clicking our new Button widget results in the following output:

GUI.py

After we install the tzlocal Python module, we can print our current locale by running the following code:

 
# TZ Local Button callback
def localZone(self):
from tzlocal import get_localzone
self.scr.insert(tk.INSERT, get_localzone())

def createWidgets(self):
# Adding local TZ Button
self.localTZ = ttk.Button(self.widgetFrame,
text=self.i18n.localZone,
command=self.localZone
self.localTZ.grid(column=1, row=9, sticky='WE')

We have internationalized the strings of our two new Buttons in Resources.py.

English version:

        self.timeZones = "All Time Zones" 
self.localZone = "Local Zone"

German version:

        self.timeZones = "Alle Zeitzonen" 
self.localZone = "Lokale Zone"

Clicking our new button now tells us which time zone we are in (hey, we didn't know that, did we…).

GUI.py

We can now translate our local time to a different time zone. Let's use USA Eastern Standard Time as an example.

We display our current local time in our unused Label 2 by improving our existing code.

When we run the code, our internationalized Label 2 (displayed as Etikette 2 in German) will display the current local time:

GUI.py

We can now change our local time to US EST by first converting it to Coordinated Universal Time (UTC) and then applying the timezone function from the imported pytz module:

import pytz 
class OOP():
# Format local US time with TimeZone info
def getDateTime(self):
fmtStrZone = "%Y-%m-%d %H:%M:%S %Z%z"
# Get Coordinated Universal Time
utc = datetime.now(timezone('UTC'))
print(utc.strftime(fmtStrZone))

# Convert UTC datetime object to Los Angeles TimeZone
la = utc.astimezone(timezone('America/Los_Angeles'))
print(la.strftime(fmtStrZone))

# Convert UTC datetime object to New York TimeZone
ny = utc.astimezone(timezone('America/New_York'))
print(ny.strftime(fmtStrZone))

# update GUI label with NY Time and Zone
self.lbl2.set(ny.strftime(fmtStrZone))

Clicking the button, now renamed as New York, results in the following output:

GUI.py

Our Label 2 got updated with the current time in New York and we are printing the UTC times of the cities, Los Angeles and New York, with their respective time zone conversions, relative to the UTC time on the Eclipse console, using a US date formatting string:

GUI.py

UTC never observes Daylight Saving Time. During Eastern Daylight Time (EDT) UTC is four hours ahead and during Standard Time (EST) it is five hours ahead of the local time.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.2.225