# Buggy code
def persist(self):
    """
    Persist the modified schedule into <<configdir>>/<<default_include>>/_schedule.conf
    """
    config_dir = self.opts.get("conf_dir", None)
    if config_dir is None and "conf_file" in self.opts:
        config_dir = os.path.dirname(self.opts["conf_file"])
    if config_dir is None:
        config_dir = salt.syspaths.CONFIG_DIR

    minion_d_dir = os.path.join(
        config_dir,
        os.path.dirname(
            self.opts.get(
                "default_include", salt.config.DEFAULT_MINION_OPTS["default_include"]
            )
        ),
    )

    if not os.path.isdir(minion_d_dir):
        os.makedirs(minion_d_dir)

    schedule_conf = os.path.join(minion_d_dir, "_schedule.conf")
    log.debug("Persisting schedule")
    schedule_data = self._get_schedule(include_pillar=False)
    try:
        with salt.utils.files.fopen(schedule_conf, "wb+") as fp_:
            fp_.write(
                salt.utils.stringutils.to_bytes(
                    salt.utils.yaml.safe_dump({"schedule": schedule_data})
                )
            )
    except (IOError, OSError):
        log.error(
            "Failed to persist the updated schedule", exc_info_on_loglevel=logging.DEBUG
        )


# Traceback (SaltClientError)
2018-02-26 18:02:15,266 [salt.minion      :204 ][WARNING ][16492] Master ip address changed from 10.1.51.25 to 10.1.51.24
2018-02-26 18:02:15,616 [salt.minion      :433 ][ERROR   ][16492] Exception unsupported operand type(s) for -: 'str' and 'datetime.datetime' occurred in scheduled job
2018-02-26 18:02:16,613 [salt.minion      :433 ][ERROR   ][16492] Exception unsupported operand type(s) for -: 'str' and 'datetime.datetime' occurred in scheduled job
2018-02-26 18:02:17,611 [salt.minion      :433 ][ERROR   ][16492] Exception unsupported operand type(s) for -: 'str' and 'datetime.datetime' occurred in scheduled job
2018-02-26 18:02:18,611 [salt.minion      :433 ][ERROR   ][16492] Exception unsupported operand type(s) for -: 'str' and 'datetime.datetime' occurred in scheduled job
2018-02-26 18:02:19,611 [salt.minion      :433 ][ERROR   ][16492] Exception unsupported operand type(s) for -: 'str' and 'datetime.datetime' occurred in scheduled job
2018-02-26 18:02:20,288 [salt.minion      :204 ][WARNING ][16492] Master ip address changed from 10.1.51.24 to 10.1.51.25
2018-02-26 18:02:20,611 [salt.minion      :433 ][ERROR   ][16492] Exception unsupported operand type(s) for -: 'str' and 'datetime.datetime' occurred in scheduled job
2018-02-26 18:02:21,611 [salt.minion      :433 ][ERROR   ][16492] Exception unsupported operand type(s) for -: 'str' and 'datetime.datetime' occurred in scheduled job
2018-02-26 18:02:22,610 [salt.minion      :433 ][ERROR   ][16492] Exception unsupported operand type(s) for -: 'str' and 'datetime.datetime' occurred in scheduled job
2018-02-26 18:02:23,619 [salt.minion      :433 ][ERROR   ][16492] Exception unsupported operand type(s) for -: 'str' and 'datetime.datetime' occurred in scheduled job
2018-02-26 18:02:24,620 [salt.minion      :433 ][ERROR   ][16492] Exception unsupported operand type(s) for -: 'str' and 'datetime.datetime' occurred in scheduled job
2018-02-26 18:02:25,300 [salt.minion      :644 ][ERROR   ][16492] No master could be reached or all masters denied the minion's connection attempt.
2018-02-26 18:02:25,301 [salt.scripts     :146 ][WARNING ][16492] Fatal functionality error caught by minion handler:
Traceback (most recent call last):
File "/usr/lib/python2.7/site-packages/salt/scripts.py", line 144, in minion_process
minion.start()
File "/usr/lib/python2.7/site-packages/salt/cli/daemons.py", line 350, in start
raise SaltClientError('Minion could not connect to Master')
SaltClientError: Minion could not connect to Master
2018-02-26 18:02:25,301 [salt.scripts     :147 ][WARNING ][16492] ** Restarting minion **

# Fixed code
def persist(self):
    """
    Persist the modified schedule into <<configdir>>/<<default_include>>/_schedule.conf
    """
    config_dir = self.opts.get("conf_dir", None)
    if config_dir is None and "conf_file" in self.opts:
        config_dir = os.path.dirname(self.opts["conf_file"])
    if config_dir is None:
        config_dir = salt.syspaths.CONFIG_DIR

    minion_d_dir = os.path.join(
        config_dir,
        os.path.dirname(
            self.opts.get(
                "default_include", salt.config.DEFAULT_MINION_OPTS["default_include"]
            )
        ),
    )

    if not os.path.isdir(minion_d_dir):
        os.makedirs(minion_d_dir)

    schedule_conf = os.path.join(minion_d_dir, "_schedule.conf")
    log.debug("Persisting schedule")
    schedule_data = self._get_schedule(include_pillar=False, remove_hidden=True)
    try:
        with salt.utils.files.fopen(schedule_conf, "wb+") as fp_:
            fp_.write(
                salt.utils.stringutils.to_bytes(
                    salt.utils.yaml.safe_dump({"schedule": schedule_data})
                )
            )
    except (IOError, OSError):
        log.error(
            "Failed to persist the updated schedule", exc_info_on_loglevel=logging.DEBUG
        )