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

how to calculate time duration from two date/time values using 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"
    ]
  },
 ]

我想显示 hours:minutes 的持续时间,而不是“开始”和“结束”时间。

时间格式可能有点不寻常(?),它来自timewarrior。我想如果 date/time 存储在正常的 unix 时间戳中,jq 会更容易完成,但也许这仍然是可能的? 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."
  }
]

或类似的东西。

这可能吗?

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

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)" ;

现在的解决方案很简单:

map( {time: duration(.end;.start)} + del(.start,.end) )