HOW TO

Set a time zone and specify daylight saving time when publishing a feature layer using ArcPy

Last Published: December 29, 2020

Summary

When publishing a feature layer from ArcGIS Pro, it is possible to set a time zone and specify daylight saving time using ArcPy by modifying the config properties for publishing.

Procedure

  1. Import the necessary modules.
import arcpy
import os
import xml.dom.minidom as DOM
import codecs
import xml.etree.ElementTree as ET
  1. Define the function to enable the necessary extensions, and read the Service Definition Draft (.sddraft) file. Find all elements named TypeName to define the server object extension (SOE) names, enable feature access, and write to the .sddraft file.
def enable_extensions(sddraftPath, soe):
  doc = DOM.parse(sddraftPath)
  
  typeNames = doc.getElementsByTagName('TypeName')
  for typeName in typeNames:
     if typeName.firstChild.data == soe:
       extension = typeName.parentNode
       for extElement in extension.childNodes:
         if extElement.tagName == 'Enabled':
           extElement.firstChild.data = 'true'

  f = open(sddraftPath, 'w')
  doc.writexml(f)
  f.close()
  1. ​​​​​Define the function to configure the properties of an extension, and read the .sddraft file. The SOE is the extension to which properties must be added. Find all elements named TypeName to define the server object extension (SOE) names, enable feature access, and write to the .sddraft file.
def enable_configproperties(sddraftPath, soe, property_set):  
  doc = DOM.parse(sddraftPath)

  typeNames = doc.getElementsByTagName('TypeName')
  for typeName in typeNames:
        
    if typeName.firstChild.data == soe:
      extension = typeName.parentNode
            
      # prp = extension.childNodes.getElementsByTagNameNS('PropertyArray')
      for extElement in extension.childNodes:                     
        if extElement.tagName == 'Definition':
          for definition in extElement.childNodes:
            if definition.tagName == 'ConfigurationProperties':
              for config_prop in definition.childNodes:
                if config_prop.tagName == 'PropertyArray':
                  for prop in property_set:
                    prop_set = doc.createElement("PropertySetProperty")
                    attr = doc.createAttribute("xsi:type")
                    attr.value = "typens:PropertySetProperty"
                    prop_set.setAttributeNode(attr)
                    prop_key = doc.createElement("Key")
                    txt = doc.createTextNode(prop["key"])
                    prop_key.appendChild(txt)
                    prop_set.appendChild(prop_key)
                    prop_value = doc.createElement("Value")
                    attr = doc.createAttribute("xsi:type")
                    attr.value = "xs:string"
                    prop_value.setAttributeNode(attr)
                    txt = doc.createTextNode(prop["value"])
                    prop_value.appendChild(txt)
                    prop_set.appendChild(prop_value)
                    config_prop.appendChild(prop_set)
                                        
  f = open(sddraftPath, 'w')
  doc.writexml(f)
  f.close()
  1. ​​​Connect to the specified portal, and specify the login credentials. Publish a map in the current ArcGIS project (.aprx). Set the time zone, and specify the daylight saving time. Enable the extensions on the map server, and publish the feature layer.
Note:
There may be an error upon connecting to the portal through arcpy.SignInToPortal(). Refer to Problem: Unable to log in to ArcGIS Online using ArcPy for more information.
if __name__ == "__main__":
  arcpy.SignInToPortal("https://www.arcgis.com/", "username", "password")
  # aprx = arcpy.mp.ArcGISProject("CURRENT")
  aprx = arcpy.mp.ArcGISProject(r"aprx path")
  m = aprx.listMaps('<Map_Name>')[0]
  # if to share selected layers
  # layers = m.listLayers()
  # lyr = layers[0]
  name = "service name"
  in_server = "https://server.domain.com:6443/arcgis/"
  sddraft = m.getWebLayerSharingDraft('FEDERATED_SERVER', 'MAP_IMAGE', name)
  sddraft.federatedServerUrl = in_server
  sddraft.copyDataToServer = False
  sddraft.exportToSDDraft("location and name of sddraft")

  #The desired timezone can be in UTC or PDT
  property_set = [{
  "key": "dateFieldsRespectsDayLightSavingTime",
  "value": "true"
  },
  {
  "key": "dateFieldsTimezoneID",
  "value": "<desired_timezone>"
  }]
  # To set time zone on hosted feature service, soe = "FeatureServer"
  enable_configproperties(sddraftPath, soe="MapServer", property_set=property_set)

  # Enable extensions on the map server
  enable_extensions(sddraftPath, "FeatureServer") 
  # enable_extensions(sddraftPath, "VersionManagementServer") 
  # enable_extensions(sddraftPath, "LRServer")
  
  print("Start Staging")  
  arcpy.StageService_server("location and name of sddraft", "location and name of sd") 
  print("Start Uploadinb")  
  arcpy.UploadServiceDefinition_server("location and name of sd", in_server)
  print("Finish publishing")     

Article ID: 000024162

Software:
  • ArcGIS Pro 2 x

Receive notifications and find solutions for new or common issues

Get summarized answers and video solutions from our new AI chatbot.

Download the Esri Support App

Related Information

Discover more on this topic

Get help from ArcGIS experts

Contact technical support

Download the Esri Support App

Go to download options