Ruby/Rails - Convert Unix Timestamp to HH:MM am/pm with Timezone - ruby-on-rails

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.

Related

Ruby on Rails convention for date time format from external source

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'

Ruby/Rails: Convert from one timezone to another (not necessarily UTC)

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

convert iso-8601 datetime to utc time rails

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/

Convert to CST Using Joda API

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.

What's the best way to detect date formats in user submitted data?

I'm reading csv data uploaded by users in my Ruby on Rails app. When a user specifies that a particular column has dates(or times), I want to be able to automatically detect the format. This means it can be in American or British formats (any of dd/mm/yy, mm/dd/yy, yyyy-mm-dd, 12 Feb 2010, etc etc)
I have tried parsedate in Ruby but it doesn't work for both American and British dates, unless you specify the format. Is there any way to really do this properly, or am I asking for too much? I don't mind calling a script in another language just for this one task. I'm wondering how it's handled in programs like Excel and Google docs.
Unless the application has a locality I don't know how you can determine this accurately.
What you do know however is that:
There are only 12 months.
Only years can be 4 digits long.
If it contains text then it must be the month.
You could write your own parser with these rules to work it out. It could however (without application locality) misinterpret 05/10/2010 as UK 5th Oct 2010 or US 10th May 2010.
there is little that a program can do to magically determine which type of short date format it is.
If you give a program a date like 09/06/08, it could mean either:
9th of June, 2008, or
6th of September, 2008, or perhaps even
8th of June, 2009.
When Ruby parses dates from string, it will use the default format providers to determine what format the date is in. See the Ruby DateTime class documentation for more info.
I think the best thing to do in your situation would be to try and arrange all of your records in to groups, where each group has one particular format of date. If you yourself can't manually determine the difference between the American and British dates by some criterion, unfortunately a program won't be able to either.
However... if each user is from a specific locale, and you can make the (rather large) assumption that every date they upload in a CSV conforms to their country's date format standards, you could make use of the internationalization API. It should be technically possible to grab that particular user's locale, and then load up the correct i18n data (with the appropriate date formatter), and parse the file using the formatter i18n provides you. Read the Rails Internationalization API guide to get an idea of how you can utilize the i18n API.
I know this is an old post but for archives' sakes I recommend using the Chronic gem for parsing dates/times in CSV imports.
Chronic.parse("8/15/2020") # => 2020-08-15 12:00:00 -0000
Chronic.parse("15/8/2020") # => 2020-08-15 12:00:00 -0000
Chronic.parse("8-15-2020") # => 2020-08-15 12:00:00 -0000
Chronic.parse("8-15-2020 3PM") # => 2020-08-15 15:00:00 -0000
FYI you'll want to tell Chronic to parse in the client's account timezone. Otherwise it will use the globally configured timezone (which is UTC in my example).

Resources