Is there any way in rails to convert Mountain Time (US & Canada) to EDT. And are there other names for other worldwide timezones?
To anwer your question:
2.0.0p353 :001 > now = DateTime.now.in_time_zone('Mountain Time (US & Canada)')
=> Mon, 06 Jan 2014 06:11:53 MST -07:00
2.0.0p353 :002 > now.in_time_zone('Eastern Time (US & Canada)')
=> Mon, 06 Jan 2014 08:11:53 EST -05:00
Please notice DST is being handled automatically:
2.0.0p353 :009 > now = (DateTime.now+6.months).in_time_zone('Mountain Time (US & Canada)')
=> Sun, 06 Jul 2014 07:14:30 MDT -06:00
2.0.0p353 :010 > now.in_time_zone('Eastern Time (US & Canada)') => Sun, 06 Jul 2014 09:14:30 EDT -04:00
And of course look at resource linked by #Micheal Moulsdale, you will find there all TimeZones names.
A good place to start would be the Active support Time Zone, and then on to the TZInfo gem
http://api.rubyonrails.org/classes/ActiveSupport/TimeZone.html
after you convert the date to the right zone, try date.zone() to get the short zone name.
zone = ActiveSupport::TimeZone.new(current_user.time_zone)
date.in_time_zone zone
date.zone
Related
I am trying to covert the UTC time "2018-04-02T14:30:00Z" to EST in pure Ruby. I noticed the following discrepancy. If I parse the time in UTC using Rails and then add the EST zone_offset I get a different time than using the "in_time_zone" helper. 9:30 versus 10:30.
2.2.4 :001 > t = Time.parse "2018-04-02T14:30:00Z"
=> 2018-04-02 14:30:00 UTC
2.2.4 :002 > t + Time.zone_offset("EST")
=> 2018-04-02 09:30:00 UTC
2.2.4 :003 > t.in_time_zone('Eastern Time (US & Canada)')
=> Mon, 02 Apr 2018 10:30:00 EDT -04:00
Rails does time zones really well, and you're going to have a difficult time replicating the results of ActiveSupport::TimeWithZone#in_time_zone without just using it.
For example, as you've pointed out:
>> t = Time.parse "2018-04-02T14:30:00Z"
>> t.in_time_zone('Eastern Time (US & Canada)')
Mon, 02 Apr 2018 10:30:00 EDT -04:00
But consider that you can likewise do:
>> t = Time.parse "2018-01-02T14:30:00Z"
>> t.in_time_zone('Eastern Time (US & Canada)')
Tue, 02 Jan 2018 09:30:00 EST -05:00
In other words, ActiveSupport is handling not only your time zones, but also your Standard/Daylight Savings challenges, all for free. Not even moment.js will do that (or if it will, I haven't figured out how).
Have you considered using require 'active_support', which would give you this functionality without using all of Rails?
you can try the localtime method, something like Time.parse("2018-04-02T14:30:00Z").localtime("-05:00").strftime("%m/%d/%Y %I:%M %p") to pretty print to get the time object remove the strftime
I believe that "Eastern Time (US & Canada)" is actually EDT and not EST. That would explain your hour difference.
irb(main):014:0> t = Time.parse "2018-04-02T14:30:00Z"
=> 2018-04-02 14:30:00 UTC
irb(main):015:0> t.in_time_zone('EST')
=> Mon, 02 Apr 2018 09:30:00 EST -05:00
irb(main):016:0> t.in_time_zone('Eastern Time (US & Canada)')
=> Mon, 02 Apr 2018 10:30:00 EDT -04:00
irb(main):017:0>
See also: https://time.is/EST
and https://time.is/EDT
Also, timezones are a huge pain and routinely break my brain.
I,m using following versions of Ruby and Rails
Ruby : 2.0.0p481 &
Rails : 4.1.1
Could not find any method in DateTime class to convert time to IST.
Tried DateTime.in_time_zone in IRB, could convert IST to EST but not vise versa.
2.0.0-p481 :003 > date = "Thu Jan 07 2016 16:20:00 GMT+0530 (India Standard Time)"
2.0.0-p481 :003 > date = date.in_time_zone('Eastern Time (US & Canada)')
2.0.0-p481 :003 > date
=> Thu, 07 Jan 2016 05:50:00 EST -05:00
Wanted to know how can I convert other tine zones to IST.
Please let me know if there is any way from which I can achive this.
ActiveSupport::TimeZone provide the names of all timezones. You can list out all timezones by doing
ActiveSupport::TimeZone.all.map(&:name)
For just US timezones
ActiveSupport::TimeZone.us_zones.map(&:name)
So change your time with the available timezones. Like this
irb> date = DateTime.now
=> Thu, 31 Dec 2015 22:15:59 +0530
# convert to EST
irb> date_est = date.in_time_zone("Eastern Time (US & Canada)")
=> Thu, 31 Dec 2015 11:45:59 EST -05:00
# convert to IST
irb> date_ist = date_est.in_time_zone("Chennai")
=> Thu, 31 Dec 2015 22:15:59 IST +05:30
Happy coding...
In my Rails app I do the following:
Time.zone.name #=> 'UTC'
Why when I do:
Time.parse('2014-12-19').end_of_day.to_datetime
I get: Fri, 19 Dec 2014 23:59:59 -0800
And when I do:
Time.use_zone('Pacific Time (US & Canada)') { Time.parse('2014-12-19').end_of_day.to_datetime }
I get the exact same thing: Fri, 19 Dec 2014 23:59:59 -0800
Why is the zone not being applied?
You need to use Time.zone.parse to make it use the proper timezone.
2.1.3 (main):0 > Time.zone.name
=> "UTC"
2.1.3 (main):0 > Time.zone.parse('2014-12-19').end_of_day.to_datetime
=> Fri, 19 Dec 2014 23:59:59 +0000
Are you looking for this
Time.use_zone('Pacific Time (US & Canada)') { Time.parse('2014-12-19').end_of_day.to_datetime.in_time_zone}
This would give you the correct time zone. For more information on this have a look at THIS SO QUESTION
You are probably on Windows, which has a bug in Rails and Rails will always return your local time zone. See How does Rails know my timezone?
You can also use
zone = ActiveSupport::TimeZone['Hawaii']
zone.at(Time.now)
=> Tue, 16 Jun 2015 09:22:53 HST -10:00
But I rather like Pamio's idea
Time.now.in_time_zone('Hawaii')
=> Tue, 16 Jun 2015 09:43:17 HST -10:00
In my rails4 app, I have date given in two strings, one gives timestamp, while other gives which timezone this is in:
a = "04/23/2014 04:00"
b = "Eastern Time (US & Canada)"
I want to convert this date to utc, so that I can save it in UTC
"2014-04-23 08:00:00 UTC"
What is the best way to do it?
Using the strptime method on the Rails DateTime class, you can parse a DateTime object from a string containing both the time and timezone (timezone is passed via the %z directive). From there, you can convert the time to UTC:
a = "04/23/2014 04:00"
b = "Eastern Time (US & Canada)"
datetime_with_tz = DateTime.strptime([a, b].join(' '), "%m/%d/%Y %H:%M %z")
#=> Wed, 23 Apr 2014 04:00:00 -0500
datetime_with_tz.utc
#=> Wed, 23 Apr 2014 09:00:00 +0000
Use the in_time_zone method of the DateTime class
Loading development environment (Rails 2.3.2)
>> now = DateTime.now.utc
=> Sun, 06 Sep 2009 22:27:45 +0000
>> now.in_time_zone('Eastern Time (US & Canada)')
=> Sun, 06 Sep 2009 18:27:45 EDT -04:00
>> quit
So for your particular example
a.in_time_zone('Eastern Time (US & Canada)')
I want to convert one time from one timezone to another.
Example:
Image that I have a first Time, like
t=....
puts t
=> Sun Aug 12 00:00:00 +0200 2012
I want to have the exact same hour, but in a different time zone.
I can do this:
mytimezone.local(t.year,t.month,t.day,t.hour,t.min)
=> Sun, 12 Aug 2012 00:00:00 BRT -03:00
So is there a way to accomplish the same things in a better way?
Have you tried in_time_zone method?
> t = Time.parse("Sun Aug 12 00:00:00 +0200 2012")
=> 2012-08-12 02:00:00 +0400
> t.in_time_zone('Eastern Time (US & Canada)')
=> Sat, 11 Aug 2012 18:00:00 EDT -04:00
> mytimezone = ActiveSupport::TimeZone.new('Eastern Time (US & Canada)')
=> (GMT-05:00) Eastern Time (US & Canada)
> t.in_time_zone(mytimezone)
=> Sat, 11 Aug 2012 18:00:00 EDT -04:00