code: def _format_datetime(dttm):
      
  
      if dttm.tzinfo is None or dttm.tzinfo.utcoffset(dttm) is None:
          # dttm is timezone-naive; assume UTC
          zoned = pytz.utc.localize(dttm)
      else:
          zoned = dttm.astimezone(pytz.utc)
      ts = zoned.strftime("%Y-%m-%dT%H:%M:%S")
      ms = zoned.strftime("%f")
      precision = getattr(dttm, "precision", None)
      if precision == "second":
          pass  # Already precise to the second
      elif precision == "millisecond":
          ts = ts + "." + ms[:3]
      elif zoned.microsecond > 0:
          ts = ts + "." + ms.rstrip("0")
      return ts + "Z"
docs: Convert a datetime object into a valid STIX timestamp string.
  
      1. Convert to timezone-aware
      2. Convert to UTC
      3. Format in ISO format
      4. Ensure correct precision
         a. Add subsecond value if non-zero and precision not defined
      5. Add "Z"