Resumen
La clase TimeZoneInfo se puede utilizar para leer o asignar una zona horaria a un objeto datetime de Python.
Debate
Los objetos nativos de datetime no son sensibles a la zona horaria. Con la asignación de una zona horaria a un objeto datetime se podrán realizar operaciones relacionadas con la zona horaria. Por ejemplo, puede utilizar la zona horaria asociada con un valor horario y convertirla a otra zona horaria.
Sintaxis
TimeZoneInfo (time_zone_id)
Parámetro | Explicación | Tipo de datos |
time_zone_id | A valid time zone ID. A list of available time zone IDs can be obtained from the ListTimeZones function. | String |
Descripción general del método
Método | Explicación |
tzname (dt) | Returns the time zone name corresponding to the Python datetime object, dt, as a string. |
Métodos
tzname (dt)
Parámetro | Explicación | Tipo de datos |
dt | A reference to a Python datetime object. (El valor predeterminado es None) | DateTime |
Valor de retorno
Tipo de datos | Explicación |
String | The time zone name corresponding to the datetime object, dt. |
Returns the time zone name corresponding to the datetime object, dt, as a string.
Muestra de código
TimeZoneInfo example 1
The following script applies a 'Pacific Standard Time' time zone to a Python datetime object. It then loops through each month to demonstrate how the time zone name will change to 'Pacific Daylight Time' during the summer in observance of Daylight Savings Time.
import arcpy
import datetime
tzinfo = arcpy.time.TimeZoneInfo('Pacific Standard Time')
time = datetime.datetime(2011, 1, 1, tzinfo=tzinfo)
for delta in range(1, 13):
next_date = time + arcpy.time.EsriTimeDelta(1 * delta, "months")
print next_date, tzinfo.tzname(next_date)
TimeZoneInfo example 2
The following script demonstrates how to convert a datetime value in Pacific Standard Time to Eastern Standard Time.
import arcpy
import datetime
from_tzinfo = arcpy.time.TimeZoneInfo('Pacific Standard Time')
target_tzInfo = arcpy.time.TimeZoneInfo('Eastern Standard Time')
from_time = datetime.datetime.now(from_tzinfo)
print "target_time =", str(from_time.astimezone(target_tzInfo))