如何使用 jq 从两个日期/时间值计算持续时间

ale*_*lec 3 datetime json duration jq

我有一些看起来像这样的 json...

[
  {
    "start": "20200629T202456Z",
    "end": "20200629T211459Z",
    "tags": [
      "WPP",
      "WPP review tasks, splashify popup",
      "clients",
      "work"
    ],
    "annotation": "update rules, fix drush errors, create base wpp-splash module."
  },
  {
    "start": "20200629T223000Z",
    "end": "20200629T224641Z",
    "tags": [
      "WPP",
      "WPP review tasks, splashify popup",
      "clients",
      "work"
    ]
  },
 ]
Run Code Online (Sandbox Code Playgroud)

我想显示持续时间:小时:分钟而不是“开始”和“结束”时间。

时间格式可能有点不寻常(?),它来自 timewarrior。jq我想如果日期/时间存储在正常的 unix 时间戳中,这会更容易完成,但也许这仍然是可能的?jq 可以像这样写输出吗

[
  {
    "time": "0:50:03",
    "tags": [
      "WPP",
      "WPP review tasks, splashify popup",
      "clients",
      "work"
    ],
    "annotation": "update rules, fix drush errors, create base wpp-splash module."
  }
]
Run Code Online (Sandbox Code Playgroud)

或类似的东西。

那可能吗?

pea*_*eak 5

为了清楚起见,让我们定义一个辅助函数:

def duration($finish; $start):
  def twodigits: "00" + tostring | .[-2:];
  [$finish, $start]
  | map(strptime("%Y%m%dT%H%M%SZ") | mktime) # seconds
  | .[0] - .[1]
  | (. % 60 | twodigits) as $s
  | (((. / 60) % 60) | twodigits)  as $m
  | (./3600 | floor) as $h
  | "\($h):\($m):\($s)" ;
Run Code Online (Sandbox Code Playgroud)

现在的解决方案很简单:

map( {time: duration(.end;.start)} + del(.start,.end) )
Run Code Online (Sandbox Code Playgroud)