Frequently asked question

What are the alternatives to the retired arcpy.time python module?

Last Published: October 11, 2022

Answer

Attempting to access the arcpy.time module generates an error similar to the following:

module 'arcpy' has no attribute 'time'

The arcpy.time python module was deprecated and removed at ArcGIS Pro version 3.0.

In addition to Map Time, Layer Time and Time Zone functionality in the arcpy.mp python module, ArcGIS Pro 3.0 has been updated to Python 3.9, which has additional time support. Below are examples of common workflows.

Alternatives to arcpy.time.EsriTimeDelta

There are a couple of options to use core Python, that is, non-ArcPy time deltas. Firstly, there is the datetime.timedelta type. The datetime.timedelta supports units of days, seconds, and milliseconds. Secondly, the dateutil library relativedelta type supports all date units, such as months, years, etc.
More information on datetime.timedelta and dateutil.relativedelta can be found in the Python 3.9.13 Documentation: datetime — Basic date and time types. The following is an example:

from datetime import datetime
from dateutil.relativedelta import relativedelta

time = datetime(2011, 1, 1)

for delta in range(0, 12):
    next_date = time + relativedelta(months=delta)
    print(next_date)

Starting at ArcGIS Pro version 2.8, arcpy.mp added support for LayerTime and MapTime. These classes do not use EsriTimeDeltas. LayerTime intervals are expressed as timeStepInterval and timeStepIntevalUnits. MapTime intervals are expressed as currentTimeSpan, currentTimeSpanUnits, timeStepInteval and timeStepIntevalUnits.

The MapTime class also has a timeStep method that returns a new time based on a given reference time and a time interval. The Python datetime object returned from the timeStep method can be used to set the currentTimeStart and currentTimeEnd properties. To create a temporal map series, use the timeStep method to iterate through time values.

Here is an example of LayerTime timeStepInterval:

import arcpy
aprx = arcpy.mp.ArcGISProject('C:/Project/Temperature.aprx')
m = aprx.listMaps('USA')[0]
lyr = m.listLayers('temperature')[0]
t = lyr.time
print(t.timeStepInterval)
print(t.timeStepIntervalUnits)

Here is an example of MapTime currentTimeSpan:

import arcpy
aprx = arcpy.mp.ArcGISProject('C:/Project/Temperature.aprx')
lyt = aprx.listLayouts()[0]
mf = lyt.listElements('MAPFRAME_ELEMENT')[0]
mt = mf.time
print(mt.currentTimeSpan)
print(mt.currentTimeSpanUnits)

Alternatives to arcpy.time.ListTimeZones

Use arcpy.mp.ListTimeZones. For example:

import arcpy
tzList = arcpy.mp.ListTimeZones("*Canada*")
print(tzList)

Alternatives to arcpy.time.TimeZoneInfo

Use zoneinfo, which is new in Python 3.9. More information can be found in the Python 3.9.13 Documentation: zoneinfo — IANA time zone support

Zoneinfo example 1:

from datetime import datetime
from zoneinfo import ZoneInfo
from dateutil.relativedelta import relativedelta

tzinfo = ZoneInfo('US/Pacific')
time = datetime(2011, 1, 1, tzinfo=tzinfo)

for delta in range(0, 12):
    next_date = time + relativedelta(months=delta)
    print(next_date, tzinfo.tzname(next_date))

Zoneinfo example 2:

from datetime import datetime
from zoneinfo import ZoneInfo

from_tzinfo = ZoneInfo('US/Pacific')
target_tzinfo = ZoneInfo('US/Eastern')
from_time = datetime.now(from_tzinfo)
print("target_time =", str(from_time.astimezone(target_tzinfo)))

Article ID:000027752

Get help from ArcGIS experts

Contact technical support

Download the Esri Support App

Go to download options

Discover more on this topic