Note: The default ITS GitLab runner is a shared resource and is subject to slowdowns during heavy usage.
You can run your own GitLab runner that is dedicated just to your group if you need to avoid processing delays.

Commit 619506d3 authored by Kendra Brooke Kleber's avatar Kendra Brooke Kleber
Browse files

function and test case

parents
<?php
/**
* AddMinutes adds time in clock format
* Requires: the first argument is a 12-hour time string with the format "[H]H:MM {AM|PM}",
* and the second argument is a signed integer
* Modifies: nothing
* Effects: The sum is returned in the same format as the first argument
**/
function AddMinutes($time, $minutes) {
//If negative, add a full day less the minutes, ignore full days
if(intval($minutes) < 0) {
$minutes = (1440 + ($minutes % 1440)) % 1440; //PHP mod doesn't work for negative numbers
}
//Ignore complete days added
elseif(intval($minutes) >= (60*24)) {
$minutes = intval($minutes)%(60*24);
}
//Extract numbers from time string
$ampm = substr($time, -2);
$afternoon;
if($ampm == "AM") {
$afternoon = 0;
} elseif($ampm == "PM") {
$afternoon = 1;
} /*else {
return "Time formatting error";
}*/
$curr_mins = substr($time, -5, 2);
$curr_hours = substr($time, 0, strrpos($time, ":")); //strpos could be false
//Assuming a well-formatted input,
//recording for AM/PM calculation before modifying values
$curr_mins_sum = $curr_mins + ($curr_hours%12)*60;
//Add minutes
$more_minutes = ($minutes%60);
$carry = false;
$curr_mins = ($more_minutes + $curr_mins);
if(($curr_mins) >= 60) {
$curr_mins = ($curr_mins)%60;
$carry = true;
}
//Carry the hour if needed
$more_hours = intdiv($minutes, 60) + $carry; //implicit cast of bool to int
//Flip AM/PM if needed
$flip = ($curr_mins_sum+$minutes)%1440;
if($flip > 719 && $flip <= 1439) {
$afternoon = ($afternoon + 1)%2; //inverse
}
//Add hours
$curr_hours = ($more_hours + $curr_hours)%12;
if($curr_hours == 0) {
$curr_hours = 12;
}
//Formatted output
return $curr_hours.":".($curr_mins < 10 ? "0" : "").$curr_mins." ".($afternoon ? "PM" : "AM");
}
?>
\ No newline at end of file
I used an online PHP compiler for convenience, so I tested these manually
+--------------------+----------------+
| Input | Correct Output |
+--------------------+----------------+
| "9:13 AM", 200 | 12:33 PM |
+--------------------+----------------+
| "1:00 AM", "1" | 1:01 AM |
+--------------------+----------------+
| "1:00 AM", "60" | 2:00 AM |
+--------------------+----------------+
| "12:00 AM", "60" | 1:00 AM |
+--------------------+----------------+
| "11:59 PM", "1" | 12:00 AM |
+--------------------+----------------+
| "11:59 AM", "1" | 12:00 PM |
+--------------------+----------------+
| "12:00 PM", "1" | 12:01 PM |
+--------------------+----------------+
| "12:00 PM", "1441" | 12:01 PM |
+--------------------+----------------+
| "12:00 PM", "721" | 12:01 AM |
+--------------------+----------------+
| "12:00 PM", "719" | 11:59 PM |
+--------------------+----------------+
| "1:00 PM", "721" | 1:01 AM |
+--------------------+----------------+
| "9:13 AM", -720 | 9:13 PM |
+--------------------+----------------+
| "9:13 AM", -1 | 9:12 AM |
+--------------------+----------------+
| "9:13 AM", -553 | 12:00 AM |
+--------------------+----------------+
| "9:13 AM", 887 | 12:00 AM |
+--------------------+----------------+
| "9:00 AM", 900 | 12:00 AM |
+--------------------+----------------+
| "9:13 AM", -2881 | 9:12 AM |
+--------------------+----------------+
| "12:00 AM", -2 | 11:58 PM |
+--------------------+----------------+
| "9:13 AM", 1439 | "9:12 AM" |
+--------------------+----------------+
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment