Operating System - HP-UX
1844022 Members
2535 Online
110226 Solutions
New Discussion

getting epoch time through shell

 
SOLVED
Go to solution
Ken Penland_1
Trusted Contributor

getting epoch time through shell

does anyone know of a shell script that will convert a date timestamp into seconds epoch?

I know how to do this easy enough with perl, but what I need this for perl will not work, it has to be a shell script..

basically it would need to convert something like:
Fri Oct 31 15:44:42 EST 2003
into:
1067633082
'
8 REPLIES 8
Rusty Sapper
Frequent Advisor

Re: getting epoch time through shell

Ken Penland_1
Trusted Contributor

Re: getting epoch time through shell

yeah, I dont think that is going to work, but thanks...some of you amaze me with your abilities to quickly find threads round here ;)

Here is a little background on my situation...basically we have this third party software that "web enables" some of our applications...well, and I am not sure of all the details, but basically it has the ability to run home-grown scripts, but it reads in the script and converts it into some format that it understands...which, obviously causes it to bawk when it see's a perl script....I even tried getting around it by just having a shell script call the perl script and it still bawks...I have a feeling it will freak out when it see's a C prog also...

dont ask me, I didnt write this stupid thing :P

'
A. Clay Stephenson
Acclaimed Contributor

Re: getting epoch time through shell

Something is very strange here. If your application can invoke the shell --- which in turn I assume can call commands like date, awk, ... then it should be able to utilize any executable including Perl. I suspect that the proper Perl environment is not setup so that you may need to add extra -I arguments and PATH before Perl is invoked.
Perl is definitely the way to do this.

If you must, I can probably get you very close with caljd.sh -- although the timezone stuff makes this problem more difficult. If the datestamps can be done UT rather than local timezones then this is a much simpler exercise.


If it ain't broke, I can fix that.
curt larson_1
Honored Contributor

Re: getting epoch time through shell

I even tried getting around it by just having a shell script call the perl script and it still bawks...I have a feeling it will freak out when it see's a C prog also...

Does other unix commands work, i.e. grep, sed, awk, etc. after all they are compiled C programs, just like the shell is.

Maybe your application will only execute commands found in a specific location, i.e. has it's own PATH variable (or equiv) setup.
Ken Penland_1
Trusted Contributor

Re: getting epoch time through shell

Yeah, I see where you are getting with this, and I can see how it would be confusing ;)


all I know about this product is this:

the users, from the web, run a script called menu.sh...

in this script it has echo's, read's, if checks and case statements..other than that, no commands except shell programs in the case statements...like if the user picks option 1, it runs this script..

but you are right...in the shell scripts that it calls, they use stuff like grep and awk...which is compiled....so perhaps a C program will work, I will try it on Monday...

as for perl, when I try to run a perl script (which works manually from command line) it actually just hangs forever and starts churning on the CPU until it is manually killed. so I am unclear as to what the problem is with that...

I was kind of just hoping that there was already a script written out there somewhere that did this...Oh, and the timezone doesnt make a difference really...basically I need the epoch time for comparisons, like was Jul 22nd, 15:44 2003 90 days ago? if so, then do this...
'
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: getting epoch time through shell

Okay, in that case I can help you if you can do enough awk or shell scripting on your own to at least pull out "Jul 22 2003" from the data string.

I'll do this in POSIX/ksh but you might have to use expr if this is a really dumb shell.

DT="Jul 22 2003"
DIFF=$(( $(caljd) - $(caljd.sh -i ${DT}) ))
if [[ ${DIFF} -ge 90 ]]
then
echo "This here is 90 days or older"
fi

Invoke as caljd.sh -u for full usage. You can also yank out the cal_jdate function and use it directly. It takes three numerical args: month, day, year and converts them to a JulianDay --- number of days since Jan 1, 4713 BCE. Differences are then easy.
If it ain't broke, I can fix that.
john korterman
Honored Contributor

Re: getting epoch time through shell

Hi,
maybe the attached script can help you. In order to achieve the requested, it has to be fed correctly, e.g.:
# ./return_seconds 1970 2003 10 31 15 44 42

which produces this result:
1067615082

The parameters are explained in the script: no. 1 is the year to count back to; the script counts back to midnight of $1. The other six parameters are the year, month etc, from which you count.


regards,
John K.
it would be nice if you always got a second chance
Ken Penland_1
Trusted Contributor

Re: getting epoch time through shell

Excellent!

yes, both of the above will work :)

One thing to mention though, on the return_seconds script, the way that is written, the timezone does make a differance, because midnight 1970 is not really 0 seconds epoch, it is 6 hours off when running in EST...I think I will play around with the script to hard code in the date of Dec 31 19:00:00 1969 as being the start time :)

Thanks for your assistance :)
'