Converting Local Time Strings to UTC
Converting a local time string to a UTC time string can be a complex task, as timezones and Daylight Saving Time (DST) can introduce ambiguity. This article provides a detailed explanation of how to achieve this conversion using the datetime and pytz modules.
Steps:
Example:
The following example converts the local time string "2001-2-3 10:11:12" in the "America/Los_Angeles" timezone to its equivalent UTC time:
from datetime import datetime import pytz local = pytz.timezone("America/Los_Angeles") naive = datetime.strptime("2001-2-3 10:11:12", "%Y-%m-%d %H:%M:%S") local_dt = local.localize(naive, is_dst=None) utc_dt = local_dt.astimezone(pytz.utc) print(utc_dt.strftime("%Y-%m-%d %H:%M:%S"))
This will output: "2001-02-03 02:11:12".
The above is the detailed content of How Can I Convert Local Time Strings to UTC Using Python?. For more information, please follow other related articles on the PHP Chinese website!