How to get tzinfo from ActiveSupport::TimeZones.zones_map? - ruby-on-rails

I'm trying to retrieve an array of all time zones tzinfo but I only need the "America/Los_Angeles" part from each so I can validate timezones being received from an iOS mobile client. Currently running the following:
ActiveSupport::TimeZone.zones_map.values.collect { |z| z.tzinfo }
I get the class TZInfo::TimezoneProxy along with the info I need.

Try following code:
ActiveSupport::TimeZone::MAPPING.values

The zones in ActiveSupport::Timezones are limited to a subset of 146 "meaningful" zones, though they do not define by what critera they determine which zones are "meaningful".
If you use it to validate IANA time zone identifiers coming from another platform, you will certainly have false negatives, as there are over 500 of them.
Instead, use the TZinfo Gem directly, rather than Rail's strange mutated version of it. See also the section on Rails Time Zone Identifiers in the timezone tag wiki.

Found the instance method "identifier" in the documentation - http://www.rubydoc.info/gems/tzinfo/TZInfo/TimezoneProxy#_load-class_method
So all I needed was
ActiveSupport::TimeZone.zones_map.values.collect{|z| z.tzinfo.identifier }

Related

List the time zones for a Country

I am looking for the code that displays list of countries/regions in ruby on rails when selecting a specific country/region, it displays the time zone of it. For example when i choose “United Kingdom" as country/region then it should displays “(GMT +00:00)London" as Time Zone. My application runs on Rails 3.2.11. I am displaying all the countries using countries gem and all the time zones using ActiveSupport::TimeZone. I want to know if Ruby on Rails provide this functionality by default or if there is any other possible way to achieve this.
In Rails 5.2 this is simple: ActiveSupport::TimeZone.country_zones("US")
You can use TimeZone class. You can get all timezones list by:
zones_hash = ActiveSupport::TimeZone::MAPPING
And you can get time zone of any country as the following:
country_zone = ActiveSupport::TimeZone.new("Europe/Skopje")
country_zone.formatted_offset
The output: +01:00 for the above example.

How can i remove the milliseconds from date time in rails?

I have to compare the date in rails to get the values after that date I have send the date as "2013-03-04T06:26:25Z"but actually the record in the db contains date as follows 2013-03-04 06:26:25.817149 so when i check with the date it also returns that record but i want records after that date. how can i remove the milliseconds from the db? please help me.
I had a similar problem
Update your time object like this before sending it to the database :
time.change(:usec => 0)
Since ruby 1.9 you can use round
t = Time.now.round
t.usec # => 0
I was also having this problem, however when I was applying the change as indicated in #Intrepidd's answer, it wasn't affecting the microseconds of my time object.
Please note that (at least currently) the :usec key only works with the Time#change method, and not with the DateTime#change method.
The DateTime#change method ignores the keys that it doesn't accept, so you wouldn't be able to tell that your attempted change of the microseconds didn't work unless you inspected the object further (such as with DateTime#rfc3339(9)).
So before you attempt this change, make sure that you are working with a Time object, not a DateTime object.

Formatting Date according to user profile settings

Each user of our application can have different format for Date and Time. I understand the date format is dependent on user's language and possibly time zone.
I guess I can try to run something like Date::DATE_FORMATS[:default] = "%m/%d/%Y" in ApplicationController. In this case Date.today.to_s would return the property formatted date. Will it be thread safe?
Another option I am looking at is to store profiles into config/locales/en.yml with different keys like en_US, en_GB, en_... and store locale name into the user's profile. In this case I will need to use I18n.localize to format the date. Is it possible to fall back to en if there is no key found in specific en_US?
In the case of Date::DATE_FORMATS or TIME::DATE_FORMATS, you will need to take extra care to make their usage threadsafe, unfortunately. Shouldn't be too difficult to do, however.
AS for your second question, I believe you are concerned with locale fallbacks, and the i18n gem has support for locale fallbacks. This feature is easily enabled with:
I18n::Backend::Simple.include(I18n::Backend::Fallbacks)

why is the datetime in database different when I output it?

In my database on heroku, it shows contactemail.created_at = "2010-08-08 17:16:19"
However, when I use puts.contactemail.created_at I get something different. I get:
2010-08-08 10:11:13 -0700
I need to input that value through an API to another application, and I am pretty sure that the first format is what it wants. If it doesn't take that, it wants 08/08/10 17:16:19 -- in either case, I don't know how to format it properly.
This is in Ruby on Rails.
The display of date is based on your servers locale settings. If you are looking for the first format you could try
puts.contactemail.created_at.to_s(:db)
Have a look at strftime doc here http://ruby-doc.org/core/classes/Time.html#M000298 to get the second format
HTH
Ruby on rails can manage different time zones.
You can:
Make a direct sql consult (rails have some helpers)
Make that the two dates be equals, configuring config.active_record.default_timezone

How to return my current time zone in RoR?

When I use return the time that the record created, it show this :
2010-01-20 15:04:40 UTC
but I want the time in my specify time zone, for example, China. Is there any convenient method in RoR?
Configure your time zone in config/environment.rb to have Rails cast all timestamps to this time zone.
config.time_zone = 'Berlin'
As an alternative you can always use something like
Time.utc(2000).in_time_zone('Alaska')
See the documentation here.
Take a look at the TimeZone and TimeWithZone classes. They add time zone support. There's also been some additions to the Time and DateTime classes that also help deal with time zones. The documentation is given here: http://api.rubyonrails.org/classes/ActiveSupport/TimeWithZone.html.
There's also an excellent post here giving some extra details: http://ryandaigle.com/articles/2008/1/25/what-s-new-in-edge-rails-easier-timezones

Resources