/*
 * @Descripttion: 当前组件信息
 * @version: 1.0.0
 * @Author: 无尘
 * @Date: 2020-04-15 16:01:54
 * @LastEditors: 无尘
 * @LastEditTime: 2020-09-23 15:58:40
 */
/* 全局过滤器 */
/**
 * @description: 当前函数信息
 * @param {Number} timeSpan
 * @param {String} format
 * @return {String}
 * @author: 无尘
 */
const dateFormat = function(timeSpan, format) {
  if (!timeSpan) return '';
  timeSpan = timeSpan.toString().length === 10 ? timeSpan * 1000 : timeSpan;
  let date = new Date(timeSpan);
  let o = {
    'M+': date.getMonth() + 1,
    'D+': date.getDate(),
    W: '日一二三四五六'.charAt(date.getDay()),
    'h+': date.getHours(),
    'm+': date.getMinutes(),
    's+': date.getSeconds(),
    'q+': Math.floor((date.getMonth() + 3) / 3),
    S: date.getMilliseconds()
  };
  if (/(Y+)/.test(format)) {
    format = format.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
  }
  for (let k in o) {
    if (new RegExp('(' + k + ')').test(format)) {
      format = format.replace(RegExp.$1, RegExp.$1.length === 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length));
    }
  }
  return format;
};

/**
 * @description: 时间戳-- - > 年 - 月 - 日 时: 分: 秒
 * @param {Number} data
 * @return {String}
 * @author: 无尘
 */
const formatTimeStamp = function(data) {
  if (!data) {
    return '';
  }
  let date = new Date(data);
  let month = date.getMonth() + 1;
  let day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
  let newMonth = month < 10 ? '0' + month : month;
  let hours = date.getHours() < 10 ? '0' + date.getHours() : date.getHours();
  let minutes = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes();
  let seconds = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
  return `${date.getFullYear()}-${newMonth}-${day} ${hours}:${minutes}:${seconds}`;
};

/**
 * @description: 时间戳-- - > 月 - 日
 * @param {Number} data
 * @return {String}
 * @author: 无尘
 */
const timeStampToYmd = function(data) {
  if (!data) {
    return '';
  }
  let date = new Date(data);
  let month = date.getMonth() + 1;
  let day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
  let newMonth = month < 10 ? '0' + month : month;
  return `${date.getFullYear()}-${newMonth}-${day}`;
};

/**
 * @description: 时间戳-- - > 年 - 月 - 日
 * @param {Number} data
 * @return {String}
 * @author: 无尘
 */
const timeStampToHms = function(data) {
  if (!data) {
    return '';
  }
  let date = new Date(data);
  let hours = date.getHours() < 10 ? '0' + date.getHours() : date.getHours();
  let minutes = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes();
  let seconds = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
  return `${hours}:${minutes}:${seconds}`;
};

export default {
  dateFormat,
  formatTimeStamp,
  timeStampToYmd,
  timeStampToHms
};
