-4

[SunOS] How to find the difference in date time?

Date 1 : 2017-01-02 17:24:33 Date 2 : 2017-01-03 22:55:02

Answer : 1770 minutes

  • Which version of Sun OS do you have in mind? Solaris 11, Solaris 10, some older version of Solaris, or do you really mean Sun OS 4.x? (The Solaris versions x are equivalent to version 5.x, IIRC.) – Jonathan Leffler Jan 10 '17 at 19:41
  • Please read http://stackoverflow.com/help/how-to-ask , http://stackoverflow.com/help/dont-ask , http://stackoverflow.com/help/mcve and take the [tour](http://stackoverflow.com/tour) before posting more Qs here. Good luck. – shellter Jan 10 '17 at 20:11

1 Answers1

1

Neither Sun OS nor Solaris is Linux; the native date command doesn't have the facilities of GNU date, AFAIK. It may be obvious how you do it on Linux, but it isn't so obvious how you do it with POSIX date which is approximately what Sun OS or Solaris supports.

You should find How do I get the date from two weeks ago using Solaris date? on Unix & Linux useful. It suggests that if you're using a recent enough version of Solaris (implying Solaris 11, I think, and excluding earlier versions), then you can use gdate to access the facilities of GNU date. You can then convert the two given date/time values to 'seconds since the epoch', subtract them, and divide by 60 to get the number of minutes in the interval between the times.

For example:

date1="2017-01-02 17:24:33"
date2="2017-01-03 22:55:02"
secs1=$(gdate -d "$date1" +'%s')
secs2=$(gdate -d "$date2" +'%s')
echo $(( ($secs2 - $secs1) / 60 ))

The output from that is 1770.

The $(( … )) is POSIX shell arithmetic, also supported by Korn shell and Bash. I don't know whether the Solaris 11 /bin/sh supports it or not. Historically, that shell was a Bourne shell without support for $( … ) let alone $(( … )).

If you don't have gdate already, consult the cross-referenced question for more information about optional GNU software that can be installed on Solaris from the official Solaris repositories (see Solaris default install user tools).

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278