From e87c4896487a6513840e7831a420bb0583ad7a8d Mon Sep 17 00:00:00 2001 From: Nigel McNie Date: Tue, 3 Jun 2008 23:00:24 +1200 Subject: [PATCH] Initial attempt at parsing clock times in the time field. Allows you to enter just one time, or time[dash]time. The former assumes the next line's time (or start time in the start[dash]stop form) is the stop time for it. Most likely will break when you try and wrap around the 24 hour boundary. Waa waa waa. --- tks.pl | 47 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) diff --git a/tks.pl b/tks.pl index 37db9c1..0bb1a7f 100755 --- a/tks.pl +++ b/tks.pl @@ -173,6 +173,7 @@ sub load_timesheet_file { # This hashref always has a 'line' key, containing the contents of the line. If # the line had valid timesheeting information on it too, that is returned # (using the 'wr', 'date', 'time' and 'comment' fields) +my $lastline; sub parse_line { my ($line) = @_; @@ -192,15 +193,59 @@ sub parse_line { if ( $line =~ m{\A ( \d+ | [a-zA-Z0-9_-]+ ) \s+ # Work request number OR alias - ( \d+ | \d* \. \d+ ) \s+ # Time in integer or decibal + ( \d\d? | \d* \. \d+ ) \s+ # Time in integer or decimal ( .* ) \z}xms ) { $result->{wr} = $1; $result->{time} = $2; $result->{comment} = $3; chomp $result->{comment}; + } + + if ( $line =~ m{\A + ( \d+ | [a-zA-Z0-9_-]+ ) \s+ # Work request number OR alias + ( \d\d?:?\d\d ( \- \d\d?:?\d\d )? ) \s+ # Time specified in 24 hour time + ( .* ) \z}xms ) { + mutter " ** Found a time-based line: $1 $2\n"; + $result->{wr} = $1; + $result->{comment} = $4; + chomp $result->{comment}; + + my $time = $2; + + # If we have a start-to-end formatted time + if ( $time =~ m/-/ ) { + my ($start, $end) = split(/-/, $time); + $start = convert_to_minutes($start); + $end = convert_to_minutes($end); + $result->{time} = ($end - $start) / 60; + + $lastline->{time} = ($start - $lastline->{time}) / 60 if $lastline->{needs_closing_time}; + $lastline->{needs_closing_time} = 0; + } + else { + $lastline->{time} = (convert_to_minutes($time) - $lastline->{time}) / 60 if $lastline->{needs_closing_time}; + + # We have a starting date only - need to wait for the next line + $result->{needs_closing_time} = 1; + $result->{time} = convert_to_minutes($time); + $lastline = $result; + } } return $result; } + +sub convert_to_minutes { + my ($time) = @_; + if ( $time =~ m/:/ ) { + my ($hours, $minutes) = split(/:/, $time); + return $hours * 60 + $minutes; + } + elsif ( length($time) == 4) { + # 24 hour time + return substr($time, 0, 2) * 60 + substr($time, 2); + } + die("Time in invalid format: $time"); +} -- 2.39.5