Calculate time difference without 'date -d` in Solaris
Hi,
I am writing a script to calculate the time difference of a log file from the current time and want to check "No traffic since x min". I am working on Solaris. localhost@server: uname -a SunOS server 5.10 Generic_147147-26 sun4v sparc SUNW,Netra-T2000 date -d "2017-07-27 08:17:40" '+%s' date: illegal option -- d usage: date [-u] mmddHHMM[[cc]yy][.SS] date [-u] [+format] date -a [-]sss[.fff] Quote:
Can anyone tell me what is missing in the above code |
I suggest you use the %s switch. It gives you the time in seconds since a fixed reference in the past.
e.g. Code:
> date --date="2017-07-27 10:25:43" +"%s" |
this command is not working..
Quote:
|
You are using date as a variable, but it is also a command.
This is confusing! Code:
DATE="2017-07-27 10:25:43" You can check with Code:
echo $DATE |
edit: missed part of the original question.
The fundamental problem is that your system doesn't use the standard "date" program, it uses some other stripped down version that doesn't support the -d flag. Can you download the source code for date and compile it on your machine? |
Quote:
Don't use -d or --date, just Code:
date +"%Y-%m-%d %H:%M:%S" |
Quote:
Code:
#!/bin/bash Note that it doesn't check for the time "now" -- it just gets the date of the last log entry and compares that to the last date it saw, so it doesn't use the date command at all. You'd need to change the log file name to the log you want to check, and modify the cut command to snip out the date on the last line. The body of the email looks like this: Code:
more /root/bin/checkmail.msg |
Quote:
Quote:
Anyway boby.kumar, returning to your first post, modify your script to use the date +%s command directly. We don't know what your command line arguments are for the script, but if you use date +%s everywhere you can do easy math with all the time values you get. |
Quote:
see http://pubs.opengroup.org/onlinepubs...ties/date.html The Open Group Base Specifications Issue 7 IEEE Std 1003.1-2008, 2016 Edition Copyright © 2001-2016 The IEEE and The Open Group date - write the date and time |
Not sure of this helps but it may be easiest to diff the unix timestamp between the log and timenow. You can get unixtime from awk like so :
$ /usr/xpg4/bin/awk 'BEGIN{srand(); print srand()}' 1501667971 This is because POSIX awk will see srand() with the current timestamp. You can get a version and highly precise time of a file from ls with : $ ls -lapbE /var/log/syslog -rw-r--r-- 1 root sys 0 2017-07-10 03:10:02.612417954 +0000 /var/log/syslog Now the hard part is to convert that time to unixtime and then just diff the two numbers. I have not figured out the last part yet. |
The last time (a couple of years ago) I worked on/with Solaris, there was a directory in the path that had gnu versions of utilities, including date, where it was called gdate. I don't know if that was standard with Solaris installs or specific to the shop; that is, added by the admins.
Was that /usr/xpg4/bin ? That looks familiar. Am I correct in remembering that the "%s" is not an option for the Solaris date command? I see that -E is not valid for gnu. Is that the option that give the detailed date? I wonder if the OP will ever come back...there are lot's of possible solutions here. |
Quote:
Thus the "GNU extensions" are an error. |
All times are GMT -5. The time now is 06:10 PM. |