2017-04-25 165 views
2

我正在使用jira rest api来解决问题。估计的时间会以秒为单位返回 - 但是基于每周5天,每天8个小时。将工作秒数转换为周,天,小时和分钟

如此,例如,1d的估计时间被返回28800秒

我有这个代码,我想要做什么,但它的气味,我想知道如果有一个更好的功能做我想

let es = 246180; // time (number of *work* seconds) is "1w 3d 4h 23m" 
let sInMin = 60; 
let sInHour = (60 * 60); 
let sInDay = (sInHour * 8); 
let sInWeek = (sInDay * 5); 

let w = Math.trunc(es/sInWeek); 
let d = Math.trunc((es - (w * sInWeek))/sInDay); 
let h = Math.trunc((es - (w * sInWeek) - (d * sInDay))/sInHour); 
let m = Math.trunc((es - (w * sInWeek) - (d * sInDay) - (h * sInHour))/sInMin); 

console.log(w,d,h,m) // 1 3 4 23 

如果我添加momentmoment-duration-format进来,然后我就可以做

moment.duration({w,d,h,m}).format("w[w] d[d] h[h] m[m]") 

去神奇1w 3d 4h 23m字符串

我该如何改进呢?

回答

1

JIRA REST API已经为您提供了正在尝试计算的字段。

从样本响应他们的文档here

"timetracking": { 
    "originalEstimate": "10m", 
    "remainingEstimate": "3m", 
    "timeSpent": "6m", 
    "originalEstimateSeconds": 600, 
    "remainingEstimateSeconds": 200, 
    "timeSpentSeconds": 400 
    } 

你抓住了originalEstimateSeconds领域,并试图做数学得到的字符串,但字符串在originalEstimate领域脱颖而出,你已经取得。不需要计算它。

+0

嗯,这有点糟糕,因为我必须使用搜索api来获取问题(由externalId搜索),并且* api似乎不会返回时间跟踪,只是实际的秒数值。 jira api是如此*血腥*不一致,它让我生气;( 感谢您的单挑 – jmls

+0

你打了哪个特定的api,你在哪里得到显示你在几秒钟内的估计的领域?在请求的'fields'参数中请求?如果是这样,也许有一个不同的字段将值显示为字符串?只是基于文档进行推测。 –

相关问题