-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfdate.ksh
More file actions
executable file
·133 lines (120 loc) · 3.13 KB
/
fdate.ksh
File metadata and controls
executable file
·133 lines (120 loc) · 3.13 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
124
125
126
127
128
129
130
131
132
#!/bin/ksh
##########################################################################
# Title : fdate - future date
# Author : Heiner Steven <heiner.steven@odn.de>
# Date : 1998-03-17
# Requires : gawk
# Category : System Utilities
# SCCS-Id. : @(#) fdate 1.3 %$%
##########################################################################
# Description
# o Uses non-standard functions "strftime()" and "systime()"
# of non-standard program "gawk" to print future dates
# Changes
# 27.05.98 stv support dates in the past (0.3)
##########################################################################
PN=`basename "$0"` # Program name
VER='1.3'
# Default date format
DateFormat="%a, %d %b %Y %H:%M:%S +0100"
usage () {
echo >&2 "$PN - print future date, $VER (stv '98)
usage: $PN [+dateformat] [timeperiod ...]
dateformat: see the manual page for strftime(3).
timeperiod: seconds to add to/subtract from the current time.
The time period may be specified in one of two ways:
o in seconds, i.e. "3600" for one hour
o in multiples of minutes, hours, weeks, months, or years i.e.
2 weeks 1 day"
exit 1
}
msg () {
for msgLine
do echo "$PN: $msgLine" >&2
done
}
fatal () { msg "$@"; exit 1; }
# We need GAWKs non-standard extensions systime() and strftime(),
# and we are prepared to search for gawk:
if [ -z "$GAWK" ]
then
for path in `echo "$PATH" | sed 's|^:|./ |;s|:$| ./|;s|:| |g'`
do [ -x "$path/gawk" ] && break
done
GAWK=$path/gawk
fi
[ -x "$GAWK" ] || fatal "Sorry, cannot find gawk in your PATH"
#set -- `getopt h "$@"`
while [ $# -gt 0 ]
do
case "$1" in
--) shift; break;;
-h) usage;;
-*) break;; # could be "-day"
*) break;; # First file name
esac
shift
done
case "$1" in
+*) # A "+" starts the date format
DateFormat=`echo "$1" | sed 's/^+//'`
shift;;
esac
echo "$@" | tr '[A-Z]' '[a-z]' |
$GAWK '
function secondsof(word) {
sign = +1
if ( word ~ /^-/ ) {
sign = -1
sub (/^-/, "", word)
}
sub (/^+/, "", word) # "+week"
sub (/s$/, "", word) # "weeks" -> "week"
for ( shortname in Secs ) {
if ( shortname == word ) {
return sign * Secs [word]
}
}
return 0
}
BEGIN {
DateFormat = "'"$DateFormat"'"
# Calculate the seconds for common time intervals
Secs ["minute"] = Secs ["min"] = 60
Secs ["hour"] = 60*60
Secs ["day"] = 60*60*24
Secs ["week"] = 60*60*24*7
Secs ["month"] = int (60*60*24*7*4.25)
Secs ["year"] = Secs ["day"] * 365
op = ""
seconds = 0
}
{
for ( i=1; i<=NF; i++ ) {
number = 0
if ( $i ~ /^[-+]$/ ) {
op = $i ""
continue
} else if ( $i ~ /^[-+]*[0-9][0-9]*$/ ) {
number = $i
if ( i < NF && secondsof($(i+1)) ) {
number = number * secondsof($(i+1))
i++
}
} else if ( secondsof($i) ) {
number = secondsof($i)
} else {
print "WARNING: ignoring " $i | "cat >&2"
}
if ( op == "-" ) {
seconds -= number
} else {
seconds += number
}
}
}
END {
now = systime ()
print strftime (DateFormat, now + seconds)
}
'