I have an ISO-8601 datetime stamp, and need to convert it into local time in GMT. What is the way to do it in Ruby on Rails? I have '1325233011', and need to convert it into local time in GMT standards.
I think what you're asking for is a locale time in GMT+5.
Given an ISO timestamp, 1325233011
When I convert this to a locale-based date/time
Time.at(1325233011) => '2011-12-30 03:16:51 -0500'
Take a look at the ruby-docs, http://www.ruby-doc.org/core-1.9.3/Time.html for more information. Ruby has robust Time and Date classes with many helper utilities. My machine is configured for GMT-5 so it returns the local time. It's easy to change the way timezone settings are interpreted in your program, but that's for another day. Hope this helps!
From Collegue's help got it
Time.at(1325233011).to_datetime
For Iso-8601:
Time.at(1325233011).to_datetime.iso8601
For verification of time correct conversion and comparision use this link
http://coderstoolbox.net/unixtimestamp/
Related
The date is coming from an imported xml that gives a 13 digit string (unix epoch time format).
The following snippet causes the resulting column value to be set to 1970-01-01
seconds=msg.time_stamp_long.to_i/1000
time=Time.at(seconds).utc.to_s
msg.time_stamp=time
msg.save
How to get the correct format for a DateTime column?
Time.at(1663681609392 / 1000).to_datetime
or if it's a string
Time.at("1663681609392".to_i / 1000).to_datetime
Be aware that unix time is epoch UTC time. Time and DateTime can get tricky in Ruby and in Rails. Make sure you are getting the time you expect. You may need to look into methods like .in_time_zone
To use your variables from the question:
msg.update(time_stamp: Time.at(msg.time_stamp_long / 1000))
is all you really need. As Max pointed out the DB adapter will handle the rest. But when you go to display or manipulate dates/times in Rails you might want to look into Date, Time, TimeWithZone, DateTime, etc. to understand the options that are out there and how they apply to your use case.
Given rails has a convention for most common tasks, is there a format of date/time data that ruby/rails ingests most easily, or which it 'prefers'?
I will have date/time data coming from an external source, and I can choose how it's formatted (but it not be easy to change later). I have researched and found two recommended formats:
A string of format YYYY-MM-DD HH:MM:SS
Unix epoch time (i.e. number of seconds since 1 Jan 1970)
Does rails deal more easily with one of these formats over the other (or is there another convention?)
Additional note: I can see from this talk that it's almost always best to store time in UTC, so I have that much figured out
I would agree that Ruby on Rails default to using the UTC time zone at least in the database.
I do not see a strong convention what time string format Rails prefers, but I would always choose ISO 8601: 'YYYY-MM-DDTHH:MM:SSZ'
Let's suppose I have the following data:
Unix timestamp: 1388935522
User Timezone: America/Los_Angeles
I currently have the following piece of code,
Time.at(1388935522).strftime("%I:%M %P")
which converts the Unix timestamp.
What is the best way to use the "User Timezone" (currently as a "string") to determine the offset (which would be -8:00) and display the time based upon the user's timezone (7:25 am).
Thanks very much!
You have basically two alternatives:
server-side approach
client-side approach using JavaScript
I explained both in this answer no more than a few days ago.
I know there a lot of questions along the lines of "how to convert from local to utc", but i think i want something slightly different. Apologies if i am repeating another question (i did search).
I want to convert from one DateTime with Timezone to another DateTime with Timezone, ie:
07/31/2013 18:14:46.676 America/New_York -> 08/01/2013 23:14:46.676 Europe/London
I have looked at TimeWithZone but it seems to assume you set a local timezone and then convert to/from UTC which is not what i want.
Thanks
Any one know how to convert UTC time to CST time using joda date time api ?
My code is something like this.
DateTimeZone zone = DateTimeZone.forID("CST");
DateTime mstTime = utcDateTime.toDateTime(zone);
Api says 'The datetime zone id CST is not recognised'
Short time-zone ids like "CST" are unclear and ambiguous, so they are not supported. Use a longer form, like "America/New_York".
Some of the three-letter time zones (EST and MST, for example) are included in the default time zone database used by Joda. Others (CST and PST, for example) are not. (See http://joda-time.sourceforge.net/timezones.html for more details.)
The time zone IDs supported by Joda can be obtained by calling org.joda.time.DateTimeZone.getAvailableIDs(), and that set does differ from those returned by java.util.TimeZone.getAvailableIDs().
You can use "CST6CDT" format of joda time.