time - gnuplot:如何将 12 小时时间格式转换为 24 小时时间格式?
问题描述
如何将烦人的 12 小时时间格式(包括上午和下午)转换为 24 小时时间格式?
我想避免像awk
这个例子中那样的外部程序。
由于 gnuplot 有一个%p
用于“am”和“pm”(检查help time_specifiers
)的时间说明符,我认为这很容易。但是下面的代码并没有给出正确的结果,而是一条警告信息:
警告:字符串中的错误时间格式
也许,我使用%p
不正确?
代码:
### 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 集成”解决方案。
代码:(编辑: gnuplot >=5.2.0 的简化解决方法,a.m.
包括p.m.
)
### change 12h time format to 24h format
reset session
$Data12 <<EOD
12/31/19 8:12:01 AM
12/31/19 8:12:02 a.m.
12/31/19 8:12:03 PM
12/31/19 8:12:04 p.m.
12/31/19 12:00:05 am
12/31/19 12:00:06 a.m.
12/31/19 12:00:07 pm
12/31/19 12:00:08 p.m.
EOD
myTimeFmt12 = "%m/%d/%y %H:%M:%S"
myTimeFmt24 = "%d.%m.%Y %H:%M:%S"
# change 12h am/pm format to 24h format
myTime12to24(colT,colM) = (_t = timecolumn(colT,myTimeFmt12), _p=strcol(colM), \
strftime(myTimeFmt24, _t + 12*3600*( \
((_p eq "PM" || _p eq "pm" || _p eq "p.m.") ? 1 : 0) + (floor(_t/3600)%12==0 ? -1 : 0))))
set table $Data24
plot $Data12 u (myTime12to24(1,3)) w table
unset table
print $Data24
### end of code
结果:
31.12.2019 08:12:01
31.12.2019 08:12:02
31.12.2019 20:12:03
31.12.2019 20:12:04
31.12.2019 00:00:05
31.12.2019 00:00:06
31.12.2019 12:00:07
31.12.2019 12:00:08
代码:(因为 gnuplot >=5.4.0,%p
也被实现为输入格式,但它并不打算涵盖a.m.
和p.m.
。我同意并非这种不幸的时间格式的所有变体都可以考虑在内。)
### change 12h time format to 24h format
reset session
$Data12 <<EOD
12/31/19 8:12:01 AM
12/31/19 8:12:02 a.m.
12/31/19 8:12:03 PM
12/31/19 8:12:04 p.m.
12/31/19 12:00:05 am
12/31/19 12:00:06 a.m.
12/31/19 12:00:07 pm
12/31/19 12:00:08 p.m.
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
结果:
31.12.2019 08:12:01
31.12.2019 08:12:02
31.12.2019 20:12:03
31.12.2019 08:12:04 # not correct
31.12.2019 00:00:05
31.12.2019 12:00:06 # not correct
31.12.2019 12:00:07
31.12.2019 12:00:08
推荐阅读
- javascript - 如何在 3s 视频中的特定帧处执行 DOM 操作?
- python - 无法将项目添加到新的 DynamoDB 表
- android - 想在不同的类文件中调用其他活动
- java - 添加 Firebase Admin 6.12.2 后 Netty 无法处理请求
- kotlin - 使用 listOf 与 arrayListOf
- python - 尝试在远程主机中安装 git 时出现 AnsibleAuthenticationFailure
- java - 演示文稿被取消,因为显示指标自创建以来已更改
- regex - 匹配正则表达式中确切数量的标记
- reactjs - 无法在 shopify react 应用程序中注册已卸载的网络挂钩
- reactjs - 如何隐藏 gatsby 主题的文本内容?