function toMysqlDate(value) {
  if (!value) return null
  const d = new Date(value)
  if (Number.isNaN(d.getTime())) return null
  const pad = n => String(n).padStart(2, '0')
  const offset = 8 * 60
  const local = new Date(d.getTime() + offset * 60 * 1000)
  return local.getUTCFullYear() + '-' + pad(local.getUTCMonth() + 1) + '-' + pad(local.getUTCDate()) + ' ' + pad(local.getUTCHours()) + ':' + pad(local.getUTCMinutes()) + ':' + pad(local.getUTCSeconds())
}

function formatDate(date) {
  return toMysqlDate(date)
}

module.exports = { toMysqlDate, formatDate }
