next up previous contents index
Next: Timestamps for Q Up: Test Data Creation Previous: Test Data Creation

Timestamps for R

 

#!/usr/local/bin/perl

# -----------------------------------------------
# week-long lifespan, consider weekday (periodic)
# -----------------------------------------------
# This is a PERL script to convert the output of the "last" command.
# The login times are converted to minutes (eg. Wed, 9:24 ->
# 2*1024 + 9*60 + 24 = 2572). The result gives intervals within the 
# lifespan 0..10079 (10080 = number of minutes within a week).


# an associative array to map weekday names to integers

%weekday = ("Mon",0,"Tue",1,"Wed",2,"Thu",3,"Fri",4,"Sat",5,"Sun",6);


# number of minutes per day

$one_day = 24 * 60;


# subsequently read lines of standard input ...

while (<>) {

   # split an input line using white spaces as separators

   @fields = split(/\s+/,$_);

   # convert hh:mm start and end times into integers

   ($hours,$minutes) = split(/:/,$fields[6]);
   $start = $weekday{$fields[3]} * $one_day + $hours * 60 + $minutes;
   ($hours,$minutes) = split(/:/,$fields[8]);
   $end   = $weekday{$fields[3]} * $one_day + $hours * 60 + $minutes;

   # deal with logins that ran over midnight

   if ($start > $end) {
      $end = $end + $one_day;
   }

   # write interval [$start,$end] to standard output;
   # if the [$start,$end] does not fall within the scope [0,10079]
   # then a "point" interval [$start,$start] is used

   if (($start <= $end) && ($end < 10080)) {
      print "[",$start,",",$end,"]\n";
   }
   else {
      print "[",$start,",",$start,"]\n";
   }
}



Thomas Zurek