Search code examples
gnuplottime-format

gnuplot: how to correctly interpret negative times?


I have some issue with negative times in gnuplot.

Basically, I would like to write a negative time, e.g. as -00:01:00, but gnuplot is not interpreting it as -60 seconds, but as +60 seconds. I can somehow understand why: because -00 hours is equal to +00 hours and then 01 minutes are counted positive.

Did I overlook something? Is there maybe an easy workaround?

More examples are given below. Let's convert some times in the format %H:%M:%S (actually %tH:%tM:%tS). I'm fine with all lines, except line 6. Line 7 will be interpreted as %tH:%tM without seconds that's why it is -3660 seconds.

Code:

### negative times
reset session

$Data <<EOD
1   01:00:00
2   01:00:01
3  -01:00:00
4  -01:00:01
5   00:01:01
6  -00:01:01
7     -01:01
8   00:-01:-01
9   00:-01:01
EOD

myTimeFmt = "%tH:%tM:%tS"

set table $Test
    plot $Data u 1:(strcol(2)):(timecolumn(2,myTimeFmt)) w table
unset table
print $Test
### end of code

Result:

 1      01:00:00         3600
 2      01:00:01         3601
 3     -01:00:00        -3600
 4     -01:00:01        -3601
 5      00:01:01           61
 6     -00:01:01           61
 7        -01:01        -3660
 8      00:-01:-01        -61
 9      00:-01:01         -61

Solution

  • The following is an attempt to include the possibility of entering negative times starting with -00 hours %tH:%tM:%tS (or minutes %tM:%tS).

    It will handle cases 4 and 6 differently than gnuplot currently will do. The workaround will handle cases which have negative or -00 hours and additionally negative minutes or seconds (cases 7-14 and 16-17) the same way as gnuplot will do. Well, the latter are strange formats anyway.

    Code:

    ### workaround for handling negative times
    reset session
    
    $Data <<EOD
    1    01:00:00
    2   -01:00:00
    3    00:01:00
    4   -00:01:00
    5    00:00:01
    6   -00:00:01
    7    00:00:-01
    8    00:-00:01
    9    00:-00:-01
    10   00:-01:01
    11   00:-01:-01
    12  -00:-01:-01
    13  -00:-01:01
    14  -00:-01:-01
    15  -01:01:01
    16  -01:-01:-01
    17   01:-01:-01
    EOD
    
    myTimeFmt = "%tH:%tM:%tS"
    
    myTimeSigned(fmt,s) = s[1:1] eq '-' && strptime("%tH",s)==0 && strptime(fmt,s)>0 ? \
                          -strptime(fmt,s[2:]) : strptime(fmt,s)
    myTime(n,fmt) = myTimeSigned(fmt,strcol(n))
    
    set table $Test
        plot $Data u 1:(strcol(2)):(timecolumn(2,myTimeFmt)):(myTime(2,myTimeFmt)) w table
    unset table
    print $Test
    ### end of code
    

    Result:

             input       gnuplot  workaround
    
     1      01:00:00       3600    3600
     2     -01:00:00      -3600   -3600
     3      00:01:00         60      60
     4     -00:01:00         60     -60   # different
     5      00:00:01          1       1
     6     -00:00:01          1      -1   # different
     7      00:00:-01        -1      -1
     8      00:-00:01         1       1
     9      00:-00:-01       -1      -1
     10     00:-01:01       -61     -61
     11     00:-01:-01      -61     -61
     12    -00:-01:-01      -61     -61
     13    -00:-01:01       -61     -61
     14    -00:-01:-01      -61     -61
     15    -01:01:01      -3661   -3661
     16    -01:-01:-01    -3661   -3661
     17     01:-01:-01     3539    3539