Best way to increment weeks based on fiscal start year

Hi Folks -

I'm looking for the best way to to increment fiscal weeks - allow me to explain.

At my one client, 10/01/17 was the beginning if year fiscal year 2018.
Each week, I need to manage a unique set of variable that are updated in my application - they are called substitution variables.

I manage them (manually) in a comma delimited format, and then run a ksh over them to extract both columns and spool them to an "*.mxl" file in a specific import format.

For instance:

curwk_d,'wk04 fy18';
pw1_d,'wk03 fy18';
pw2_d,'wk02 fy18';
pw3_d,'wk01 fy18';
pw4_d,'wk53 fy17';
p5w_d,'wk52 fy17';
p6w_d,'wk51 fy17';
p7w_d,'wk50 fy17';
p8w_d,'wk49 fy17';
p9w_d,'wk48 fy17';
p10w_d,'wk47 fy17';
p11w_d,'wk46 fy17';
p12w_d,'wk45 fy17';

And then the import format is as such:

alter system set variable curwk_d 'wk04 fy18';
alter system set variable pw1_d 'wk03 fy18';
alter system set variable pw2_d 'wk02 fy18';
alter system set variable pw3_d 'wk01 fy18';
alter system set variable pw4_d 'wk53 fy17';
alter system set variable pw5_d 'wk52 fy17';
alter system set variable pw6_d 'wk51 fy17';
alter system set variable pw7_d 'wk50 fy17';
alter system set variable pw8_d 'wk49 fy17';
alter system set variable pw9_d 'wk48 fy17';
alter system set variable pw10_d 'wk47 fy17';
alter system set variable pw11_d 'wk46 fy17';
alter system set variable pw12_d 'wk45 fy17';

Then, the "*.mxl" file is read into the target system using a certain utility called from a shell script, but I digress.

As you can see, there are 13 variables. 1 is the current week (from start of fiscal) and the 12 additional previous week variables.

For instance, current week is the upcoming week, which is the 4th week since start of fiscal, as indicated by wk04.

My question is, is there an easy way to manage this and increment as necessary each week (on Saturday) when this is run?

Thank you!

Not sure I fully understand. You want to switch "curwk_d" from "wk04 fy18" to "wk05 fy18", "pw1_d" to "wk04 fy18", and so forth? What if a week 53 doesn't exist? What if the script is accidentally run twice on a Sunday?

Hi Rudy -

You want to switch "curwk_d" from "wk04 fy18" to "wk05 fy18", "pw1_d" to "wk04 fy18", and so forth?

Yes

What if a week 53 doesn't exist?

This will never be the case, there will always be 53 weeks for this client

What if the script is accidentally run twice on a Sunday?

It should never be run twice, however I suppose there is that possibility. The plan is to schedule it so that doesn't happen.

Please let me know if you need anything else!

How about

