4.11.1 Time Intervals

Two functions are provided for measuring the time intervals elapsed between pairs of Julian Day numbers. These are useful when an experiment is started at some time $x$, and datapoints are to be labelled with the times elapsed since the beginning of the experiment. The time_diff() function returns the time interval, with physical dimensions of time, between two Julian Day numbers. This function is actually very simple, and is entirely equivalent to the algebraic expression (y-x)*unit(day). The following example, demonstrates its use to calculate the time elapsed between the traditional date for the foundation of Rome by Romulus and Remus in 753 BC and that of the deposition of the last Emperor of the Western Empire in AD 476:

pyxplot> x = time_julianday(-752,4,21,12,0,0)
pyxplot> y = time_julianday( 476,9, 4,12,0,0)
pyxplot> print time_diff(x,y)
3.8764483e+10 s
pyxplot> print time_diff(x,y)/unit(year)
1228.3723

The function time_diff_string() is similar, but returns a textual representation of the time interval, and is useful for producing textual axis labels representing the time elapsed since the beginning of an experiment. As with the time_string() function, it takes an optional third parameter which specifies the textual format in which the time interval should be represented. If no format is supplied, then the following verbose format is used:
"%Y years %d days %h hours %m minutes and %s seconds"
Table 4.4 lists the tokens which are substituted for various parts of the time interval. The following examples demonstrate the use of the function:

Token

Substitution value

%%

A literal % sign.

%d

The number of days elapsed, modulo 365.

%D

The number of days elapsed.

%h

The number of hours elapsed, modulo 24.

%H

The number of hours elapsed.

%m

The number of minutes elapsed, modulo 60.

%M

The number of minutes elapsed.

%s

The number of seconds elapsed, modulo 60.

%S

The number of seconds elapsed.

%Y

The number of years elapsed.

Table 4.4: Tokens which are substituted for various components of the time interval by the time_diff_string function.

pyxplot> x = time_julianday(-752,4,21,12,0,0)
pyxplot> y = time_julianday( 476,9, 4,12,0,0)
pyxplot> print time_diff_string(x,y)
1229 years 78 days 0 hours 0 minutes and 0 seconds
pyxplot> print time_diff_string(x,y,"$%Y\^{}$\backslash $mathrm{y}%d\^{}$\backslash $mathrm{d}$")
$1229^\mathrm {y}78^\mathrm {d}$

A plot of the rate of downloads from an Apache webserver.

In this example, we use PyXPlot’s facilities for handling dates and times to produce a plot of the rate of downloads from an Apache webserver based upon the download log which it stores in the file /var/log/apache2/access.log. This file contain a line of the following form for each page or file requested from the webserver:
127.0.0.1 - - [14/Apr/2010:16:05:36 +0100] "GET / HTTP/1.1" 200 2623 "-" "Mozilla/5.0 (X11; U; Linux x86_64; en-GB; rv:1.9.1.9) Gecko/20100402 Ubuntu/9.10 (karmic) Firefox/3.5.9"
However, PyXPlot’s default input filter for .log files (see Section 5.1) manipulates the dates in strings such as these into the form
127.0.0.1 - -  [ 14  4  2010 16 05 36 +0100 ]  "GET   HTTP 1.1" 200 2623 "-" "Mozilla 5.0 (X11; U; Linux x86_64; en-GB; rv 1.9.1.9) Gecko 20100402 Ubuntu 9.10 (karmic) Firefox 3.5.9"
such that the day, month, year, hour, minute and second components of the date are contained in the 5th to 10th white-space-separated columns respectively. In the script below, the time_­julianday() is then used to convert these components into Julian Days, and the histogram command (see Section 5.9) is used to sort each of the web accesses recorded in the Apache log file into hour-sized bins. Because this may be a time-consuming process for large log files on busy servers, we use the tabulate command (see Section 5.5) to store the data into a temporary datafile on disk before deciding how to plot it:

set output ’apache.dat’
histogram f() ’/var/log/apache2/access.log’ $\backslash $
using time_julianday($7,$6,$5,$8,$9,$10) binwidth 1/24
tabulate f(x) with format "%16f %16f"

Having stored our histogram in the file apache.dat, we now plot the resulting histogram, labelling the horizontal axis with the days of the week. The commands used to achieve this will be introduced in Chapter 1. Note that the major axis ticks along the horizontal axis are placed not at integer Julian Days, which fall at midday on each day, but at —$.5$, which falls at midnight on each day. Minor axis ticks are placed along the axis every quarter day, i.e. every six hours.

set xlabel ’Day’
set ylabel ’Rate of downloads per day’
set xtics 0.5, 1
set mxtics 0.5, 0.25
set xformat "%s"%(time_dayweekname(x,0)) rotate unit(30*deg)
plot "apache.dat" notitle with lines

The plot below shows the graph which results on a moderately busy webserver which hosts, among many other sites, the PyXPlot website:

\includegraphics[width=\textwidth ]{examples/eps/ex_apachelog}