code: def _closest_date(
      target_dt, date_list, before_target=None
  ) -> datetime.date | None:
      """
      Find the date in a list closest to the target date.
  
      An optional parameter can be given to get the closest before or after.
  
      :param target_dt: The target date
      :param date_list: The list of dates to search
      :param before_target: closest before or after the target
      :returns: The closest date
      """
  
      def time_before(d):
          return (
              target_dt - d
              if d <= target_dt
              else datetime.timedelta.max
          )
  
      def time_after(d):
          return (
              d - target_dt
              if d >= target_dt
              else datetime.timedelta.max
          )
  
      def any_time(d):
          return target_dt - d if d < target_dt else d - target_dt
  
      if before_target is None:
          return min(date_list, key=any_time).date()
      if before_target:
          return min(date_list, key=time_before).date()
      else:
          return min(date_list, key=time_after).date()
  
imports: from __future__ import annotations
  import datetime
label_desc: Use image node_id 2 for calling a global function with example usage: _closest_date(target_dt, date_list, before_target) and returns: unknown, unknown, unknown, min, min, min