#!/usr/bin/perl
#
# relog.pl : perl script to take a stepping motor time log generated
#            by S. Stepanyan's tac_x.pl script and convert the unix-style
#            date/time values to the floating-point time representation
#            used by Labview, seconds since 00:00 Jan. 1, 1904.
#
# Usage:  relog.pl < [original_log_file] > [reformatted_log_file]
#
# Input:  (stdin) lines that look like the following.
#      Wed Apr 25 06:36:15 2007 5.02  11032 8.74177001733187 8.49941974047397
# Output: (stdout) lines that look like the following.
#      3259561175 5.02 11032 8.74177001733187 8.49941974047397
#

use Time::Local;
use Time::Epoch;

%months = ('Jan' => 0, 'Feb' => 1, 'Mar' => 2, 'Apr' => 3,
           'May' => 4, 'Jun' => 5, 'Jul' => 6, 'Aug' => 7,
           'Sep' => 8, 'Oct' => 9, 'Nov' => 10, 'Dec' => 11);
%days   = ('Sun' => 0, 'Mon' => 1, 'Tue' => 2, 'Wed' => 3,
           'Thu' => 4, 'Fri' => 5, 'Sat' => 6);

while (<>) {
   chop $_;
   my @fields = split(' ',$_);
   next if (@fields < 8);
   my ($hour,$min,$sec) = split(/:/,$fields[3]);
   my $time = timelocal($sec,$min,$hour,
                     $fields[2],$months{$fields[1]},$fields[4]);
   # special fix for DST mismatch, probably a Win2K issue
   if ($months{$fields[1]} == 3 && $fields[2] < 20) {
      $time -= 3600;
   }
   my $epochtime = perl2epoch($time,'macos','-0000');
   printf("%s %s %s %s %s\n",$epochtime,$fields[5],$fields[6],$fields[7],$fields[8]);
}

#gawk -F"[\055,\040,:,\011]" '{if (NF==12) {\
#                                 printf("%d   %s %s %s %s %s\n",\
#                                     mktime("2007 04 16 "$4+5" "$5" "$6)\
#                                    -mktime("1970 01 01 00 00 00")\
#                                    +2082841400,$8,$9,$10,$11,$12);\
#                              }\
#                             }'
