Date Calculation Help
Posted in 2008
Topics: General Discussion
On the first of each month I need to do a calculation based on how many days were in the previous month. I can do the calculation in Informix or with Unix scripting. Any suggestions?
You mean something like this? DATABASE sample MAIN DEFINE my_date DATE, my_day SMALLINT LET my_date = "05/01/08" LET my_date = my_date - 1 LET my_day = DAY(my_date) DISPLAY my_day END MAIN Gives you "30". --EEM > -----Original Message----- > From: ids-bounces@iiug.org [mailto:ids-bounces@iiug.org] On Behalf Of > MARK DUNKER > Sent: Monday, October 27, 2008 9:14 AM > To: ids@iiug.org > Subject: Date Calculation Help [13803] > > On the first of each month I need to do a calculation based on how many > days were in the previous month. > > I can do the calculation in Informix or with Unix scripting. Any > suggestions? > > > *********************************************************************** > ******** > Forum Note: Use "Reply" to post a response in the discussion forum.
Here's some simple shell code I wrote to run a report for week-end (Sun.), month-end and year-end. It calculates dates for report parameters but the date stuff should get you started. This is just the date-related pieces of the larger script. Great? No. Functional? Yes. It ran on Linux (bash) so the array might be different in other shells. #!/bin/sh # . /etc/ifx.env # set -xv DOW=`/bin/date +%w ` # day-of-week DOM=`/bin/date +%e ` # day-of-month start_time=`/bin/date +%T ` SUN=0 SAT=6 # days in each month, start with month=0 monthdays=(30 31 28 31 30 31 30 31 31 30 31 30 31) #......... 0 J F M A M J J A S O N D RUN_Y=`date +%Y` RUN_M=`date +%m` RUN_D=`date +%d` # Check for leap year leap_test=$(( $RUN_Y % 4 )) if [ $DOM -eq 1 ] # running end-o-month then if [ $RUN_M -eq 1 ] # adjust for year-end then # decrement year RUN_Y=`expr $RUN_Y - 1` BEG_DATE="12/01/$RUN_Y" END_DATE="12/31/$RUN_Y" else # adjust for month-end # decrement month RUN_M=`expr $RUN_M - 1` # set day to last day of last month RUN_D=${monthdays[$RUN_M]} # is it March in leap year? if [ $RUN_M -eq 2 -a $leap_test -eq 0 ] then RUN_D=29 fi BEG_DATE="$RUN_M/01/$RUN_Y" END_DATE="$RUN_M/$RUN_D/$RUN_Y" fi else # decrement day RUN_D=`expr $RUN_D - 1` BEG_DATE="$RUN_M/01/$RUN_Y" END_DATE="$RUN_M/$RUN_D/$RUN_Y" fi RUN_DATE=`/bin/date "+%Y-%m-%d"` # today -- Bob -------------- Original message -------------- From: "MARK DUNKER" <mr_mark95@go.com> > On the first of each month I need to do a calculation based on how many days > were in the previous month. > > I can do the calculation in Informix or with Unix scripting. Any suggestions? > > > *******************************************************************************
Yes....perfect! Thanks.
On Mon, Oct 27, 2008 at 7:28 AM, Everett Mills <eemills@nationalbeef.com>
wrote:
> You mean something like this?
>
> DATABASE sample
Completely superfluous - the code does not access the database, so you
should omit it.
It will speed the program up, too.
> MAIN
>
> DEFINE
>
> my_date DATE,
>
> my_day SMALLINT
>
> LET my_date = "05/01/08"
>
> LET my_date = my_date - 1
>
> LET my_day = DAY(my_date)
>
> DISPLAY my_day
>
> END MAIN
>
> Gives you "30".
Neat. Can be generalized by doing:
LET my_date = my_date - DAY(my_date) -- instead of - 1.
This subtracts 16 from the 16th of the month, giving you the last day
of the prior month, whereupon the DAY function does indeed work.
>> From: On Behalf Of MARK DUNKER
>>
>> On the first of each month I need to do a calculation based on how
>> many days were in the previous month.
>>
>> I can do the calculation in Informix or with Unix scripting. Any
>> suggestions?
Consider a rather simple stored procedure - the hard part is choosing
a good name.
CREATE PROCEDURE days_in_prior_month(d DATE DEFAULT TODAY) RETURNINGINT AS num_days;
RETURN DAY(d - DAY(d));
END PROCEDURE;
Untested - might work...
--
Jonathan Leffler #include <disclaimer.h>
Email: jleffler@earthlink.net, jleffler@us.ibm.com
Guardian of DBD::Informix v2008.0513 -- http://dbi.perl.org/
"Blessed are we who can laugh at ourselves, for we shall never cease
to be amused."
NB: Please do not use this email for correspondence.
I don't necessarily read it every week, even.