Difference between revisions of "Find the number of minutes in a HH:MM:SS time string"

From CodeCodex

(Haskell)
(Ruby)
 
(14 intermediate revisions by 13 users not shown)
Line 1: Line 1:
 
==Implementations==
 
==Implementations==
===Java===
+
 
 +
 
 +
===Erlang===
 
<pre>
 
<pre>
 +
time_to_minutes(Time_str) ->
 +
    F = fun(X, T) -> 60*T + list_to_integer(X) end,
 +
    lists:foldl(F, 0, string:tokens(Time_str, ":")) / 60.
 +
</pre>
 +
 +
===Haskell===
 +
<pre>
 +
import Text.Regex
 +
 +
timeToMinutes :: String -> Double
 +
timeToMinutes time_str
 +
  = hours * 60 + minutes + seconds /60
 +
    where [hours, minutes, seconds] = map read $ splitRegex (mkRegex ":") time_str
 +
</pre>
 +
 +
 +
Below is the same solution in pointfree style while also reusing some more standard functions.
 +
<pre>
 +
import Text.Regex
 +
ttm :: String -> Double
 +
ttm = sum . zipWith ($) [(*60), id, (/60)] . map read . (splitRegex . mkRegex) ":"
 +
</pre>
 +
 +
===Java===
 +
<pre class="java">
 
