get-date and semesters

i have a script that runs to create a list of students.  The script will have different outputs for different semesters. Currently, the program reads a configuration file to get the semester, however this year someone (actually NOT me) didn’t update the configuration file and the results were less than optimal.

I can/did update the file, but since i know appropriately when the change of semester will occcur, can i write code to change the semester variable?

John Cook has a cookbook (of COURSE he does, pun may\may not be intended) with a very short section on dates – but it exactly takes care of my needs.

(Get-Date) - (get-date 1/1/2013)

returns the time between today and the beginning of 2013.

Days : 254
Hours : 10
Minutes : 58
Seconds : 16
Milliseconds : 826
Ticks : 219850968268411
TotalDays : 254.45713919955
TotalHours : 6106.97134078919
TotalMinutes : 366418.280447352
TotalSeconds : 21985096.8268411
TotalMilliseconds : 21985096826.8411

 The same code, wrapped in parens with (dot)days is the days part of the time data (254). 

((Get-Date) - (get-date 1/1/2013)).days

Notice that (get-date x/xx/XXXX) turns the string x/x/xxxxcoersed into a datetime object.

So if the current date is greater than 01/01/2014, we have started the spring 2014 semester (actually the january intersession) and if the current date is greater than 06/01/2014 and the beginning of the summer semester is June 1, the semester variable should be

$semester = “14SX”

We can create a switch that will compare the current date to a series of semester dates and assume the variable to each step – with the final assignment being the current date.  If we get to the last date we can also send email (daily as the program runs) to the manager or to the programmer about entering some additional dates.

$a = (get-date 1/26/2015)
$email = $false
$notify = $false
switch ($a){
{($a) -ge (get-date 9/1/2013)}{$semester = "13FA"}

{($a) -eq (get-date 1/1/2014)}{$semester = "14JA"; $email=$true}
{($a) -gt (get-date 1/1/2014)}{$semester = "14JA"}

{($a) -eq (get-date 1/25/2014)}{$semester = "14SP"; $email=$true}
{($a) -gt (get-date 1/25/2014)}{$semester = "14SP"}

{($a) -eq (get-date 6/1/2014)}{$semester = "14SX"; $email=$true}
{($a) -gt (get-date 6/1/2014)}{$semester = "14SX"}

{($a) -eq (get-date 9/1/2014)}{$semester = "14FA"; $email=$true}
{($a) -gt (get-date 9/1/2014)}{$semester = "14FA"}

{($a) -eq (get-date 1/1/2015)}{$semester = "15JA"; $email=$true}
{($a) -gt (get-date 1/1/2015)}{$semester = "15JA"}

{($a) -eq (get-date 1/25/2015)}{$semester = "15SP"; $email=$true}
{($a) -gt (get-date 1/25/2015)}{$semester = "15SP";$notify = $true}

default {$semester = "not assigned"}
}
$semester
If ($email) { write-host "changing semester to $semester"}
If ($notify) { write-host "$semester is the final semester coded into the script at ..."}
}

The switch will get the current value of the date (in this case for testing, we have set that to a future date), and then compare it to the critical dates for each semester.  On “equals”, the semester variable is updated and an flag set to send an email indicating that the semester has changes (and the out of the program will reflect the new semester).  The ‘greater than” logical operator will fire for each date after the first so the semester will be reset multiple times – only the last one is kept.  Only on the “equals” date will the email on change of semester be sent. On later dates, the variable is set but no email.  When we start the last configured semester, we intend to send a notification of the lack of new dates to the programmer and to the the program manager.  

Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.