forked from e36freak/awk-libs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimes.awk
140 lines (118 loc) · 4.01 KB
/
times.awk
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/usr/bin/awk -f
## usage: month_to_num(month)
## converts human readable month to the decimal representation
## returns the number, -1 if the month doesn't exist
function month_to_num(mon, months, m) {
# populate months[] array
months["january"] = 1; months["february"] = 2; months["march"] = 3;
months["april"] = 4; months["may"] = 5; months["june"] = 6;
months["july"] = 7; months["august"] = 8; months["september"] = 9;
months["october"] = 10; months["november"] = 11; months["december"] = 12;
# also populate abbreviations
for (m in months) {
months[substr(m, 1, 3)] = months[m];
}
# convert month to lowercase
mon = tolower(mon);
# check if month exists
if (mon in months) {
return months[mon];
} else {
return -1;
}
}
## usage: day_to_num(day)
## converts human readable day to the decimal representation
## returns the number, -1 if the day doesn't exist
## like date +%w, sunday is 0
function day_to_num(day, days, d) {
# populate days[] array
days["sunday"] = 0; days["monday"] = 1; days["tuesday"] = 2;
days["wednesday"] = 3; days["thursday"] = 4; days["friday"] = 5;
days["saturday"] = 6;
# also populate abbreviations
days["sun"] = 0; days["mon"] = 1; days["tues"] = 2; days["wed"] = 3;
days["thurs"] = 4; days["fri"] = 5; days["sat"] = 6;
# convert day to lowercase
day = tolower(day);
# check if day exists
if (day in days) {
return days[day];
} else {
return -1;
}
}
## usage: hr_to_sec(timestamp)
## converts HH:MM:SS or MM:SS to seconds
## returns -1 if invalid format
function hr_to_sec(time, t, l, i, j) {
# check for valid format
if (time !~ /^[0-9]+(:[0-9][0-9])?:[0-9][0-9]$/) {
return -1;
}
# convert
l = split(time, t, /:/);
j = time = 0;
for (i=l; i>0; i--) {
time += t[i] * (60 ^ j++);
}
return time;
}
## usage: sec_to_hr(seconds)
## converts seconds to HH:MM:SS
function sec_to_hr(sec, m, s) {
s = sec % 60;
sec = int(sec / 60);
m = sec % 60;
sec = int(sec / 60);
return sprintf("%02d:%02d:%02d", sec, m, s);
}
## usage: ms_to_hr(milliseconds)
## converts milliseconds to a "time(1)"-similar human readable format, such
## as 1m4.356s
function ms_to_hr(ms, m, s, ns) {
ms = ms / 1000;
s = int(ms);
m = int(s / 60);
ns = s % 60;
return sprintf("%dm%0.3fs", m, ns + (ms - s));
}
## usage: add_day_suff(day_of_month)
## prepends the appropriate suffix to "day_of_month". for example,
## add_day_suff(1) will return "1st", and add_day_suff(22) will return "22nd"
## returns -1 if "day_of_month" is not a positive integer
function add_day_suff(day) {
# make sure day is a positive int
if (day !~ /^[0-9]+$/ || day <= 0) {
return -1;
}
# append prefix
if ((day > 3 && day < 21) || day ~ /[04-9]$/) {
return day "th";
} else if (day ~ /1$/) {
return day "st";
} else if (day ~ /2$/) {
return day "nd";
} else {
return day "rd";
}
}
# Copyright Daniel Mills <dm@e36freak.com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to
# deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
# sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.