Calculating Elapsed Time Using Perl
Let’s say we need to figure out how long it has been since something happened and we either don’t know how (to use) or what the built-in functions are to do this.
Or perhaps you’d like to just understand how to do it without relying on those built-in functions.Let’s say we need to figure out how long it has been since something happened and we either don’t know how (to use) or what the built-in functions are to do this.
Or perhaps you’d like to just understand how to do it without relying on those built-in functions. It seems like something like this should be really easy to do (in some languages it is), but it is difficult finding tutorials which will explain how to accomplish this, so I have decided to show you one technique that you can use in your programs to accomplish the comparison of time. You only need to know how to get a timestamp and the rest we can build raw (that is my way of saying without using external libraries or built-in functions or modules). I am going to use Perl CGI as an example, but this same technique can be used in all languages (although of course the base function names will likely change).
This is a timestamp of the number of seconds since January 1, 1\’70, which is sometimes also referred to as the computer epoch. How do we know this besides me telling you? Use the table below as a guide:
seconds 1
minute 60
hour 3600
day 86400
week 604800
Now, the next time you see a timestamp you can say, “ahh, that is the number of seconds since January 1, 1\’70, the computer epoch!” (and people will probably look at you like you are on something). Seriously, as a programmer, though, you can tap into this clock and effectively use a stopwatch to determine the passage of time. That is what we are really going to focus on: stopping the time to mark an event. Consider the code below:
#!/usr/bin/perl
$past = \’57138777;
$present = time;
$seconds = int($present – $past);
print “Content-type: text/htmlnn”;
print “Seconds elapsed: $seconds”;