Ruby Converting Utc to User's Time Zone

Ruby converting UTC to user's time zone

I added a method in_timezone method in Time class as follows:

class Time
require 'tzinfo'
# tzstring e.g. 'America/Los_Angeles'
def in_timezone tzstring
tz = TZInfo::Timezone.get tzstring
p = tz.period_for_utc self
e = self + p.utc_offset
"#{e.strftime("%m/%d/%Y %I:%M %p")} #{p.zone_identifier}"
end
end

How to use it:

t = Time.parse("2013-11-01T21:19:00Z")  
t.in_timezone 'America/Los_Angeles'

Converting UTC to user preferred timezone on server side in Rails

After debugging for another hour, I figured out my mistake.

In the time_formats.rb file where I am trying to set the time zone to the user's preference, I used the following:

Time::DATE_FORMATS[:default] = in_time_zone("%I:%M %p")

The in_time_zone was causing the problem. I am already setting using the Application Controller to modify the time to the user's setting with current_user.timezone : nil. By changing my time_formats.rb to:

 Time::DATE_FORMATS[:default] = "%I:%M %p"

I am now showing the correct time based on the user settings, and I can push to Heroku successfully while letting the Application Controller do the work.

ruby on rails converting to users time zone

Does this help?

t = TimeZone["Eastern Time (US & Canada)"]
t.utc_to_local(DateTime.now) #gives the local time

Whoever posted before me deleted his answer (it was much better than my answer). If he brings his answer back I'll take mine down again.

How to convert UTC time to configured timezone in rails 4

If the parsed datetime is a UTC time, add the UTC timezone explicitly to it before parsing:

# this parses the time as local time:    
Time.zone.parse("2016-05-27T09:00:00.0000000")
# => Fri, 27 May 2016 09:00:00 JST +09:00

# this parses the time as UTC and converts to local time:
Time.zone.parse("2016-05-27T09:00:00.0000000Z")
# => Fri, 27 May 2016 18:00:00 JST +09:00

Note the "Z" appended to the datetime string, meaning that this is a datetime in UTC timezone.

Rails: convert UTC DateTime to another time zone

time.in_time_zone(time_zone)

Example:

zone = ActiveSupport::TimeZone.new("Central Time (US & Canada)")
Time.now.in_time_zone(zone)

or just

Time.now.in_time_zone("Central Time (US & Canada)")

You can find the names of the ActiveSupport time zones by doing:

ActiveSupport::TimeZone.all.map(&:name)
# or for just US
ActiveSupport::TimeZone.us_zones.map(&:name)

how can i convert utc time to local time timezone with +05:30

%w|+05:30 +10:00|.map do |time_zone|
hours, mins = time_zone.split(':').map(&:to_i).map(&:abs)
sign = time_zone[0] == '-' ? -1 : 1
Time.now.in_time_zone(ActiveSupport::TimeZone[(hours + 1.0 * mins / 60]) * sign)
end
#⇒ [Wed, 06 Jul 2016 12:54:10 IST +05:30, Wed, 06 Jul 2016 17:24:10 AEST +10:00]

Convert UTC to local time in Rails 3

Rails has its own names. See them with:

rake time:zones:us

You can also run rake time:zones:all for all time zones.
To see more zone-related rake tasks: rake -D time

So, to convert to EST, catering for DST automatically:

Time.now.in_time_zone("Eastern Time (US & Canada)")

Ruby on Rails retrieve datetime by converting it from UTC to user's time zone from database by querying date

If you store the datetime you can use it for that query using beginning_of_day and end_of_day methods on the time with timezone

date = Time.parse('Wed, 19 Nov 2014 19:23:59 UTC +00:00')
# => 2014-11-19 19:23:59 UTC
d = date.in_time_zone('Paris')
# => Wed, 19 Nov 2014 20:23:59 CET +01:00

User.where(created_at: (d.beginning_of_day..d.end_of_day)).count
# (46.5ms) SELECT COUNT(*) FROM "users" WHERE ("users"."created_at" BETWEEN '2014-11-18 23:00:00.000000' AND '2014-11-19 22:59:59.999999')


Related Topics



Leave a reply



Submit