美文网首页
持续时间格式化(Human readable duration

持续时间格式化(Human readable duration

作者: 鹿啦啦zz | 来源:发表于2020-04-25 15:38 被阅读0次

https://www.codewars.com/kata/52742f58faf5485cae000b9a/javascript

题目简介

本道题提供秒数,需要返回格式化后的可持续时间,在最后有题目详情

举个例子

formatDuration(62) // returns "1 minute and 2 seconds"
formatDuration(3662) // returns "1 hour, 1 minute and 2 seconds"
For the purpose of this Kata, a year is 365 days and a day is 24 hours.
对于这个关卡来说,一年是365天,一天是24小时

我的方法

function formatDuration (seconds) {
if(seconds==0){return 'now'}
  // Complete this function
  var s = seconds % 60
  var m = Math.floor(seconds / 60)%60
  var h = Math.floor(seconds/ (60*60))%24
  var d = Math.floor(seconds/ (60*60*24))%365
  var y = Math.floor(seconds/ (60*60*24*365))
  function fomater(val,flag){
    var str=''
    var arr=['year','day','hour','minute','second']
    if(val){
      str+= val+' '+arr[flag]
      str+=(val>1?'s':'')
      if(flag==4){
      }else if((flag==0&&(d||h||m||s))||(flag==1&&(h||m||s))||(flag==2&&(m||s))||(flag==3&&s)){
        str+=', '
      }
    }
    return str
  }
  var res = (fomater(y,0)+fomater(d,1)+fomater(h,2)+fomater(m,3)+fomater(s,4))
  if(res.indexOf(',')>-1){res=res.replace(/([^,]*),([^,]*)$/g, '$1 and$2');}
  return res
}
我的思路是这样子的:
  1. 先计算出各个组件的数值
  2. 写一个方法给各个数值加上单位逗号,最后一个组件不加
  3. 把组件拼接在一起
  4. 把最后一个替换成and

大佬的方法

function formatDuration (seconds) {
  var time = { year: 31536000, day: 86400, hour: 3600, minute: 60, second: 1 },
      res = [];

  if (seconds === 0) return 'now';
  
  for (var key in time) {
    if (seconds >= time[key]) {
      var val = Math.floor(seconds/time[key]);
      res.push(val += val > 1 ? ' ' + key + 's' : ' ' + key);
      seconds = seconds % time[key];
    }
  }
  return res.length > 1 ? res.join(', ').replace(/,([^,]*)$/,' and'+'$1') : res[0]
}
大佬的思路是这样子的
  1. 先把组件所占的秒数写在time对象中
  2. 遍历time对象,从年开始计算组件数值,有值就往res数组中塞值
  3. 把组件值用连起来,并替换最后一个逗号为and
另一种写法
   const formatDuration = ms => {
  if (ms < 0) ms = -ms;
  const time = {
    day: Math.floor(ms / 86400000),
    hour: Math.floor(ms / 3600000) % 24,
    minute: Math.floor(ms / 60000) % 60,
    second: Math.floor(ms / 1000) % 60,
    millisecond: Math.floor(ms) % 1000
  };
  return Object.entries(time)
    .filter(val => val[1] !== 0)
    .map(([key, val]) => `${val} ${key}${val !== 1 ? 's' : ''}`)
    .join(', ');
};

// 事例
formatDuration(1001); // '1 second, 1 millisecond'
formatDuration(34325055574); 
// '397 days, 6 hours, 44 minutes, 15 seconds, 574 milliseconds'

题目详情(有点长)

Description:
Your task in order to complete this Kata is to write a function which formats a duration, given as a number of seconds, in a human-friendly way.
描述
完成这个关卡的任务是给一个秒数,写一个人类友好格式化持续时间的函数

The function must accept a non-negative integer. If it is zero, it just returns "now". Otherwise, the duration is expressed as a combination of years, days, hours, minutes and seconds.
该函数必须接受一个非负整数,如果是0返回now.另外,持续时间用years, days, hours, minutes and seconds组成表达

It is much easier to understand with an example:
举个例子会更容易理解

formatDuration(62) // returns "1 minute and 2 seconds"
formatDuration(3662) // returns "1 hour, 1 minute and 2 seconds"
For the purpose of this Kata, a year is 365 days and a day is 24 hours.
对于这个关卡来说,一年是365天,一天是24小时

Note that spaces are important.
请注意,空格很重要

Detailed rules
The resulting expression is made of components like 4 seconds, 1 year, etc. In general, a positive integer and one of the valid units of time, separated by a space. The unit of time is used in plural if the integer is greater than 1.详细规则
结果表达是由4 seconds, 1 year等组件组成的。通常,一个整数和一个有效的单位由空格分割。如果整数大于1时间单位应使用复数

The components are separated by a comma and a space (", "). Except the last component, which is separated by " and ", just like it would be written in English.
组件由一个逗号和一个空格分隔,除了最后一个组件用‘and'分隔。就像英语书写一样

A more significant units of time will occur before than a least significant one. Therefore, 1 second and 1 year is not correct, but 1 year and 1 second is.
要注意时间单位顺序。1 second and 1 year不正确,应该是1 year and 1 second

Different components have different unit of times. So there is not repeated units like in 5 seconds and 1 second.
不同的组件有不同的时间单位,所以不能有重复的时间单位,像5 seconds and 1 second

A component will not appear at all if its value happens to be zero. Hence, 1 minute and 0 seconds is not valid, but it should be just 1 minute.
值为0时组件不出现。因此,1 minute and 0 seconds 是无效的,它应只表示为1 minute

A unit of time must be used "as much as possible". It means that the function should not return 61 seconds, but 1 minute and 1 second instead. Formally, the duration specified by of a component must not be greater than any valid more significant unit of time.
必须“尽可能”使用一个时间单位。 这意味着该函数不应返回61秒,而应返回1分1秒。 正式地,组件指定的持续时间不得大于任何有效的更重要的时间单位

相关文章

网友评论

      本文标题:持续时间格式化(Human readable duration

      本文链接:https://www.haomeiwen.com/subject/wssxwhtx.html