Displaying Time in Alternate Time Zones with Python's pytz
Introduction:
Many applications require converting and displaying time across different time zones. Determining the current time in any given zone can be a challenge without the right tools.
Solution:
The Python pytz library offers a convenient way to manipulate time zones and display time in a different time zone. The following is an elegant solution:
from datetime import datetime from pytz import timezone south_africa = timezone('Africa/Johannesburg') sa_time = datetime.now(south_africa) print(sa_time.strftime('%Y-%m-%d_%H-%M-%S'))
This code retrieves the current time in the South African time zone and prints it in the specified format.
Explanation:
This method provides a straightforward and extensible way to display time in any desired time zone.
The above is the detailed content of How Can Python's pytz Library Display Time in Different Time Zones?. For more information, please follow other related articles on the PHP Chinese website!