/**
 
/**
 
* Turns a period of time into the number of minutes represented
 
* Turns a period of time into the number of minutes represented
Line 15: Line 42:
 
try {
 
try {
 
 
minutes += Double.valueOf(split[0])*60;
+
minutes += Double.parseDouble(split[0])*60;
minutes += Double.valueOf(split[1]);
+
minutes += Double.parseDouble(split[1]);
minutes += Double.valueOf(split[2])/60;
+
minutes += Double.parseDouble(split[2])/60;
 
return minutes;
 
return minutes;
 
 
Line 26: Line 53:
 
}
 
}
 
</pre>
 
</pre>
===Haskell===
 
<highlightsyntax language="haskell">
 
import Text.Regex
 
  
timeToMinutes :: String -> Double
+
===Perl===
timeToMinutes time_str
+
<pre class="perl">
  = hours * 60 + minutes + seconds /60
+
sub time_to_minutes {
     where [hours, minutes, seconds] = map read $ splitRegex (mkRegex ":") time_str
+
    my ($time_str) = @_;
</highlightsyntax>
+
     my ($hours, $minutes, $seconds) = split(/:/, $time_str);
 +
    return $hours * 60 + $minutes + $seconds / 60;
 +
}
 +
</pre>  
  
 
+
===PHP===
Below is the same solution in pointfree style while also reusing some more standard functions.
+
<pre class="php">
 
+
  $array = explode($input);
<highlightsyntax language="haskell">
+
   $minutes = $array[1];
import Text.Regex
+
 
+
ttm :: String -> Double
+
ttm = sum . zipWith ($) [(*60), id, (/60)] . map read . (splitRegex . mkRegex) ":"
+
</highlightsyntax>
+
 
+
===Ruby===
+
<pre>
+
def time_to_minutes(time_str)
+
    split_str = time_str.split(":")
+
    return (split_str[0].to_f * 60) + (split_str[1].to_f) + (split_str[2].to_f / 60)
+
   rescue Exception => e
+
    # do any additional error handling here
+
    return -1
+
end
+
 
</pre>
 
</pre>
 +
 
===Python===
 
===Python===
<HIGHLIGHTSYNTAX language="python">
+
<pre class="python">
 
def time_to_minutes(time_str):
 
def time_to_minutes(time_str):
 
     try:
 
     try:
Line 64: Line 77:
 
         return -1
 
         return -1
 
     return int(hours)*60 + int(minutes) + int(seconds)/60.0
 
     return int(hours)*60 + int(minutes) + int(seconds)/60.0
</HIGHLIGHTSYNTAX>  
+
</pre>  
[[Category:Java]]
+
 
 +
===Ruby===
 +
<pre class="ruby">
 +
def time_to_minutes(time_str)
 +
  hours, minutes, seconds = time_str.split(":").map{|str| str.to_i}
 +
  hours * 60 + minutes + seconds / 60.0
 +
rescue          # do any additional error handling here
 +
  -1
 +
end
 +
</pre>
 +
 
 +
===Seed7===
 +
<pre>
 +
$ include "seed7_05.s7i";
 +
  include "float.s7i";
 +
 
 +
const func float: time_to_minutes (in string: time_str) is func
 +
  result
 +
    var float: result is 0.0;
 +
  local
 +
    var array string: split_str is 0 times "";
 +
  begin
 +
    split_str := split(time_str, ':');
 +
    result := float parse (split_str[1]) * 60.0 +
 +
              float parse (split_str[2]) +
 +
              float parse (split_str[3]) / 60.0;
 +
  end func;
 +
 
 +
const proc: main is func
 +
  begin
 +
    writeln(time_to_minutes("09:15:30"));
 +
  end func;
 +
</pre>
 +
 
 +
=== Tcl ===
 +
<pre class="tcl">
 +
proc time_to_minutes H {
 +
set h [expr {[clock format [clock scan $H] -format %H]*60}]
 +
set m [clock format [clock scan $H] -format %M]
 +
set s [expr {[clock format [clock scan $H] -format %M]/60.}]
 +
return [expr {$h+$m+$s}]
 +
}
 +
</pre>
 +
 
 +
===WinBatch===
 +
<pre>
 +
; Find the number of minutes in a HH:MM:SS time string
 +
 
 +
; Example 1: Using a user defined function.
 +
 
 +
#DefineFunction udfGetMinutes (strHms)
 +
Return ItemExtract (2, strHms, ":")
 +
#EndFunction
 +
 
 +
strHms1 = "19:54:12"
 +
strMinutes1 = udfGetMinutes (strHms1) ; strMinutes1 = "54"
 +
intMinutes1 = Int (strMinutes1)      ; Convert string to integer, version 1.
 +
 
 +
 
 +
; Example 2: Step by step code.
 +
strHms2 = "20:09:02"
 +
strMinutes2 = ItemExtract (2, strHms2, ":") ; strMinutes2 = "09"
 +
intMinutes2 = 0 + strMinutes2              ; Convert string to integer, version 2.
 +
 
 +
 
 +
; Example 3: One line of code.
 +
intMinutes3 = 0 + ItemExtract (2, "22:04:00", ":") ; intMinutes3 = 4
 +
 
 +
Exit
 +
; This code example was written by Detlev Dalitz.
 +
</pre>
 +
 
 +
===Zsh===
 +
<pre>
 +
function time_to_minutes
 +
{
 +
typeset -a time_str
 +
local -i hours minutes seconds
 +
 
 +
time_str=(${(s|:|)@})
 +
 
 +
hours=$time_str[1]
 +
minutes=$time_str[2]
 +
seconds=$time_str[3]
 +
 +
# Return only entire part! use 'print/echo' for complete output
 +
return $((hours * 60.0 + minutes + seconds / 60.0))
 +
}
 +
</pre>
 +
 
 +
[[Category:Time]]
 +
[[Category:Erlang]]
 
[[Category:Haskell]]
 
[[Category:Haskell]]
[[Category:Ruby]]
+
[[Category:Java]]
 +
[[Category:Perl]]
 +
[[Category:PHP]]
 
[[Category:Python]]
 
[[Category:Python]]
[[Category:Time]]
+
[[Category:Ruby]]
 +
[[Category:Seed7]]
 +
[[Category:Tcl]]
 +
[[Category:WinBatch]]
 +
[[Category:Zsh]]

Latest revision as of 19:14, 17 January 2012

Implementations[edit]

Erlang[edit]

time_to_minutes(Time_str) ->
    F = fun(X, T) -> 60*T + list_to_integer(X) end,
    lists:foldl(F, 0, string:tokens(Time_str, ":")) / 60.

Haskell[edit]

import Text.Regex

timeToMinutes :: String -> Double
timeToMinutes time_str
  = hours * 60 + minutes + seconds /60
    where [hours, minutes, seconds] = map read $ splitRegex (mkRegex ":") time_str


Below is the same solution in pointfree style while also reusing some more standard functions.

import Text.Regex
ttm :: String -> Double
ttm = sum . zipWith ($) [(*60), id, (/60)] . map read . (splitRegex . mkRegex) ":"

Java[edit]

	/**
	 * Turns a period of time into the number of minutes represented
         * (eg. 06:30:15 returns 360.25)
	 * @param hourFormat The string containing the hour format "HH:MM:SS"
	 * @return The number of minutes represented, or -1 if the date could not be processed.
	 */
	public static double parseTimeToMinutes(String hourFormat) {
		
		double minutes = 0;
		String[] split = hourFormat.split(":");
		
		try {
			
			minutes += Double.parseDouble(split[0])*60;
			minutes += Double.parseDouble(split[1]);
			minutes += Double.parseDouble(split[2])/60;
			return minutes;
		
		} catch (Exception e) {
			return -1;
		}
		
	}

