Strange output after date conversion in Ruby on Rails - ruby-on-rails

I am getting data from an API. The date attached to each object comes in this format:
Timestamp is the date and time of an
event in UTC time. This is expressed
as a specific number of milliseconds
since the standard base "epoch" of:
January 1, 1970, 00:00:00 GMT
I am trying to convert this into a different date format in Ruby.
CODE
objects.each do |obj|
p "object"
p obj.created
p Time.at(obj.created)
end
OUTPUT
"object"
1308886130000
43446-12-14 10:33:20 +1000
"object"
1308886104000
43446-12-14 03:20:00 +1000
"object"
1308801345000
43444-04-07 03:10:00 +1000
The years are obviously incorrect. What am I doing wrong?
I am using Ruby 1.9.2 and Rails 3.0.1

The Time.at function expects time since the epoch in seconds. You're giving it milliseconds, so the calculation is way off.
Divide that timestamp by 1000.

Related

Convert date time in another timezone with offset value in ruby

I am fairly new to ruby. I have a date value say "2014-12-15T19:56:59Z" and an offset value say "-06:00". How can I convert the given date time in the timezone given by offset value.
Using Time::parse and Time#localtime:
require 'time'
t = Time.parse('2014-12-15T19:56:59Z')
#=> 2014-12-15 19:56:59 UTC
t.localtime('-06:00')
#=> 2014-12-15 13:56:59 -0600

How to use strptime to convert timestamp string with milliseconds to time object

I need to use strptime to convert a timestamp string with milliseconds to a Time object.
A work around is using parse:
t= Time.parse('29 Sep 2013 12:25:00.367')
=> 2013-09-29 12:25:00 -0400
But it is very important for my code to use strptime, because I want to be able to pass multiple types of format including: "HH:MM", "HH", etc. through the function.
I can do it with nanoseconds like this:
Time.strptime("12:34:56:789434", "%H:%M:%S:%N")
=> 2016-03-16 12:34:56 +0100
I want something like this:
Time.strptime("12:34:56:789", "%H:%M:%S:%[insert magic letter that represent milliseconds]")
My thought is that there must be a way to do it with milliseconds as well.
Is it possible and how?
Try %L.
Refer to Ruby's DateTime documentation.
If you can update to Ruby 1.9.3, it supports this using %3N:
http://ruby-doc.org/core-1.9.3/Time.html#method-i-strftime

Timezone in numeric values?

From this API, it's asking for a timezone in a negative of positive value. What does this mean and how would I produce it in Rails 4?
tz Time zone as negative of positive values. Ex: 6.5
From looking through the Ruby Time API, I can't seem to make sense of where to look exactly. Any help would be awesome!
What you need is gmt_offset method, it returns difference between GMT and selected time zone (time object) in seconds. Just divide it by 3600 and there you go
Example
t = Time.gm(2000,1,1,20,15,1) #=> 2000-01-01 20:15:01 UTC
t.gmt_offset #=> 0
l = t.getlocal #=> 2000-01-01 14:15:01 -0600
l.gmt_offset #=> -21600

How to convert Evernote API Timestamps to Postgresql Timestamps

I'm accessing the Evernote API via the evernote gem for ruby on rails, and I'm storing the objects (notebooks, tags, notes, etc.) in a Postgresql database.
Evernote returns timestamps that look like this:
1344141917000
1344141967000
1344138641000
The evernote api documentation says this is the number of milliseconds that have passed since the base time of January 1, 1970, 00:00:00 GMT
I've conducted the following exercise in the rails console in an attempt to reconstruct the date.
evernote_timestamp_base = Time.gm(1970,01,01,00,00,00)
=> 1970-01-01 00:00:00 UTC
evernote_timestamp_base + 1344138641000
=> 44564-01-22 04:43:20 UTC
Definitely not right. But chopping those last three zeros off yields the right date:
evernote_timestamp_base + 1344138641
=> 2012-08-05 03:50:41 UTC
Am I missing something here? What's the deal with those last three zeros? Will I have to parse and chop the evernote timstamp values and then add them to the 1970 base, or is there an easier way?
Also, what's the best Postgresql data type for storing these values?
Thanks in advance for your help.
To do this in Ruby, use Time.at. You'll need to divide by 1000 since Evernote timestamps are in millseconds and Ruby/Unix timestamps are in seconds.
createdNote = noteStore.createNote(authToken, note)
createTime = Time.at(createdNote.created / 1000);
puts "Note was created at #{createTime}"
In postgresql you could use a timestamp [with|without] time zone as the type for the column.
The unix timestamp is defined as the number of seconds since January 1, 1970, 00:00:00 GMT which is what Ruby and many other systems use and or support. PostgreSQL can do the conversion for you to. Just pass the value in seconds to the to_timestamp function.
SELECT to_timestamp(1344138641000/1000.0)
To convert it back use
SELECT EXTRACT(EPOCH FROM col)

Date/Time conversion problems from Rails to Flex?

I am getting the following error:
TypeError: Error #1034: Type Coercion failed: cannot convert "2010-01-02 23:28:17 UTC" to Date.
I am using WebORB to transfer data between Rails and Flex. The data coming from Rails is of type: 'ActiveSupport::TimeWithZone'. I am trying to assign it to a Flex 'Date' data type.
What am I missing?
From the documentation: http://www.adobe.com/livedocs/flex/3/langref/Date.html#Date()
If you pass a string to the Date class constructor, the date can be in a variety of formats, but must at least include the month, date, and year. For example, Feb 1 2005 is valid, but Feb 2005 is not. The following list indicates some of the valid formats:
Day Month Date Hours:Minutes:Seconds GMT Year (for instance, "Tue Feb 1 00:00:00 GMT-0800 2005", which matches toString())
I think in Rails, what you need to do is calling strftime to format the date output that's going to be sent to Flex
time_with_zone.strftime("%a %b %d %H:%M:%S %z %Y") # => "Sun Jan 03 20:58:16 +0700 2010"
Thanks for the help but, oddly, that wasn't it. Your help, Sikachu, did put me on the right track. I couldn't simply assign the returned result--I had to feed it into the constructor. So, instead of doing this, which didn't work:
var flexDate:Date = server_result.date;
I did this, which works:
var flexDate:Date = new Date(server_result.date);

Resources