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
406 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

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 …
Today, still in the boom of Apple, PC's and products, nearly 50% of the computer users use Windows as graphical operating systems. If you are among those users who love windows, but are grappling to keep the system's hard drive optimized, then you s…
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…

679 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