-
Notifications
You must be signed in to change notification settings - Fork 0
/
dhms
executable file
·84 lines (78 loc) · 3.54 KB
/
dhms
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#! /usr/bin/awk -f
# dhms - convert time durations between seconds and days/hours/minutes/seconds.
# Copyright (C) 2022-2023 Erik Auerswald <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# This is a simple Awk based Unix filter.
# Input is read from files given as command line arguments, or standard input.
# Only non-negative time durations can be converted.
# Each input line is expected to comprise exactly one time duration value.
# A time duration value must either be a unitless number of seconds, or
# a value in DHMS format (zero valued units can be omitted).
# Whitespace is ignored.
# Letters are converted to lower case.
# 47d11h8m15s is an example DHMS value, as is 47min 11s.
# Each input line with a time duration value is replaced with the same
# time duration value in the other format.
BEGIN {
progname = "dhms" # used as prefix for error messages
unit_factors["w"] = 7 * 24 * 60 * 60 # seconds per week
unit_factors["d"] = 24 * 60 * 60 # seconds per day
unit_factors["h"] = 60 * 60 # seconds per hour
unit_factors["m"] = 60 # seconds per minute
unit_factors["s"] = 1 # seconds per second
}
function dhms_to_secs(dhms) {
gsub(/[:[:space:]]+/, "", dhms) # remove separators
val_cnt = split(dhms, vals, "[a-z,]+") # last field is empty
unit_cnt = split(dhms, units, "[0-9.,]+") # first field is empty
if (val_cnt != unit_cnt) {
print progname": Error:" \
" number of values differs from number of units:" \
" val_cnt=" val_cnt " unit_cnt=" unit_cnt > "/dev/stderr"
return -1
}
secs = 0 # reset sum for each dhms ---> seconds conversion
for (i = 1; i < val_cnt; i++) {
factor = unit_factors[substr(units[i+1], 1, 1)]
if (factor) {
secs += vals[i] * factor
} else {
print progname": Error: unknown unit "units[i] \
> "/dev/stderr"
return -1
}
}
return secs
}
function secs_to_dhms(secs) {
days = int(secs / 86400)
secs %= 86400
hours = int(secs / 3600)
secs %= 3600
minutes = int(secs / 60)
secs %= 60
return days"d"hours"h"minutes"m"secs"s"
}
{
$0 = tolower($0) # convert to lower case before checking patterns
}
/^[[:space:]]*([0-9]+[.,]?[0-9]*|[0-9]*[.,]?[0-9]+)[[:space:]]*$/ {
print secs_to_dhms($0)
next # no need to check further patterns
}
/^[[:space:]]*(([0-9]+[.,]?[0-9]*|[0-9]*[.,]?[0-9]+)[[:space:]]*(w(eek)?|d(ay)?|h((ou)?r)?|m(in(ute)?)?|s(ec(ond)?)?)s?[,:[:space:]]*)+[[:space:]]*$/ {
secs = dhms_to_secs($0)
if (secs >= 0) print secs # on error, secs == -1
}