import { useEffect, useState } from 'react';
/**
 * 自定义hook
 * 登录页时间
 * @returns {object} 返回时间信息，包含time、dayofweek、date
 */
const useTime = () => {
  const [currentDate, setCurrentDate] = useState({}); 
  useEffect(() => {
    let loginTimeInterval = null;
    if (loginTimeInterval) clearInterval(loginTimeInterval), (loginTimeInterval = null);
    loginTimeInterval = setInterval(() => {
      const date = new Date();
      const weekday = ['周日', '周一', '周二', '周三', '周四', '周五', '周六'];
      const time = `${date.getHours() < 10 ? `0${date.getHours()}` : date.getHours()}:${
        date.getMinutes() < 10 ? `0${date.getMinutes()}` : date.getMinutes()
      }:${date.getSeconds() < 10 ? `0${date.getSeconds()}` : date.getSeconds()}`;
      setCurrentDate({
        time,
        dayofweek: weekday[date.getDay()],
        date: date.pattern('yyyy/MM/dd'),
      });
    }, 1000);
    return () => {
      loginTimeInterval && clearInterval(loginTimeInterval);
    };
  }, []);
  return currentDate;
};
export default useTime;
