Last active
August 29, 2015 14:06
-
-
Save lf94/486e40458f9c4dbc3c0b to your computer and use it in GitHub Desktop.
Calculate my working hours from a .hours file. Comparison of Python and Haskell.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Read in an hours file and print the total hours. | |
-- Hours file format: | |
-- Desc\tDate\tStartTime-EndTime | |
import System.Environment | |
import Data.List.Split | |
-- Return milliseconds | |
get_milliseconds :: [String] -> Double | |
get_milliseconds xs = hours + minutes | |
where | |
hours = (read (xs !! 0) :: Double) * 60 * 60 * 1000 | |
minutes = (read (xs !! 1) :: Double) * 60 * 1000 | |
-- Time format: HH:MM (24H) | |
time_difference :: String -> String -> Double | |
time_difference start end = (end_milli - start_milli) / 1000.0 / 60.0 / 60.0 | |
where | |
start_milli = get_milliseconds (splitOn ":" start) | |
end_milli = get_milliseconds (splitOn ":" end) | |
-- Recursively get every line, split it up, and figure out the total | |
calculate_total :: [String] -> Double | |
calculate_total [] = 0 | |
calculate_total (x:xs) = (time_difference start end) + (calculate_total xs) | |
where | |
military_time = splitOn " " ((splitOn "\t" x) !! 2) | |
start = military_time !! 0 | |
end = military_time !! 2 | |
-- Determine if a line is not a comment | |
not_comment :: String -> Bool | |
not_comment str | |
| str !! 0 == '/' && str !! 1 == '/' = False | |
| otherwise = True | |
main = do | |
args <- getArgs | |
content <- readFile (args !! 0) | |
let linesOfFiles = lines content | |
putStrLn $ show (calculate_total (filter not_comment linesOfFiles)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Read in an hours file and print the total hours. | |
# Hours file format: | |
# Desc\tDate\tStartTime-EndTime | |
import sys | |
# Time format: HH:MM (24H) | |
def time_difference(start, end): | |
end = end.split(":") | |
start = start.split(":") | |
end_milli = int(end[0])*60*60*1000 | |
end_milli += int(end[1])*60*1000 | |
start_milli = int(start[0])*60*60*1000 | |
start_milli += int(start[1])*60*1000 | |
diff = end_milli - start_milli | |
diff = diff / 1000 / 60 / 60 | |
return diff | |
with open(sys.argv[1]) as fh: | |
lines = fh.readlines() | |
total = 0.0 | |
for line in lines: | |
if(line[0] == '/' and line[1] == '/'): | |
continue | |
data = line.split("\t") | |
time = data[2].split(" ") | |
diff = time_difference(time[0], time[2]) #time[1] is a hyphen(-) | |
print(diff) | |
total += diff | |
print(total) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment