Get Time Zone of Own System in Python (3 Examples)
The tutorial is structured as the table below:
Then, let’s get straight into the tutorial!
Loading the time Module
As a first step, we have to import the time module.
Example 1: Getting the Time Zone Simply with tzname
The tzname function returns two strings as a tuple in which the first string is the name of the local non-DST time zone and the second string is the name of the local DST timezone.
print("Timezone: ", time.tzname) #Time Zone: ('CET', 'CEST')
Example 2: Getting the Time Zone with localtime
The localtime function returns the current localtime as a timetuple when it’s run without any parameters and strftime function is used to get specific fields out of timetuples and format them as desired. The combination of the two is another way of getting the time zone of the system.
print(time.strftime("Time Zone: %Z", time.localtime())) #Time Zone: CET
Example 3: Getting the Time Zone and the Date with localtime
The combination of the localtime and strftime functions can be utilized even further to get the date, daily time and the time zone combined and it provides a very flexible way of formatting as well, an example can be seen below.
print(time.strftime("Date and Time Zone: %Y-%m-%d %H:%M:%S %Z, The time offset: %z", time.localtime())) #Date and Time Zone: 2022-11-11 15:52:49 CET, The time offset: +0100
Video, Further Resources & Summary
Do you need more explanations on how to get the time zone of your system? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.
The YouTube video will be added soon.
Furthermore, you could have a look at some of the other tutorials on Statistics Globe:
- Calculate Time Difference Between Two Columns of pandas DataFrame in Python
- Calculate Number of Hours, Minutes & Seconds Between Two datetimes in Python
- Calculate Number of Years, Months & Days Between Two Dates in Python
- Calculate Time Difference Between Two datetime Objects in Python
- Calculate Time Difference in Milliseconds Between Two datetimes
- How to Add & Subtract Weeks to & from Date in Python
- Add Days, Months & Years to datetime Object
This post has shown how to get the time zone of your operating system. In case you have further questions, you may leave a comment below.
This page was created in collaboration with Ömer Ekiz. You may have a look at Ömer’s author page to read more about his academic background and the other articles he has written for Statistics Globe.