gnuplot:如何将 12 小时时间格式转换为 24 小时时间格式? [英] gnuplot: how to convert 12h time format into 24h time format?

查看:25
本文介绍了gnuplot:如何将 12 小时时间格式转换为 24 小时时间格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将烦人的 12 小时时间格式(包括上午和下午)转换为 24 小时时间格式?

How can I convert the annoying 12h time format (with AM and PM) into the 24h time format?

我想避免像 this example 中的 awk 之类的外部程序.

I wanted to avoid external programs like awk as in this example.

由于 gnuplot 有一个用于am"和pm"的时间说明符 %p(检查 help time_specifiers),我认为这很容易.但是下面的代码并没有给出正确的结果,而是一条警告信息:

Since gnuplot has a time-specifier %p for "am" and "pm" (check help time_specifiers), I thought it would be easy. But the following code does not give the correct result, but a warning message:

警告:字符串中的时间格式错误

warning: Bad time format in string

也许,我使用 %p 不正确?

Maybe, I'm using %p incorrectly?

代码:

### change 12h time format to 24h format (does not give correct results)
reset session

$Data12 <<EOD
12/31/19  8:12:01 AM
12/31/19  8:12:02 PM
12/31/19 12:00:03 am
12/31/19 12:00:04 pm
EOD

myTimeFmt12 = "%m/%d/%y %H:%M:%S %p"
myTimeFmt24 = "%d.%m.%Y %H:%M:%S"

set table $Data24
    plot $Data12 u (strftime(myTimeFmt24,timecolumn(1,myTimeFmt12))) w table
unset table
print $Data24
### end of code

结果:

         "tbTime12to24.plt" line 15: warning: Bad time format in string
         "tbTime12to24.plt" line 15: warning: Bad time format in string
         "tbTime12to24.plt" line 15: warning: Bad time format in string
         "tbTime12to24.plt" line 15: warning: Bad time format in string
 31.12.2019 08:12:01    
 31.12.2019 08:12:02    
 31.12.2019 12:00:03    
 31.12.2019 12:00:04

推荐答案

虽然我希望有一个简短的gnuplot 集成"解决方案,但我通过定义自己的公式提出了一个有点冗长"的解决方案.

That's a somewhat "lengthy" solution I have come up with by defining my own formula, although I was hoping for a short "gnuplot-integrated" solution.

代码:

### change 12h time format to 24h format
reset session

$Data12 <<EOD
12/31/19  8:12:01 AM
12/31/19  8:12:02 PM
12/31/19 12:00:03 am
12/31/19 12:00:04 pm
EOD

myTimeFmt12a = "%m/%d/%y"
myTimeFmt12b = "%H:%M:%S"
myTimeFmt24 = "%d.%m.%Y %H:%M:%S"

# change 12h am/pm format to 24h format
myTime12to24(t,p) = t+12*3600*(floor(t/3600)<12 && (p eq "PM" || p eq "pm") ? 
                    1 : floor(t/3600)==12 && (p eq "AM" || p eq "am")  ? -1 : 0)

set table $Data24
    plot $Data12 u (strftime(myTimeFmt24,timecolumn(1,myTimeFmt12a) + 
         myTime12to24(timecolumn(2,myTimeFmt12b),strcol(3)))) w table
unset table
print $Data24
### end of code

结果:

 31.12.2019 08:12:01    
 31.12.2019 20:12:02    
 31.12.2019 00:00:03    
 31.12.2019 12:00:04    

这篇关于gnuplot:如何将 12 小时时间格式转换为 24 小时时间格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