All files / time parseUnitTime.ts

100% Statements 20/20
100% Branches 0/0
100% Functions 1/1
100% Lines 20/20

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89                                  2x                                         2x       1x         1x               1x 1x 1x 1x 1x   1x     1x       1x 1x       1x 1x       1x 1x     1x   1x     2x  
/**
 * 时间数字拆分为天时分秒
 * @method parseUnitTime
 * @param {Number} time 毫秒数
 * @param {Object} spec 选项
 * @param {String} [spec.maxUnit='day'] 拆分时间的最大单位,可选 ['day', 'hour', 'minute', 'second']
 * @return {Object} 拆分完成的天时分秒
 * @example
 * import { parseUnitTime } from '@spore-ui/tskit';
 * console.info( parseUnitTime(12345 * 67890) );
 * // Object {day: 9, hour: 16, minute: 48, second: 22, ms: 50}
 * console.info( parseUnitTime(12345 * 67890, {maxUnit : 'hour'}) );
 * // Object {hour: 232, minute: 48, second: 22, ms: 50}
 */
 
import { TypeDate } from '../types';
 
const TimeUnits = {
  day: 24 * 60 * 60 * 1000,
  hour: 60 * 60 * 1000,
  minute: 60 * 1000,
  second: 1000,
} as const;
 
export type TypeTimeUnit = keyof typeof TimeUnits;
 
export interface TypeParseUnitTimeOptions {
  maxUnit?: TypeTimeUnit;
}
 
export interface TypeTimeUnitInfo {
  day: number;
  hour: number;
  minute: number;
  second: number;
  ms: number;
}
 
export function parseUnitTime(
  time: TypeDate,
  spec?: TypeParseUnitTimeOptions,
): TypeTimeUnitInfo {
  const conf: TypeParseUnitTimeOptions = {
    maxUnit: 'day',
    ...spec,
  };
 
  const data: TypeTimeUnitInfo = {
    day: 0,
    hour: 0,
    minute: 0,
    second: 0,
    ms: 0,
  };
 
  const maxUnit = Number(TimeUnits[conf.maxUnit]);
  const uDay = TimeUnits.day;
  const uHour = TimeUnits.hour;
  const uMinute = TimeUnits.minute;
  const uSecond = TimeUnits.second;
 
  let timeValue: number = Number(time);
 
  if (maxUnit >= uDay) {
    data.day = Math.floor(timeValue / uDay);
  }
 
  if (maxUnit >= uHour) {
    timeValue -= data.day * uDay;
    data.hour = Math.floor(timeValue / uHour);
  }
 
  if (maxUnit >= uMinute) {
    timeValue -= data.hour * uHour;
    data.minute = Math.floor(timeValue / uMinute);
  }
 
  if (maxUnit >= uSecond) {
    timeValue -= data.minute * uMinute;
    data.second = Math.floor(timeValue / uSecond);
  }
 
  data.ms = timeValue - data.second * uSecond;
 
  return data;
}
 
export default parseUnitTime;