Solved

Generic shell function to display dates from existing system date (given current date)  (for hpux, solaris and linux)

Posted on 2014-01-19
5
398 Views
Last Modified: 2014-01-19
I need a ascript that can help to find the last day of the previous month by given a current date in all above server OS.

For example, if cuurent date = '20/Mar/2013'
result of  "last_date = X_function" will be 28/Feb or 29/Feb depending on the year

For example, if cuurent date = '22/Mar/2013'
result of  "last_date = X_function" will be 28/Feb or 29/Feb depending on the year

if current date = '23/Jan/2014'
result of  "last_date = X_function" will be 31/Dec/2014

if current date = '03/Jan/2014'
result of  "last_date = X_function" will be 31/Dec/2014

perl calling is allowed, but has to be generic so that the perl calling can work in all above server OS.
0
Comment
Question by:tindavid
5 Comments
 
LVL 10

Expert Comment

by:lanboyo
ID: 39793263
Linux is easier than most because date is a lot better.

The key is always the unix époq

NOW=$( date +%s )
DOM=$( date +%d )
   # to figure out what the unix époq was the last day of last month, we would subtract the day of the month * number of seconds in a day from the current époq
LESS=$((  $DOM * 60 * 60 *24 ))
ELDOM=$(( $NOW - $LESS ))
OUTPUT= $( echo $ELDOM  | date -p +%d/%b/%Y )
echo $OUTPUT


Solaris is kind of icky, to get the current époq you have to do backflips. I will copy what I did for a different script if no one hits it first.
0
 
LVL 10

Expert Comment

by:lanboyo
ID: 39793273
Seriously I have greped out the output of the date command thru the adb debugger to get the époq. Honestly, what a pain, the system is clearly using it internally, but the date command won't expose it for me in Solaris.
0
 
LVL 26

Expert Comment

by:wilcoxon
ID: 39793306
What do you mean by the below?
perl calling is allowed, but has to be generic so that the perl calling can work in all above server OS.
Is it allowable to use perl modules not in the core?  What version of perl do you have installed on the machines?
0
 

Author Comment

by:tindavid
ID: 39793352
We have Oracle software (9i - 11g)  installed in most of the servers, therefore a perl version that comes with Oracle software will do.
0
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
ID: 39793444
perl -le '($h,$d)=(localtime)[2,3]; print join"/",(split/ /,localtime time-($d*24+$h-12)*60*60)[2,1,4];'
will work across daylight saving time switchover
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now