cristodagama
cristodagama

Reputation: 57

Converting Unix Timestamp to standard time in Ruby but getting strange error in console

Basically I'm just trying to sift through a csv with unix timestamps, and not sure what is going wrong as I looked at other solutions on this site.

Error

csv_parsing.rb:13:in `at': can't convert String into an exact number (TypeError)

My unix_timestamps_array variable is an array of strings composed of unix timestamps for example "1237178109" but dont feel like attaching the csv. I can confirm that before ln 7 I am getting the array of unix timestamps in the terminal.

require 'csv'
require 'date'

csv = CSV.read('sample_data.csv', headers: true)

unix_time_str_array = csv['created_at']

unix_time_int_array = unix_time_str_array.map {|e| e.to_i }

converted_time = unix_time_int_array.each {|timestamp| Time.at(timestamp)}

p converted_time

Upvotes: 0

Views: 1351

Answers (1)

Andrey Deineko
Andrey Deineko

Reputation: 52357

You need to pass a Numeric instance, not string:

Time.at("1237178109")
#=> TypeError: can't convert String into an exact number

But

Time.at(1237178109)
#=> 2009-03-16 06:35:09 +0200

Upvotes: 2

Related Questions