result = connect.query("SELECT * FROM data WHERE event_type = 'ALARM_OPENED' AND severity = '2'")
equipments = result.map { |print_this| [print_this['sourcetime'], print_this['description']] }
p equipments
MySQL 工作台中 sourcetime
的数据类型设置为 TIME
我得到的结果是2000-01-01 12:00:04 +0100
我只想获取时间12:00:04
并删除日期和+0100
最佳答案
要实现这一目标,需要两个步骤:
-
Time.parse
从数据库返回到Time
实例中的字符串。 - 使用
Time#strftime
将Time
实例格式化为所需的输出格式。
要仅显示时间的小时、分钟和秒(不带日期和时区信息),请使用 '%H:%M:%S'
指令(请参阅 docs 了解更多信息)更多示例)。
require 'time' # `Time.parse` is not Ruby Core but from the Standard Library
equipments = result.map do |record|
[
Time.parse(record['sourcetime']).strftime('%H:%M:%S'),
record['description']
]
end
关于mysql - MySQL 中时间的格式数据类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49399821/