Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Getting avg start time

by Earindil (Beadle)
on Aug 09, 2007 at 15:23 UTC ( [id://631586]=perlquestion: print w/replies, xml ) Need Help??

Earindil has asked for the wisdom of the Perl Monks concerning the following question:

I have some processes that depend on other processes before starting. I'm trying to come up with a way to compute the "avg" start time. The problem is, sometimes, the start time will cross midnight. At first, I thought I could just get the # of seconds since the epoch, avg them, and then divide. But, as you can see in this case, that just doesn't work. Ideas?
#!/bin/perl use Date::Calc qw(Date_to_Time); use POSIX; $time1 = Date_to_Time(2007,8,8,23,00,0); $time2 = Date_to_Time(2007,8,9,03,00,0); $time3 = Date_to_Time(2007,8,10,01,00,0); print "$time1\t$time2\t$time3\n"; $avg = sprintf "%0.0f", (($time1+$time2+$time3)/3); print "$avg\n"; print strftime "%H:%M:%S\n", gmtime($avg);
OUTPUT:
1186614000 1186628400 1186707600
1186650000
09:00:00 ---------------------------------- This seems to do it. I can ignore the actual mm/dd/yy that the job ran on and just pay attention to what time of day it started. If it started before 5pm, then I know it crossed midnight since all jobs kick off at 5pm.
$count = 0; $starthour = 17; foreach $time (@times) { $count ++; ($hour,$minute,$second) = split(/:/,$time`); if ($hour >= $starthour) { $time += Date_to_Time(1970,1,1,$hour,$minute,$second); } else { $time += Date_to_Time(1970,1,2,$hour,$minute,$second); } } $timeavg = $time/$count; print strftime '%H:%M:%S', gmtime($timeavg);

Replies are listed 'Best First'.
Re: Getting avg start time
by ikegami (Patriarch) on Aug 09, 2007 at 16:39 UTC

    Normally, the meaningful information is *run length* of a program. Using that information, you can predict the end time of a program given it's start time. (The average amount of time elapsed since epoch is not very useful.)

    Your problem definition is so vague, it's very hard to understand what you want. For starters, what are $time1, $time2 and $time3? Am I correct in the following restatment of your problem:

    You have a series program that runs sequentially every night. You wish to know at what time the second program in the series is likely to start on a particular night.

    (Update: The silent update to the OP confirms this. )

    If so, the calculation is:

    my $day1_prog1_start = Date_to_Time(2007, 8, 8, 17, 0, 0); my $day1_prog2_start = Date_to_Time(2007, 8, 8, 23, 0, 0); my $day2_prog1_start = Date_to_Time(2007, 8, 9, 17, 0, 0); my $day2_prog2_start = Date_to_Time(2007, 8, 10, 3, 0, 0); my $day3_prog1_start = Date_to_Time(2007, 8, 10, 17, 0, 0); my $day3_prog2_start = Date_to_Time(2007, 8, 11, 1, 0, 0); my $day1_prog1_len = $day1_prog2_start - $day1_prog1_start; my $day2_prog1_len = $day2_prog2_start - $day2_prog1_start; my $day3_prog1_len = $day3_prog2_start - $day3_prog1_start; my $avg_prog1_len = $day1_prog1_len / 3 + $day2_prog1_len / 3 + $day3_prog1_len / 3; print("Prog2 usually starts $avg_prog1_len seconds after Prog1 start.\ +n"); my ($year, $mon, $day) = Today(1); my ($hour, $min, $sec) = (Gmtime($day1_prog1_start))[3,4,5]; my $next_prog1_start = Date_to_Time($year,$mon,$day,$hour,$min,$sec); my $next_prog2_start = $next_prog1_start + $avg_prog1_len; my $next_prog1_start_str = strftime('%Y/%m/%d %H:%M:%S', gmtime($next_prog1_start)); my $next_prog2_start_str = strftime('%Y/%m/%d %H:%M:%S', gmtime($next_prog2_start)); print("If Prog1 starts at $next_prog1_start_str,\n"); print("Prog2 will likely start at $next_prog2_start_str.\n");
    Prog2 usually starts 10800 seconds after Prog1 start. If Prog1 starts at 2007/08/09 22:00:00, Prog2 will likely start at 2007/08/10 01:00:00.

    Arrays and loops can and should be used, of course.

    Update: Added example times and resulting output.

Re: Getting avg start time
by grep (Monsignor) on Aug 09, 2007 at 16:02 UTC
    Get the time - but set the date to the same date every time to get a comparison.

    Remember you want to compare times not dates and times so yoiu need to take the date out of the equation.

    #!/bin/perl ## UNTESTED use strict; use warnings; use Date::Calc qw(Date_to_Time); use POSIX; my $time1 = Date_to_Time(2007,1,1,23,00,0); my $time2 = Date_to_Time(2007,1,1,03,00,0); my $time3 = Date_to_Time(2007,1,1,01,00,0); print "$time1\t$time2\t$time3\n"; my $avg = sprintf "%0.0f", (($time1+$time2+$time3)/3); print "$avg\n"; print strftime "%H:%M:%S\n", gmtime($avg);
      That was actually my first attempt, but it doesn't work.
      $time1 = Date_to_Time(2007,1,1,23,00,0); $time2 = Date_to_Time(2007,1,1,03,00,0); $time3 = Date_to_Time(2007,1,1,01,00,0);
      1167692400      1167620400      1167613200
      1167642000
      09:00:00
      
      Actually came up with a way to do this, but, would love to hear other/better suggestions. All of my jobs start at 5pm. So, what I can do is check and see if the start time of a particular job is < 5pm. If so, I know it started the next day, then I would do this:
      $time = Date_to_Time(2007,1,2,02,00,0);
      If it is after 5pm, then it's the same day so:
      $time = Date_to_Time(2007,1,1,23,00,0);
Re: Getting avg start time
by FunkyMonk (Chancellor) on Aug 09, 2007 at 16:32 UTC
    sometimes, the start time will cross midnight
    But the dates you've used are on three different days! change the last date to the 9th and you get a very reasonable average of 1:00.
    use Date::Calc qw(Date_to_Time); use POSIX; my $time1 = Date_to_Time(2007,8,8,23,00,0); my $time2 = Date_to_Time(2007,8,9,03,00,0); my $time3 = Date_to_Time(2007,8,9,01,00,0); print "$time1\t$time2\t$time3\n"; my $avg = sprintf "%0.0f", (($time1+$time2+$time3)/3); print "$avg\n"; print strftime "%H:%M:%S\n", gmtime($avg); __END__ 1186614000 1186628400 1186621200 1186621200 01:00:00

Re: Getting avg start time
by NetWallah (Canon) on Aug 09, 2007 at 16:24 UTC
    The problem is that the time interval between $time1 and $time3 exceeds 24 hours.

    The average calculation is CORRECT. Unless your job runs over 24 hours, I think your $time3 value should be 2007-08-09 , instead of 2007-08-10.

    With that value, the average time Shows 1:00:00.

         "An undefined problem has an infinite number of solutions." - Robert A. Humphrey         "If you're not part of the solution, you're part of the precipitate." - Henry J. Tillman

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://631586]
Approved by ikegami
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2024-04-23 23:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found