code: def convert_dttm(
      cls,
      target_type: str,
      dttm: datetime,
      db_extra: Optional[dict[str, Any]] = None,
  ) -> Optional[str]:
      db_extra = db_extra or {}
  
      sqla_type = cls.get_sqla_column_type(target_type)
  
      if isinstance(sqla_type, types.DateTime):
          es_version = db_extra.get("version")
          # The elasticsearch CAST function does not take effect for the time zone
          # setting. In elasticsearch7.8 and above, we can use the DATETIME_PARSE
          # function to solve this problem.
          supports_dttm_parse = False
          try:
              if es_version:
                  supports_dttm_parse = Version(es_version) >= Version(
                      "7.8"
                  )
          except Exception as ex:  # pylint: disable=broad-except
              logger.error(
                  "Unexpected error while convert es_version",
                  exc_info=True,
              )
              logger.exception(ex)
  
          if supports_dttm_parse:
              datetime_formatted = dttm.isoformat(
                  sep=" ", timespec="seconds"
              )
              return f"""DATETIME_PARSE('{datetime_formatted}', 'yyyy-MM-dd HH:mm:ss')"""
  
          return f"""CAST('{dttm.isoformat(timespec="seconds")}' AS DATETIME)"""
  
      return None
  
file_path: superset/superset/db_engine_specs/elasticsearch.py
imports: import logging
  from datetime import datetime
  from typing import Any, Optional
  from packaging.version import Version
  from sqlalchemy import types
  from superset.constants import TimeGrain
  from superset.db_engine_specs.base import BaseEngineSpec
  from superset.db_engine_specs.exceptions import SupersetDBAPIDatabaseError, SupersetDBAPIOperationalError, SupersetDBAPIProgrammingError
label_desc: Use image node_id 2 for calling the ElasticSearchEngineSpec obj's underlying member method code with example usage: obj.convert_dttm(cls, target_type, dttm, db_extra) and returns: None, str, dttm, str, str, datetime_formatted, str