繁体   English   中英

找到给定24小时时间的最近时间

[英]Find the closest time to a given 24-hour time

鉴于功能签名:

getNearestToDesiredTime(left, middle, right, desiredTime)

哪里:

  • left, middle, right可能有效24小时( 1830, -1, 1900
  • desiredTime是理想的24小时制( 1845

你怎么能找到所需时间的最短时间?

在时间参数与所需时间等距的情况下,应返回较晚的时间。 (在上面的例子中, 1900应返回的,因为19001830均为15从所需分钟1845 )。

这是我到目前为止所做的,但它似乎没有起作用。

const timeDiffInMinutes = (t1, t2) => {
  const t1Mins = t1 / 100 * 60 + t1 % 100;
  const t2Mins = t2 / 100 * 60 + t2 % 100;
  return t2Mins - t1Mins;
};

const getNearestToDesiredTime = (left, middle, right, desiredTime) => {
  if (middle !== -1) {
    return middle;
  } else if (left === -1 && right === -1) {
    return null;
  }

  // result should be whatever time is _closest_ to the desiredTime.
  // if both options are equi-distant to the desired time, show the later time.
  const leftIsCloser =
    timeDiffInMinutes(desiredTime, left) <
    timeDiffInMinutes(desiredTime, right);

  return leftIsCloser ? left : right;
};

测试用例:

// this should be 1900
console.log(getNearestToDesiredTime(1830, 1900, 1945, 1900))

// this should be 1915
console.log(getNearestToDesiredTime(1830, 1915, 1945, 1900))

// this should be 1830
console.log(getNearestToDesiredTime(1830, -1, 1945, 1845))

// this should be 1945
console.log(getNearestToDesiredTime(1830, -1, 1945, 1930))

// this should be 1900
console.log(getNearestToDesiredTime(1830, -1, 1900, 1845))

稍微改变它以处理一组时间更通用。 我们跟踪最佳差异,然后返回制作它的时间

 // this should be 1900 console.log(getNearestToDesiredTime([1830, 1900, 1945], 1900)) // this should be 1915 console.log(getNearestToDesiredTime([1830, 1915, 1945], 1900)) // this should be 1830 console.log(getNearestToDesiredTime([1830, 1945], 1845)) // this should be 1945 console.log(getNearestToDesiredTime([1830, 1945], 1930)) // this should be 1900 console.log(getNearestToDesiredTime([1830, 1900], 1845)) function timeDiffInMinutes (t1, t2) { const t1Mins = Math.floor(t1 / 100) * 60 + t1 % 100; const t2Mins = Math.floor(t2 / 100) * 60 + t2 % 100; return t2Mins - t1Mins; }; function getNearestToDesiredTime (times, target) { var best = Infinity; var bestIndex = -1; for (var i = 0; i < times.length; i++) { var difference = Math.abs(timeDiffInMinutes(times[i], target)); if (difference <= best) { best = difference; bestIndex = i; } } return times[bestIndex]; } 

该解决方案使用不同的方法。 它从午夜所有三个时间戳计算分钟,然后使用之间的差额leftright的关系时间戳到desired时间戳,以确定哪个是最接近的。

我用很多时间戳测试了它。 还有一些奇怪的。 到目前为止似乎工作正常。

希望这可以帮助。

 const minutesFromMidnight = (t) => { const frac = t / 100; const hours = Math.trunc(frac); const mins = (frac - hours) * 100; const minsOfDay = (hours * 60) + mins; var minutesDist = 0; if (minsOfDay >= 720) minutesDist = minsOfDay - 1440; else minutesDist = minsOfDay; return minutesDist; }; const getNearestToDesiredTime = (left, middle, right, desiredTime) => { if (middle !== -1) { return middle; } else if (left === -1 && right === -1) { return null; } const desiredDist = minutesFromMidnight(desiredTime); const leftDist = minutesFromMidnight(left); const rightDist = minutesFromMidnight(right); const leftDiff = Math.abs(leftDist - desiredDist); const rightDiff = Math.abs(rightDist - desiredDist); const leftIsCloser = leftDiff < rightDiff; return leftIsCloser ? left : right; }; // this should be 1830 console.log(getNearestToDesiredTime(1830, -1, 1945, 1845) + ' = 1830'); // this should be 1945 console.log(getNearestToDesiredTime(1830, -1, 1945, 1930) + ' = 1945'); // this should be 1900 console.log(getNearestToDesiredTime(1830, -1, 1900, 1845) + ' = 1900'); // this should be 1800 console.log(getNearestToDesiredTime(1800, -1, 1900, 1745) + ' = 1800'); // this should be 1900 console.log(getNearestToDesiredTime(130, -1, 1900, 1600) + ' = 1900'); // this should be 130 console.log(getNearestToDesiredTime(1900, -1, 130, 2300) + ' = 130'); // this should be 2315 console.log(getNearestToDesiredTime(130, -1, 2315, 0) + ' = 2315'); // this should be 130 console.log(getNearestToDesiredTime(130, -1, 2315, 100) + ' = 130'); 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM