/*
 * @Descripttion: 当前组件信息
 * @version: 1.0.0
 * @Author: 无尘
 * @Date: 2019-09-11 18:18:32
 * @LastEditors: 无尘
 * @LastEditTime: 2019-09-25 17:10:43
 */
/* 全局过滤器 */

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;
};

/**
 * 时间戳---> 年-月-日 时:分:秒
 * @param timestamp
 */
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}`;
};

/**
 * 时间戳---> 年.月.日 时:分:秒
 * @param timestamp
 */
const formatTimeYmdHms = 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}`;
};

/**
 * 时间戳---> 年-月-日
 * @param timestamp
 */
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}`;
};

/**
 * 时间戳---> 时:分:秒
 * @param timestamp
 */
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,
  formatTimeYmdHms
};
