/branches/david_mrhouse/code/public/schoolday.pl

# · Perl · 239 lines · 132 code · 63 blank · 44 comment · 34 complexity · 50181655f61b277aa99fa86080b028a3 MD5 · raw file

  1. # Category = Time
  2. #@
  3. #@ These function will calculate if Today or Tomorrow are
  4. #@ schooldays. Multiple schools are supported.
  5. #@ A list of term dates and an optional list of mid-term
  6. #@ holidays are required for each school.
  7. # 2006-01-27 Andrew C Baker andrew@rekabuk.co.uk
  8. # shoolname => ["start0-end0, start1-end1, ...", "day0,day1"]
  9. my %school_terms = (
  10. Churston => ["1/1-28/01, 7/6-18/09, 21/10-21/12", "5/6,3/4,27/01"],
  11. Chestnut => ["1/1-27/02, 7/7-18/10, 21/11-21/12", ""]
  12. );
  13. # Processed term dates
  14. my %terms;
  15. my $date_format = "ddmmyy";#config_parms{date_format};
  16. $v_is_school_day = new Voice_Cmd( 'Is today a school day [Churston,Chestnut]');
  17. $v_is_school_night = new Voice_Cmd( 'Is tonight a school night [Churston,Chestnut]');
  18. #---------------------------------------------
  19. sub Convert_To_ISO8601_Date {
  20. my $date = shift;
  21. my ($y,$m,$d);
  22. my ($yyyy,$mm,$dd);
  23. if ($date_format =~ /ddmm/) {
  24. ($d,$m,$y) = split("/",$date);
  25. } else {
  26. ($m,$d,$y) = split("/",$date);
  27. }
  28. if (length($m) == 1) {
  29. $mm = "0".$m;
  30. } else {
  31. $mm = $m;
  32. }
  33. if (length($d) == 1) {
  34. $dd = "0".$d;
  35. } else {
  36. $dd = $d;
  37. }
  38. if (!defined $y) {
  39. my $today = today();
  40. $yyyy = $Year;
  41. } else {
  42. $yyyy = $y if length($y) == 4;
  43. $yyyy = "20".$y if length($y) == 2; # This will *only* work until
  44. 2099.
  45. }
  46. # 1 Hour into the day
  47. #print "Convert out = $dd/$mm/$yyyy\n";
  48. #Months are 0..11
  49. return timelocal(0, 0, 1, $dd, $mm-1, $yyyy);
  50. }
  51. #---------------------------------------------
  52. sub LoadSchool {
  53. my $school = shift;
  54. my $school_terms = shift;
  55. my $school_inset = shift;
  56. my $i;
  57. # Load Term dates
  58. $i = 0;
  59. my $term;
  60. foreach $term (split(",",$school_terms)) {
  61. my($start,$finish);
  62. my($d,$m,$y);
  63. ($start,$finish) = split("-",$term);
  64. $start = Convert_To_ISO8601_Date($start);
  65. $finish = Convert_To_ISO8601_Date($finish);
  66. $terms{$school}{terms}[$i][0] = $start;
  67. $terms{$school}{terms}[$i][1] = $finish;
  68. #print("School=$school, Term=$i, Start=$terms{$school}{terms}[$i][0], Finish=$terms{$school}{terms}[$i][1]\n");
  69. $i++;
  70. }
  71. # Load inset dates
  72. $i = 0;
  73. my $inset;
  74. foreach $inset (split(",",$school_inset)) {
  75. my $inset_day;
  76. $inset_day = Convert_To_ISO8601_Date($inset);
  77. $terms{$school}{insets}[$i] = $inset_day;
  78. #print("School=$school, Inset=$i, Date=$terms{$school}{insets}[$i]\n");
  79. $i++;
  80. }
  81. }
  82. #---------------------------------------------
  83. if ($Reload) {
  84. my $school;
  85. for $school (keys %school_terms) {
  86. LoadSchool( $school, $school_terms{$school}[0], $school_terms{$school}[1] );
  87. }
  88. }
  89. #---------------------------------------------
  90. # Is the supplied day a school day
  91. sub IsSchoolDay {
  92. my $testdate = shift;
  93. my $school = shift;
  94. # We can accept dd/mm/yy or an epoch number
  95. if ($testdate =~ /\//) {
  96. $testdate = Convert_To_ISO8601_Date($testdate);
  97. }
  98. #print "Test Date (Day) = $testdate\n";
  99. # No school at the weekend
  100. my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($testdate);
  101. if (($wday == 6) || ($wday == 0)) {
  102. return 0;
  103. }
  104. #Check term dates
  105. my ($j, $k);
  106. print "$#{ $terms{$school}{terms}}\n";
  107. for $j ( 0 .. $#{ $terms{$school}{terms}} ) {
  108. print "$j $terms{$school}{terms}[$j][0]] $terms{$school}{terms}[$j][1]\n";
  109. if (($testdate >= $terms{$school}{terms}[$j][0]) and ($testdate <= $terms{$school}{terms}[$j][1])) {
  110. # Now check if it's an inset day
  111. for $k ( 0 .. $#{ $terms{$school}{insets}} ) {
  112. if ($testdate == $terms{$school}{insets}[$k]) {
  113. print_log "Inset day $school";
  114. return 0;
  115. }
  116. }
  117. #Need to check for holidays
  118. #for () {
  119. # if Holiday {
  120. # return 0
  121. #}
  122. #}
  123. return 1;
  124. }
  125. }
  126. return 0;
  127. }
  128. #---------------------------------------------
  129. # Is the supplied day a school night
  130. sub IsSchoolNight {
  131. my $testdate = shift;
  132. my $school = shift;
  133. my $testdate_tomorrow;
  134. # We can accept dd/mm/yy or an epoch number
  135. if ($testdate =~ /\//) {
  136. $testdate = Convert_To_ISO8601_Date($testdate);
  137. }
  138. #Get tomorrows date
  139. $testdate += 86400;
  140. #print "Test Date (Night) = $testdate\n";
  141. # Is tomorrow a school_day?
  142. return IsSchoolDay( $school, $testdate);
  143. }
  144. #---------------------------------------------
  145. # Is today a school day?
  146. sub IsSchoolToday {
  147. my $school = shift;
  148. return 0 if ($Holiday);
  149. my $iso_date = $Mday."/".$Month."/".$Year;
  150. my $epoch_date = Convert_To_ISO8601_Date($iso_date);
  151. return IsSchoolDay($epoch_date, $school);
  152. }
  153. #---------------------------------------------
  154. #Is tomorrow a school day?
  155. sub IsSchoolTomorrow {
  156. my $school = shift;
  157. my $iso_date = $Mday."/".$Month."/".$Year;
  158. my $epoch_date = Convert_To_ISO8601_Date($iso_date);
  159. return IsSchoolNight($epoch_date, $school);
  160. }
  161. #---------------------------------------------
  162. if ($state = state_now $v_is_school_day) {
  163. if ( IsSchoolToday( $state) ) {
  164. speak "Its a $state school day";
  165. } else {
  166. speak "Its not a $state school day";
  167. }
  168. }
  169. #---------------------------------------------
  170. if ($state = state_now $v_is_school_night) {
  171. if ( IsSchoolTomorrow( $state)) {
  172. speak "Its a $state school night";
  173. } else {
  174. speak "Its not a $state school night";
  175. }
  176. }