Roland's homepage

My random knot in the Web

Storing and graphing time-based data

In this article we will look into the best way to store time-based data and use it with gnuplot.

Timezone

The first thing to realize is that we should specify a timezone when specifying dates and times (“datetimes”). To prevent calculations, I tend to use datetimes in UTC.

Format

Next on how to store those. Personally I am a great fan of text-based data, because it is human-readable. Expressing dates can be ambiguous. Therefore I prefer to stick to ISO 8601 combined date and time format.

Programming

Using e.g. Python, we can accomplish that as follows.

from datetime import datetime

now = datetime.utcnow().strftime('%FT%TZ')

This results in e.g. 2018-04-12T20:09:11Z.

To the best of my knowledge, all operating systems provide an API to retrieve the current UTC. Furthermore, strftime is borrowed from the standard C library and POSIX. So it is widely available in programming languages.

Gnuplot

The gnuplot program needs some instructions to handle data based on datetimes. Internally, it converts those to seconds. For data spanning multiple days, I tend to use the following.

set xdata time
set timefmt "%Y-%m-%dT%H:%M:%SZ"
set xtics 86400
set xtics format "%a %d %B"
set mxtics 12

The first two lines describe the above mentioned datetime format. The timefmt specification is a subset of that of strftime. The number 86400 represents a 24-hour day expressed in seconds.


←  Yes, please: #DeleteFacebook. But don’t leave it at that.