Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
759 views
in Technique[技术] by (71.8m points)

time - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...