-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathtimes.awk
More file actions
123 lines (101 loc) · 3.02 KB
/
times.awk
File metadata and controls
123 lines (101 loc) · 3.02 KB
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
#!/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";
}
}
# You can do whatever you want with this stuff, but a thanks is always
# appreciated