On January 1, i want to do something different.  No, in ADDITION to not being hung-over, i want to change a constant in my program.  I could change it manually, or pull it from a configuration file, but that means i’d have to remember.  Or more correction, the manager for whom i wrote the program would have to remember.  And yesterday, he suddently discovered that he hadn’t remembered.  The clients told him that.

OK, so i can get the current date.  And i can do datetime math.  IF i can specify a date, such as 1/1/2014, i can determine if (now -eq triggerdate), and in that case, change the constant.  Or more powerfully, using a switch statement check for a bunch of dates which one we are going to use and therefore which value to assign to the constant.

Clear as mud, huh?  If it is after January 2014, the semester is “14SP” until June 1, 2014 in which case the semester is “14SX” until September 1 when the semester is “14FA”.  Oops, forgot the January semester. Anyway, the critical factor is to establish the trigger dates (and to be sure that i use a break statement in the switch so that i don’t keep checking.  I obviously can’t use “-eq” so it has to be “greater than” and the dates run from high to low.  The alternative is to check and update a configuration file, which means we have to worry about that file as well as the code and the output files, et al.  Keep it all in one place.  AND when we read the first date (most into the future) as the match, i can also send an email to me or the manager saying the file needs to be updated.  (and it will happen DAILY until the updates are made).

As usual with Powershell, i was making it too difficult.  John Cook has a very simple cookbook (not sure if that is a pun-intended or not by him) entry:

PS C:\work\powershell> $a = get-date 9/1/2014
PS C:\work\powershell> $a
Monday, September 01, 2014 12:00:00 AM


PS C:\work\powershell> (get-date) -gt (get-date 9/1/2013)
PS C:\work\powershell> (get-date) -gt (get-date 9/1/2014)

They are both doing the same thing – use get-date to return a datetime object.  Proceed from there.

This entry was posted in Uncategorized. Bookmark the permalink.