how to convert date time to epoch time in solaris

Hi,

Is there any easy way to convert date time(stored in shell variable ) to epoch time in solaris box? As +%s is working on linux but not on solaris, also -d option is not working.

Any suggestion please?

Hi.

There's lots of examples of this kind of thing on the forum if you search.

This may be useful Epoch Converter - Unix Timestamp Converter

%s shows seconds since the epoch. It doesn't take a variable and format it to seconds since the epoch.

There (as I know) is no way to do this in normal ksh, etc, but ksh93 has some features. But ksh93 (at least on my Solaris) doesn't exist.

I have a shell script that will do it, but this might be easier if you can use it:

perl -e 'print time."\n"'

see also - This thread.

---------- Post updated at 11:57 AM ---------- Previous update was at 01:41 AM ----------

for solaris (without perl , %s, and -d)

I found this:

truss /usr/bin/date 2>&1 | grep ^time | awk -F"= " '{print $2}'

Note: use nawk if awk doesn't work.

I have some start time like : 2009-07-08 06:52:05
and end time : 2009-07-08 06:52:44

now here I want to subtract start time from end time and get the result in seconds

which I was doing in linux like :

epoch_time_start=`date +%s -d"$1"`
epoch_time_end=`date +%s -d"$2"`
total_time=`expr $epoch_time_end - $epoch_time_start`

but the same is not working in solaris....I can not use perl because it is not available in the system where I have to deploy this script.....

To convert a date to seconds for easier math, see pseudo code:

Get date values of the date to be converted for second, minute, hour, day, month, year.

if month > 2 then
  month=month+1
else
  month=month+13
  year=year-1
fi

day=(year*365)+(year/4)-(year/100)+(year/400)+(month*306001/10000)+day
days_since_epoch=day-719591 (which is Jan 1 1970)
seconds_since_epoch=(days_since_epoch*86400)+(hour*3600)+(minute*60)+seconds

See more detail in Chapter 3 of Expert Shell Scripting - Apress.

The +%s format for date and date functions like strftime() is not defined by POSIX standards, oddly enough. So a lot of systems - Solaris being one - require the use of perl or ruby or python to do that. Or a complex shell function.

I think this is on the list of changes for the new C standards.

Date/time has always been a problem child because of locales, different calendars and so on.