while IFS=", " read PW WK FY; do WK=$(( ${WK##*[a-z]}%53 + 1 )); printf "$PW, 'wk%02d $FY\n" $WK; done < file
curwk_d, 'wk05 fy18';
pw1_d, 'wk04 fy18';
pw2_d, 'wk03 fy18';
pw3_d, 'wk02 fy18';
pw4_d, 'wk01 fy17';
p5w_d, 'wk53 fy17';
p6w_d, 'wk52 fy17';
p7w_d, 'wk51 fy17';
p8w_d, 'wk50 fy17';
p9w_d, 'wk49 fy17';
p10w_d, 'wk48 fy17';
p11w_d, 'wk47 fy17';
p12w_d, 'wk46 fy17';

and / or

while IFS=", " read PW WK FY; do WK=$(( ${WK##*[a-z]}%53 + 1 )); printf "alter system set variable $PW, 'wk%02d $FY\n" $WK; done < file
alter system set variable curwk_d, 'wk05 fy18';
alter system set variable pw1_d, 'wk04 fy18';
alter system set variable pw2_d, 'wk03 fy18';
alter system set variable pw3_d, 'wk02 fy18';
alter system set variable pw4_d, 'wk01 fy17';
alter system set variable p5w_d, 'wk53 fy17';
alter system set variable p6w_d, 'wk52 fy17';
alter system set variable p7w_d, 'wk51 fy17';
alter system set variable p8w_d, 'wk50 fy17';
alter system set variable p9w_d, 'wk49 fy17';
alter system set variable p10w_d, 'wk48 fy17';
alter system set variable p11w_d, 'wk47 fy17';
alter system set variable p12w_d, 'wk46 fy17';

EDIT: Sorry, I had overlooked the fiscal year's increment on change from week 53 to 1. Here it is:

while IFS=", " read PW WK FY
  do    WKO=${WK##*[a-z]}
        WK=$(( WKO%53 + 1 ))
        FY="${FY//[a-z;\']/}"
        [ "$WKO" -gt "$WK" ] && (( FY++ ))
        printf "alter system set variable %s, 'wk%02d fy%s';\n" $PW $WK $FY
  done < file
1 Like

Note that if this customer always has a fiscal week #53, there must be some years that don't have a fiscal week #1.

I have never seen a fiscal calendar like this used under any circumstances. Usually, the number of fiscal weeks in a year (i.e., 52 or 53) depends on what day of the week the 1st fiscal day of the year occurs.

Please explain how the 1st fiscal day of this customer's year is determined.

1 Like

Hi Don -

Thanks for the follow up. So, I have some information.

The 1st fiscal day of my client's year is the first Saturday in October, every year.

Also, thhey operate on a 53 week year every 5 years. For instance, the last 53 week year was 2012.

Rudi's solution is working, however I was hoping to see the above conditions to determine the subvar advancements. Would that be possible?

Hi SIMMS7400,
In post #51 in your earlier thread (Parsing a column of text file - best practices) I gave you code that contains a function ( findSaturdayOnOrBefore() ) that finds the 1st Saturday on or before a date given to it as an argument. Since it worked for me when I was testing it and you never said that it didn't work for you, I assumed that it worked for you (although you haven't acknowledged seeing that post).

Can't you use that function to determine the 1st Saturday on or before 10/07/YYYY ? Isn't the result returned by that function the first day of a fiscal year for your customer? Can't you then find the first day of the next fiscal year using that function with 10/07/$((YYYY+1)) as the argument. And then, can't you add one week to the first day in a fiscal year in a loop until you hit the 1st week of the next fiscal year saving the 52 or 53 values you get in that loop as the start of the 52 or 53 weeks in that fiscal year?

Why don't you try doing this on your own, show us what you come up with, and let us know if you get stuck?

PS: Is 10/07/2017 the first day of your customer's FY2017 or is it the first day of FY2018?

PPS: Note that we are here to help you learn how to use the tools on your UNIX or UNIX-like system to write code that makes it do what you want it to do. We are not here to act as your unpaid programming staff.

Hi Don -

Yes you're right. I will try on my own.

However, for this purpose, for some reason that function isn't returning anything.

When I pass

findSaturdayOnOrBefore "10/07/2018"

nothing returns. Any ideas?

How do these two statements fit together?

What do you mean?

1 Like

Did you look at the code for that function? Read the comments that explain what it does. That function doesn't print anything; it sets variables you can use after you call it.

Hi Don -

Ah yes - I see that. I meant to say, this environment doesn't have GNU date installed, therefore the -d switch is not accepted.

You should know by now that you need to tell us what operating system and shell you're using. So, what shell are you using?

Do you have access to a 1993 or later Korn shell? If so, what output do you get from:

ksh --version

or:

ksh93 --version

depending on how it is installed on your system?

1 Like

HI Don -

The version is as follows:

  version         sh (AT&T Research) 93t+ 2009-05-01

Thank you!

With ksh version 93u+, the command:

printf '%(%m/%d/%Y)T\n' "today $((($(printf '%(%u)T' today) + 1) % 7)) days ago"

prints today's date if you run it on a Saturday and prints the previous Saturday's date if you run it on any other day of the week. Does this command work with your Korn shell? (I'm not sure which version of ksh first supported printf '%(date_format)T' date processing.)

Hi Don -

Thank you for taking the time to go over this with me.

I will try this out and get back to you.

---------- Post updated at 06:33 PM ---------- Previous update was at 06:29 PM ----------

Hi Don -

I'm getting the following error:

((0)T + 1) % 7: syntax error

If you have a very recent bash , you might also try that ksh printf command with it. If that doesn't work, you can try perl as discussed in General Purpose Date Script, or you can try parsing the output from the cal utility, or you can write some C code using the mktime(), strftime(), and/or strptime() functions.

Hi Rudi -

I'm getting the following error with your code (particularly your 3rd example - which is the correct one for the fiscal year increment going from 53 to 1:

Once I can correct that, I think i can use that same example to build some dynamic loops. ANy ideas as to what the error is? I tried to correct to no avail.

Thank you!

HI Rudi -

Sorry for the bother - I can't seem to figure out the above error. Do you have any idea?

AIX / kornshell

It works fine with my bash . You have to experiment with your ksh . Double check with your man ksh on "Parameter Expansion". It may not accept the escaped single quote - test with leaving that out, and / or using "'" instead.

1 Like

Thanks, Rudi!

Been playing around with this but unfortunately unable to get it to work.

I'll keep plugging away.