#!/usr/bin/perl use warnings; use strict; #Without using any built in date or time functions, write a #function or method that accepts two mandatory arguments. #The first argument is a string of the format "[H]H:MM {AM|PM}" #and the second argument is an integer. Assume the integer is #the number of minutes to add to the string. The return value of #the function should be a string of the same format as the first #argument. For example AddMinutes("9:13 AM", 10) would return #"9:23 AM". The exercise isn't meant to be too hard. We just #want to see how you code. Feel free to do it procedurally or #in an object oriented way, whichever you prefer. #Use any language you want. # # Note, this is perl, one of my favorite languages # along with php # #Subroutine/Function Section sub Convert_to_min# # Input: A string of the form "9:23 AM" or "11:55 PM" for example # denoting the time, note the space and colon positions. # # Returns a number between 0 and # 1500 minutes, note 720 is high noon and 1440 is midnight # This is similar to military time, but just computed in minutes # 9:30 AM is 570 minutes. Note anything over 1440 minutes is the next # day. We have to flip the PM to AM then. This is taken care of in # a later routine. see sub Convert_Military_time # { my ($inline) = @_; my $add_pm = 0; my $result = 0; my $hours = 0; my $minutes = 0; $result = index($inline, ":"); if ($result < 1 ) # i.e. False { print "time must contain a colon \n"; exit; } $hours = substr($inline,0, $result); $minutes = substr($inline, $result + 1 , 2); #Note, minutes will always be the next two digits after the colon $result = index($inline, "PM"); if ($result > 0 ) { $add_pm += 720; } # NOTE, assuming if not PM its AM # no need to add a value for PM for minutes added. $minutes += $hours * 60 ; $minutes += $add_pm; return $minutes; } #end sub Convert_to_min sub Convert_Military_time # input is a number between 0 and 1500 denoting military minutes time # output is normal time string, i.e. "12:05 PM" { my ($minutes_time) = @_; my $hours = 0; my $minutes = "00" ; my $time_of_day = " AM"; my $normal_time = "0:00 AM"; my $position_decimal_pt = 0; # used later so I can do an integral divide # that is throwing away the remainder if # there is one. # Watch out for next days entry right before midnight, ie. if ($minutes_time > 1440) #example for entry of "11:55 PM , 10 " # where we add 10 minutes to the initial # time yielding 1445 as military minutes # input for this function. { $time_of_day = " AM"; $minutes_time -= 1440; } else {if ($minutes_time > 720) { $time_of_day = " PM"; } } $minutes = $minutes_time % 60 ;# % is mod operator if ($minutes < 10) {$minutes = "0" . $minutes ;} # so it looks pretty, ie. 05, not 5 # for 12:05 PM $hours = $minutes_time / 60 ; # note div for integral divide # throwing away remainder by searching for stuff after decimal point # BUT if its a number with no remainder, there will be no decimal # point. $position_decimal_pt = index($hours, "."); if ($position_decimal_pt == -1) # means no decimal point in hours { $normal_time = $hours . ":" . $minutes . $time_of_day; return $normal_time; } else # Got a decimal point { $hours = substr($hours, 0 , $position_decimal_pt); $normal_time = $hours . ":" . $minutes . $time_of_day; return $normal_time; } } #end sub Convert_Military_time # End Subroutine Section # calculate desired element of standard Fibonacci sequence # Main Section # # usage ./perltimeadd "9:23 AM" , 10 my $inittimevalue = shift(@ARGV); unless($inittimevalue) { print " Sorry, you forgot to enter the time and minutes to add "; print " on. \n i.e. perltimeadd \"9:23 AM\" , 10 \n"; exit; } my $comma = shift(@ARGV); # This should throw away the comma my $initminaddvalue = shift(@ARGV); unless($initminaddvalue) { print "Sorry, you forgot to enter the minutes to add \n"; print " or you negleacted to put in the comma buddy.\n"; print " MAKE SURE their is a space before and after the comma too.\n"; print " You know us computers are not too smart! \n"; print " i.e. perltimeadd \"9:23 AM\" , 10 \n"; exit; } my $minutes_time = Convert_to_min($inittimevalue); my $normal_time = "0:00 AM"; print "This is original input: $inittimevalue , $initminaddvalue \n"; print "Mintes to add = $initminaddvalue \n"; $minutes_time += $initminaddvalue; print " Result in military minutes time = $minutes_time \n"; print " Note 1440 minutes = midnight, 720 minutes = noon \n"; $normal_time = Convert_Military_time($minutes_time); print " Result in normal time = $normal_time \n"; exit; # End Main Section