0

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.

4
  • What is 15841? The number of seconds after the Epoch began? If so, you should be able to divide it by 60*60*24 to get the number of days. Commented Jul 15, 2014 at 18:53
  • 15841 is the number of days after 1/1/1970 so I'm trying to find out what the command is to print today's date into the format for days @ahoffer Commented Jul 15, 2014 at 19:00
  • 1
    You can use echo $(($(nawk 'BEGIN {print srand()}') / 86400 )). Commented Jul 15, 2014 at 19:27
  • @alvits, that's good, you should add that as an answer Commented Jul 16, 2014 at 14:33

3 Answers 3

1

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

perl -le 'print int(time/86400)' 
Sign up to request clarification or add additional context in comments.

2 Comments

If you're using this to obtain a uid, better not create 2 users on the same day.
Thank you @glennjackman is there anyway to do this without perl?
0

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}' 

4 Comments

i put 'truss /usr/bin/date 2>&1 | grep ^time | awk -F"= " '{print $2}'' into a variable now do you know how I would divide that variable using the "variable" /8600 ?
@user3508234 - it will be easier to use nawk's srand(). If you are running bash you can assign to var like (( var = $(nawk 'BEGIN {print srand()}') / 86400 )). If you still prefer the above method, you can use (( var /= 86400 )) on bash or var=`echo $var / 86400 | bc` on non-bash.
@user3508234: truss /usr/bin/date 2>&1 | nawk '$1 == "time()" {print int($NF/86400)}'
@alvits, might as well do the division in awk: nawk 'BEGIN {print int(srand()/86400)}'
0

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)}' 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.