user3508234
user3508234

Reputation: 25

Convert Current Date to Unix timestamp but using Days

In Solaris, I am trying to write a shell script that converts current date to the number of days after 1/1/1970 for Unix. This is because etc/shadow isn't using Epoch time but instead a 'days format'

i.e "root:G9yPfhFAqvlsI:15841::::::" where the 15841 is a date.

So in essence what command do I use to find out the epoch time for now and then convert that to days.

Upvotes: 0

Views: 2588

Answers (3)

alvits
alvits

Reputation: 6758

Solaris date utility doesn't support %s format. However, nawk utility has srand() function that returns date in seconds when there's no parameter passed to it.

nawk 'BEGIN {print srand()}'

Results in

1405529923

To get the days instead of seconds, you can divide the result by 86400.

nawk 'BEGIN {printf("%d", srand() / 86400)}'

Upvotes: 0

ahoffer
ahoffer

Reputation: 6526

I found some pseudo code to calculate it from the basics:

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

Credit: http://www.unix.com/shell-programming-and-scripting/115449-how-convert-date-time-epoch-time-solaris.html). On the same forum thread, another poster said this would work in Solaris:

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

Upvotes: 0

glenn jackman
glenn jackman

Reputation: 246847

You probably don't have GNU tools, which might make things easier. This is simple enough though:

perl -le 'print int(time/86400)'

Upvotes: 1

Related Questions