Во-первых, «подключаемая» реализация jq's mktime/0
:
# [1970, 0, 1, 0, 0, 0] | mktime #=> 0
# For Jan, use 0 for $m.
# Emit null if any inputs are judged to be invalid.
# No attempt to account for leap seconds is made.
# Can also be used for earlier years.
#
# Adapted from
# https://codereview.stackexchange.com/questions/11614/determine-total-number-of-seconds-since-the-epoch
def mktime:
def max: if .[0] > .[1] then .[0] else .[1] end;
def min: if .[0] > .[1] then .[1] else .[0] end;
def NDaysInMonth: [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
. as [$y, $m, $d, $h, $mn, $s]
| if ($y < 0 or $m < 0 or $d < 1 or $h < 0 or $mn < 0 or $s < 0)
then null
else
1970 as $ye # UNIX epoch 01/01/1970
| (if $y > $ye then 1 else -1 end) as $sign
| ([$y, $ye] | max) as $maxY
| ([$y, $ye] | min) as $minY
| reduce range($minY; $maxY) as $i ({nleapYears: 0};
if (($i % 100 != 0 and $i % 4 == 0) or $i % 400 == 0) then .nleapYears += 1 else . end)
| if ($m > 1) and (($y % 100 != 0 and $y % 4 == 0) or $y % 400 == 0)
then .nleapYears += $sign
else .
end
| .nleapYears |= ([0, .] | max)
| reduce range(0;$m) as $i (.monthsNDays = 0;
.monthsNDays += NDaysInMonth[$i])
| (($y - $ye) * 365 + $sign * .nleapYears + .monthsNDays + $d - 1) * 86400
+ $h * 3600 + $mn * 60 + $s
end ;
Далее, для разбора строк даты в форме "2018-06-01 12:45:56"
, вы можете легко использовать jq's capture
(см.руководство jq ).Так как SO не является бесплатным сервисом программирования, это оставлено как упражнение, так как собирает все кусочки вместе.