LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Calculate time difference without 'date -d` in Solaris (https://www.linuxquestions.org/questions/linux-newbie-8/calculate-time-difference-without-date-d%60-in-solaris-4175610778/)

boby.kumar 07-28-2017 02:28 AM

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:

#!/usr/bin/bash
#
ABC () {
#now=`date +"%Y-%m-%d %H:%M:%S"`
now=`date +"%Y%m%d%H%M%S"`
time=10
sec=60
file=`ls -lshartE $1 | awk '{print $7 $8}' | cut -c1-18 | tr -d '-' | tr -d ':'`
#file=`ls -lshartE $1 | awk '{print $7" "$8}' |cut -c1-19`


DIFF=$((now - file))
HR=`expr $DIFF / $sec`


echo $now
echo $file
echo $DIFF
echo $HR
}
ABC $1

#OutPut
20170728082308
20170728070945
11363
189

OutPut is not correct

Can anyone tell me what is missing in the above code

aragorn2101 07-28-2017 05:53 AM

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"
1501136743

> date --date="2017-07-28 16:21:42" +"%s"
1501244502

So, in the above, if you subtract these two numbers you'll get the number of seconds having elapsed between 2017-07-27 10:25:43 and 2017-07-28 16:21:42. Then you go ahead calculate further.

boby.kumar 07-28-2017 08:12 AM

this command is not working..

Quote:

date: illegal option -- date=2017-07-27 10:25:43
usage: date [-u] mmddHHMM[[cc]yy][.SS]
date [-u] [+format]
date -a [-]sss[.fff]

JeremyBoden 07-28-2017 02:04 PM

You are using date as a variable, but it is also a command.
This is confusing!
Code:

DATE="2017-07-27 10:25:43"
should work.
You can check with
Code:

echo $DATE
You need the quotes because of the special characters, especially embedded spaces.

suicidaleggroll 07-28-2017 03:03 PM

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?

scasey 07-28-2017 03:04 PM

Quote:

Originally Posted by boby.kumar (Post 5741012)
this command is not working..
Code:

date: illegal option -- date=2017-07-27 10:25:43
usage: date [-u] mmddHHMM[[cc]yy][.SS]
date [-u] [+format]
date -a [-]sss[.fff]


Your answer is in the error message. The bolded text is a complete summary of Solaris' date command.
Don't use -d or --date, just
Code:

date +"%Y-%m-%d %H:%M:%S"
(I can't test that, tho. -- not running Solaris)

scasey 07-30-2017 05:17 PM

Quote:

Originally Posted by boby.kumar (Post 5740912)
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.


Can anyone tell me what is missing in the above code

If you can parse the log file for the last date, you can do something like this:
Code:

#!/bin/bash

# check smtpd log for timestamp
# email if the last entry is old

# save off the previous time file
mv /root/bin/lastmsg.txt  /root/bin/prevlastmsg.txt

# get the current last message time from the logs
tail -1 /var/log/qmail/smtpd/current | /usr/local/bin/tai64nlocal | cut -b1-19 > /root/bin/lastmsg.txt

# Run the test
TIMETEST=`diff /root/bin/lastmsg.txt /root/bin/prevlastmsg.txt`

# get the values
LASTMSG=`cat /root/bin/prevlastmsg.txt`
NOW=`cat /root/bin/lastmsg.txt`

## if the diff results in an empty file, then there is a problem.  Send an email
if [[ -z $TIMETEST ]]
        then
        mailx -s "Mail problem? Last message: $LASTMSG Now: $NOW" support@mydomain.com < /root/bin/checkmail.msg
fi

This script will send an email if there have been no log entries from the smptd server in the last 10 minutes -- it's run by cron every 10 minutes.
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
A check of the smtpd/current log shows no message received since the last check.
Since that was ten minutes ago, perhaps there is a qmail problem???


aragorn2101 08-02-2017 03:25 AM

Quote:

Originally Posted by scasey (Post 5741173)
Don't use -d or --date, just
Code:

date +"%Y-%m-%d %H:%M:%S"
(I can't test that, tho. -- not running Solaris)

OP already has the date in this format and he's trying to calculate time differences. So a direct numeric format with "%s" would be better.

Quote:

Originally Posted by boby.kumar (Post 5741012)
Code:

date: illegal option -- date=2017-07-27 10:25:43
usage: date [-u] mmddHHMM[[cc]yy][.SS]
date [-u] [+format]
date -a [-]sss[.fff]


The date fed to option --date is supposed to be a single string, i.e. it should have been quoted: " ".

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.

blastwave 08-02-2017 04:45 AM

Quote:

Originally Posted by suicidaleggroll (Post 5741172)
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 ...

Actually the issue is the opposite. He is running Solaris which is POSIX.1-2001 strict compliant. The "date" function in Linux is not standard. So the version in Solaris is correct whereas the version in Linux is all of that plus a pile of non-standard extensions. This is a common misconception however and you would need to check the specifications to see this :

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

blastwave 08-02-2017 05:01 AM

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.

scasey 08-02-2017 05:00 PM

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.

blastwave 08-03-2017 12:38 AM

Quote:

Originally Posted by scasey (Post 5743502)
The last time (a couple of years ago) I worked on/with Solaris, there was a directory ...

It is a flaw and a mistake to depend on anything that is not in the strict specifications.
Thus the "GNU extensions" are an error.


All times are GMT -5. The time now is 06:10 PM.