Created
February 8, 2013 12:26
-
-
Save fabioadrianosoares/4738707 to your computer and use it in GitHub Desktop.
Processar arquivo com atividades no formato: "hh:mm dd/mm/yyyy - atividade", acumulando o tempo por "atividade".
This file contains hidden or 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
#! /urs/bin/perl | |
use strict; | |
use warnings; | |
use feature 'say'; | |
use DateTime; | |
open my $arquivo, '<', 'C:/Documents and Settings/fabio.soares/Desktop/Observações.txt' | |
or die 'Erro ao abrir o arquivo de dados: $!'; | |
my $dia = {data => ''}; | |
my $chaveanterior; | |
my $horaanterior; | |
while (my $linha = <$arquivo>) { | |
chomp $linha; | |
if ($linha =~ /^(\d+):(\d+) (\d+)\/(\d+)\/(\d+)/) { | |
my $chave = $'; #post match | |
my $hora = DateTime->new( | |
year => $5, | |
month => $4, | |
day => $3, | |
hour => $1, | |
minute => $2); | |
unless ($dia->{data} eq $hora->dmy('/')) { | |
if ($dia->{data}) { | |
say '-'x60; | |
say "Dia: $dia->{data}"; | |
for my $item (keys $dia->{ativ}) { | |
say sprintf('%02d:%02d', $dia->{ativ}->{$item}->hours(), $dia->{ativ}->{$item}->minutes) . $item; | |
} | |
#last; | |
} | |
$dia = {data => $hora->dmy('/'), ativ => {}}; | |
$chaveanterior = undef; | |
} | |
if ($horaanterior and $hora->epoch < $horaanterior->epoch) { | |
say 'Problema ao processar o dia ' . $hora; | |
die "Hora menor que anterior"; | |
} | |
if ($chaveanterior) { | |
$dia->{ativ}->{$chaveanterior} += $hora - $horaanterior; | |
} | |
$chaveanterior = $chave; | |
unless ($dia->{ativ}->{$chave}) { | |
$dia->{ativ}->{$chave} = DateTime::Duration->new(); | |
} | |
$horaanterior = $hora; | |
} | |
} | |
close $arquivo; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment