ruby - Parse hours from string with space delimiter -


i have file strings contain dates in heading, stuff such 2017-03-06 092328 - iphone - music show - street performance, while walking dog.m4a

i parse date our of string, because need print in podcast friendly format.

i'm able parse out date, reason time component refuses parsed out :)

date.strptime("2017-03-06 092328", "%y-%m-%d %h%m%s").strftime(%y-%m-%d %h%m%s) 

expected output:

"2017-03-06 092328" 

actual output

"2017-03-06 000000" 

your problem date (what surprise!) date, in, "not time, date".

require 'time' time.strptime("2017-03-06 092328", "%y-%m-%d %h%m%s").strftime("%y-%m-%d %h:%m:%s") #=> "2017-03-06 09:23:28" 

to fair :

  • the documentation of time#strptime mentions it's based on date#strptime
  • date._parse("2017-03-06 09:23:28") happily return {:hour=>9, :min=>23, :sec=>28, :year=>2017, :mon=>3, :mday=>6}

if find confusing time object has date , time date has date, use datetime.


Comments

Popular posts from this blog

c# - Update a combobox from a presenter (MVP) -

How to understand 2 main() functions after using uftrace to profile the C++ program? -

How to put a lock and transaction on table using spring 4 or above using jdbcTemplate and annotations like @Transactional? -