# SoA Course In class practice Apr26 2022
###### tags: `Python3` `SoA` `MCUT`
Using Python programming language to design programs and complete requirements.
## Requirements:
1. Any data type (data structure)
2. class(constructors)
3. inheritance
4. exception
4.1 build-in
4.2 custom (user-defined)
7. Multi-threading
8. Input/Output (optional)
## Program design - Time Zone Converter
It program will do two things parallelly, first is convert time-zone and second is print local time.
In the first thread, it will convert time from local device to time-zone that user entered, then print converted date time. when user enter same time-zone from device, program will show the warning message (custom error handler & inheritance), and if user type the wrong format or other things, it also show warning message (system error handler).
Secnod, it just show the local date time.
Multi-thread also combined class and constructors.
### Main operations:
* thread 1, Enter the time zone that you want to convert then convert.
* thread 2, Print the local device time twice time.
### Flow chart:
* Thread 1 mainThread
````flow
st=>start: Start
e=>end: end thread
op1=>operation: convert timezone object to string
op3=>operation: Print local time
op4=>operation: Time zone converter
op5=>operation: try : convert the time
cond1=>condition: Same as local?(Custom Error)
cond2=>condition: is time zone valid?
cond3=>condition: except :
Path is correct?(OSError)
io=>inputoutput: try : input a time zone i.e. Asia/Taipei
io2=>inputoutput: print error message
io3=>inputoutput: print user entered data
io4=>inputoutput: print converted time
st->op1->io->cond1
cond1(no,bottom )->op5->cond2
cond1(yes, left)->io2
cond2(no, left)->io2
cond2(yes, bottom)->io3->io4->e
````
* Thread 2 printtimeThread
````flow
st=>start: Start
e=>end: end thread
op1=>operation: counter -=1
cond1=>condition: Does counter reach zero?
io1=>inputoutput: Print local time
st->cond1
cond1(no,left)->io1->op1->cond1
cond1(yes, bottom)->e
````
* Main program
````flow
st=>start: Start
e=>end: end thread
op1=>operation: create new thread :
thread1(params)&thread2(params)
op2=>operation: start new threads :
threads start then join
st->op1->op2->e
````
## Code:
``` Python
#Modified by Tsun-Ping Chen (MCUT) 2022.Apr.26
import datetime
import threading
import time
import pytz #time zone package
from tzlocal import get_localzone
exitFlag = 0
# define Python user-defined exceptions
class Error(Exception):
"""Base class for other exceptions"""
pass
class SameTimeZoneError(Error):
"""Raised when the input value is too large"""
pass
class printtimeThread(threading.Thread):
def __init__(self, threadID, name, counter):
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
self.counter = counter
def run(self):
print("Starting " + self.name)
print_time(self.name, self.counter, 2)
print("Exiting " + self.name)
class mainThread(threading.Thread):
def __init__(self, threadID, name):
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
def run(self):
print("Starting " + self.name)
convertTime()
print("Exiting " + self.name)
def print_time(threadName, delay, counter):
while counter:
if exitFlag:
threadName.exit()
time.sleep(15)
print ("Now is " + "%s" % (time.ctime(time.time())))
print("%s" % threadName)
counter -= 1
def convertTime():
local_tz = str(get_localzone())
try :
zone = input("Enter time zone, like : 'Asia/Taipei' ,as Taipei time zone \n")
if zone == local_tz :
raise SameTimeZoneError
except SameTimeZoneError:
print("'\033[2;31;40mThe timezone you entered is same as your system\033[0;0m")
dt_today = datetime.datetime.today() # Local time
try :
dt_zone = dt_today.astimezone(pytz.timezone(zone))
CZone = (dt_zone.strftime('%m/%d/%Y %H:%M'))
print(zone + " standard time: " + CZone)
except pytz.exceptions.UnknownTimeZoneError: #pytz exceptions
print(zone + "\033[2;31;0m Enter data is wrong \033[0;0m")
finally:
print('\033[2;30;45mYou enter is :' + zone + ' \033[0;0m')
convertlist1 = [local_tz, zone]
print(convertlist1,type(convertlist1))
# Create new threads
thread1 = printtimeThread(1, "Thread-1", 1)
thread2 = mainThread(2,"Thread-2")
# Start new Threads
thread1.start()
thread2.start()
thread1.join()
thread2.join()
print ("Exiting Main Thread")
```
## References materials:
* [Get system time zone from pytz](https://stackoverflow.com/questions/13218506/how-to-get-system-timezone-setting-and-pass-it-to-pytz-timezone)
* [Use pytz to convert timezone](https://stackoverflow.com/questions/10997577/python-timezone-conversion)
* [Class constructor](https://realpython.com/python-class-constructor/)
* [Confusion with sublcassing a threading class, and inheritance ](https://python-forum.io/thread-17340.html)
* [user defined exception](https://www.programiz.com/python-programming/user-defined-exception)
* [if-string-equals-example-code](https://tutorial.eyehunts.com/python/python-if-string-equals-example-code/)
* [convert an object to a string](https://www.adamsmith.haus/python/answers/how-to-convert-an-object-to-a-string-in-python)
* [How to Print Colored Text in Python](https://stackabuse.com/how-to-print-colored-text-in-python/)
* [Python 3 tutorials - tutorialspoint](https://www.tutorialspoint.com/python3/python_classes_objects.htm)
* [Python 3 tutorials - Geeks for Geeks](https://www.geeksforgeeks.org/python-3-input-function/)