#!/usr/bin/perl

use strict;

{
  package DateToEpoch;

  use strict;
  use DateTime;

  sub new {
    bless({}, $_[0]);
  }

  sub date_to_epoch {
    my($self,%date) = @_;

    if(not defined $self->{dt}) {
      $self->{dt} = DateTime->new(%date, time_zone => "UTC");
      $self->{epoch} = $self->{dt}->epoch();
      $self->{last_time} = \%date;
      return $self->{epoch};
    } 

    my(%set);
    foreach my $key (qw(year month day)) {
      if($date{$key} != $self->{last_time}{$key}) {
        $set{$key} = $date{$key};
        $self->{last_time}{$key} = $date{$key};
      }
    }
    if(%set) {
      $self->{dt}->set(%set);
      $self->{epoch} = $self->{dt}->epoch();
    }

    my $offset = ($date{hour} - $self->{last_time}{hour}) * 60*60 + 
      ($date{minute} - $self->{last_time}{minute}) * 60 + 
      ($date{second} - $self->{last_time}{second});
    return $self->{epoch} + $offset;
  }
}

my($dte) = new DateToEpoch();
my(@order) = qw(year month day hour minute second);
while(<>) {
  if(/^([0-9]{4})-([0-9]{2})-([0-9]{2}) ([0-9]{2}):([0-9]{2}):([0-9]{2}) (.*)/) {
    my $text = $7;
    my(%this_time);
    @this_time{@order} = ($1,$2,$3,$4,$5,$6);
    my $epoch = $dte->date_to_epoch(%this_time);
    $text =~ s/([0-9.]+e[+-][0-9]+)/sprintf("%0.9f",$1)/eg;
    if($epoch > time()+300) { # nothing in the future
      $epoch = 0;
    }
    print "$epoch $text\n";
  }
}
