Search code examples
timeformatgnuplot

How to read 12h (AM/PM) timeformat in gnuplot


I want to generate some nice graphs by using existing and auto generated data files. Unfortunately I cannot change the format of the files.

The lines contain a timestamp and a value. e.g.

July 06, 2014 at 12:46PM 84.6

In Gnuplot I can use the given format identifier to read the timestamp. But there is no possibility to separate between AM oder PM time.

set timefmt "%B %d, %Y at %H:%M"

Any suggestions on this problem?


Solution

  • The following awk script will format your file:

    awk '{time = $5; if(substr(time,6,2) == "PM" \
    && substr(time,1,2) < 12) add = 12; else add = 0; \
    $5 = substr(time,1,2)+add "" substr(time,3,3); \
    print $0}' data
    

    so that

    July 06, 2014 at 10:46AM 83.6
    July 06, 2014 at 12:46PM 84.6
    July 06, 2014 at 10:46PM 85.6
    

    will look like

    July 06, 2014 at 10:46 83.6
    July 06, 2014 at 12:46 84.6
    July 06, 2014 at 22:46 85.6
    

    within gnuplot you can do this dynamically when plotting your file. You can do (note I need to escape the "):

    set timefmt "%B %d, %Y at %H:%M"
    set xdata time
    set format x "%H:%M"
    plot "< awk '{time = $5; if(substr(time,6,2) == \"PM\" \
    && substr(time,1,2) < 12) add = 12; else add = 0; \
    $5 = substr(time,1,2)+add \"\" substr(time,3,3); \
    print $0}' data" u 1:6 w l
    

    enter image description here

    Note I am assuming that 00:15 is 00:15AM. If your output is 12:15AM then you need to modify the script accordingly. Something along these lines:

    if(substr(time,6,2) == "AM" && substr(time,1,2) == 12) add = -12