Perl[edit]

sub time_to_minutes {
    my ($time_str) = @_;
    my ($hours, $minutes, $seconds) = split(/:/, $time_str);
    return $hours * 60 + $minutes + $seconds / 60;
}

PHP[edit]

  $array = explode($input);
  $minutes = $array[1];

Python[edit]

def time_to_minutes(time_str):
    try:
        hours, minutes, seconds = time_str.split(':')
    except ValueError:
        return -1
    return int(hours)*60 + int(minutes) + int(seconds)/60.0

Ruby[edit]

def time_to_minutes(time_str)
  hours, minutes, seconds = time_str.split(":").map{|str| str.to_i}
  hours * 60 + minutes + seconds / 60.0
rescue          # do any additional error handling here
  -1
end

Seed7[edit]

$ include "seed7_05.s7i";
  include "float.s7i";

const func float: time_to_minutes (in string: time_str) is func
  result
    var float: result is 0.0;
  local
    var array string: split_str is 0 times "";
  begin
    split_str := split(time_str, ':');
    result := float parse (split_str[1]) * 60.0 +
              float parse (split_str[2]) +
              float parse (split_str[3]) / 60.0;
  end func;

const proc: main is func
  begin
    writeln(time_to_minutes("09:15:30"));
  end func;

Tcl[edit]

proc time_to_minutes H {
 set h [expr {[clock format [clock scan $H] -format %H]*60}]
 set m [clock format [clock scan $H] -format %M]
 set s [expr {[clock format [clock scan $H] -format %M]/60.}]
 return [expr {$h+$m+$s}]
}

WinBatch[edit]

; Find the number of minutes in a HH:MM:SS time string

; Example 1: Using a user defined function.

#DefineFunction udfGetMinutes (strHms)
Return ItemExtract (2, strHms, ":")
#EndFunction

strHms1 = "19:54:12"
strMinutes1 = udfGetMinutes (strHms1) ; strMinutes1 = "54"
intMinutes1 = Int (strMinutes1)       ; Convert string to integer, version 1.


; Example 2: Step by step code.
strHms2 = "20:09:02"
strMinutes2 = ItemExtract (2, strHms2, ":") ; strMinutes2 = "09"
intMinutes2 = 0 + strMinutes2               ; Convert string to integer, version 2.


; Example 3: One line of code.
intMinutes3 = 0 + ItemExtract (2, "22:04:00", ":") ; intMinutes3 = 4

Exit
; This code example was written by Detlev Dalitz.

Zsh[edit]

function time_to_minutes
{	
	typeset -a time_str
	local -i hours minutes seconds

	time_str=(${(s|:|)@})

	hours=$time_str[1]
	minutes=$time_str[2]
	seconds=$time_str[3]
	
	# Return only entire part! use 'print/echo' for complete output
	return $((hours * 60.0 + minutes + seconds / 60.0))
}