Java 7, 164 bytes
String c(String a,String b){long s=x(b,1)-x(a,1)+(x(b,0)-x(a,0))*60,m=s%60;return(s/60)+":"+(m>9?m:"0"+m);}long x(String s,int i){return new Long(s.split(":")[i]);}
Explanation:
String c(String a, String b){ // Method with two String parameters and String return-type
long s = x(b,1) - x(a,1) // Get difference in seconds from input times
+ (x(b,0) - x(a,0)*60, // plus the difference in minutes times 60 to get the seconds
m = s%60; // Temp variable of seconds after we've subtracted the minutes (used multiple times)
return (s/60) // Return minutes
+":" // plus ":"
+(m>9?m:"0"+m); // plus seconds (with a leading 0 if necessary)
} // End of method
long x(String s,int i){ // Separate ethod with String and Integer parameters and long return-type
return new Long(s.split(":")[i]; // Return either minutes or seconds of String parameter based on the index
} // End of method
Test code:
Try it here.
class M{
String c(String a,String b){long s=x(b,1)-x(a,1)+(x(b,0)-x(a,0))*60,m=s%60;return(s/60)+":"+(m>9?m:"0"+m);}long x(String s,int i){return new Long(s.split(":")[i]);}
public static void main(String[] a){
M m = new M();
System.out.println(m.c("0:00", "0:01"));
System.out.println(m.c("0:55", "1:00"));
System.out.println(m.c("1:45", "3:15"));
}
}
Output:
0:01
0:05
1:30
1In dc the input is read incorrectly, since
:
is a command (data as code philosophy). Am I allowed to use spaces instead or do I need to find other language to answer this? – seshoumara – 2017-03-28T21:10:41.1401is
01:30
valid output? (leading zero) – Luis Mendo – 2017-03-28T21:18:56.9332Yes, I'll accept that as valid. I'll edit the description. – Josh – 2017-03-28T21:35:25.340
1@seshoumara The input must contain a colon. – Josh – 2017-03-28T21:41:41.420
What is the maximum value expected for m? – Digital Trauma – 2017-03-28T22:06:50.107
Maximum expected is 9. @DigitalTrauma – Josh – 2017-03-28T22:08:03.027
@obarakon, it is in format of minutes:seconds, not hours:minutes:seconds, so your solution isn't valid unfortunately. – Josh – 2017-03-29T18:38:22.350
Is the result guaranteed to be positive. so "3:15" "1:45" is not considered valid input? – Matt – 2017-03-31T15:23:32.040
The result will always be positive – Josh – 2017-04-03T21:39:37.157