In UNIX how to convert to epoch milliseconds date strings like:
19-FEB-12
16-FEB-12
05-AUG-09
I need this to compare these dates with the current time on the server.
To convert a date to seconds since the epoch:
date --date="19-FEB-12" +%s
Current epoch:
date +%s
So, since your dates are in the past:
NOW=`date +%s`
THEN=`date --date="19-FEB-12" +%s`
let DIFF=$NOW-$THEN
echo "The difference is: $DIFF"
Using BSD's date
command, you would need
$ date -j -f "%d-%B-%y" 19-FEB-12 +%s
Differences from GNU date
:
-j
prevents date
from trying to set the clock-f
-d
)Here's one way using GNU awk
. To run:
awk -f script.awk file
Contents of script.awk
:
BEGIN {
n = systime()
FS="-"
}
{
t = mktime(sprintf("%d %d %d %d %d %d", "20" $3, convert($2), $1, 0, 0, 0))
printf "%.1f days ago\n", (n - t) / 60 / 60 / 24
}
function convert(m) {
return(((index("JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC", m) - 1) / 3) + 1)
}
Results:
358.6 days ago
361.6 days ago
1286.6 days ago
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With