How to convert UTC time to configured timezone in rails 4 - ruby-on-rails

I received a time string from a server and I want to parse or convert that string to configured time zone (Tokyo timezone in my case):
Input: "2016-05-27T09:00:00.0000000"
Expected output: Fri, 27 May 2016 18:00:00 JST +09:00
I try to type like this: Time.zone.parse("2016-05-27T09:00:00.0000000") but it returns unexpected output: Fri, 27 May 2016 09:00:00 JST +09:00

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.

Related

Rails - Change TimeWithZone Timezone

I've been banging my head against this for a while and I can't seem to understand how rails timezones and in_time_zone works.
Here is some rails c output that I'd like to understand:
[26] VMM(bby - main - dev)> Time.zone.now
=> Wed, 14 Mar 2018 09:13:17 CDT -05:00
[27] VMM(bby - main - dev)> MyModel.first.started_at
=> Fri, 09 Mar 2018 09:17:00 CST -06:00
[28] VMM(bby - main - dev)> MyModel.first.started_at.in_time_zone(Time.zone)
=> Fri, 09 Mar 2018 09:17:00 CST -06:00
So:
From the first line, the Time.zone seems to be CDT -5.
From the second line, the started_at attribute seem to be CST -6
On the third line, my intention is to change that atribute to use CDT -5, so I'd expect an output of Fri, 09 Mar 2018 10:17:00 CDT -05:00
Why does this behave as it does instead of how I expect it to?
Thanks in advance!

how to change string time to rails date format

I have date as "Wed, 29 Jun 2016" and time in "11:35 PM" format
how can i create a date time object with it?
something like what Time.current does.
It's usually pretty simple if your date can be parsed:
DateTime.parse("Wed, 29 Jun 2016 11:35 PM")
# => Wed, 29 Jun 2016 23:35:00 +0000
You can then use that in any capacity you'd use any other date/time.
Why you don't use next:
Time.parse('Wed, 29 Jun 2016 11:35 PM')
=> 2016-06-29 23:35:00 +0300

Ruby how to get a date to the default format as it's stored in the db

My db is by default storing times as such:
Object.last.created_at
# => Fri, 03 Jul 2015 23:27:50 UTC +00:00
I looked at the strftime docs and I can build that myself, but it seems there must be an easy way to get a regular Date object to that format? Just wondering if there is...
to_datetime gets really close, but not exactly all the way there.
Date.today.to_datetime
# => Sat, 04 Jul 2015 00:00:00 +0000
Any other ideas?
Try this
Time.zone.now
#=> Sat, 04 Jul 2015 20:32:44 UTC +00:00
UPDATE
DateTime.now.in_time_zone
#=> Sat, 04 Jul 2015 20:43:57 UTC +00:00
Oh silly me... it's just
Date.today.in_time_zone
# => Mon, 06 Jul 2015 00:00:00 UTC +00:00

Wrong date when parsing date string from other time zone

I tried to parse this time string "21:58:06 Apr 29, 2015 PDT". What is the right way to do that?
first approach:
zone = Time.zone
Time.zone = "Pacific Time (US & Canada)"
payed_at = Time.parse params[:payment_date]
payed_at.in_time_zone(zone)
result:
Thu, 30 Apr 2015 10:21:30 CEST +02:00
second guess:
payed_at = DateTime.parse date
result:
Thu, 30 Apr 2015 21:58:29 +0000
the correct result would be
Thu, 30. April 2015, 06:58 Uhr
environment:
ruby '2.1.5'
gem 'rails', '~> 3.2.15'
Since you have rails specified in tags, here is an ActiveSupported solution:
tz = ActiveSupport::TimeZone['US/Pacific']
tz.parse("21:58:06 Apr 29, 2015 PDT").localtime
#⇒ 2015-05-01 06:58:29 +0200
Parse using strptime method
Eg : DateTime.strptime('December 09, 2011', '%B %d, %Y')
http://ruby-doc.org/stdlib-1.9.3/libdoc/date/rdoc/DateTime.html#method-c-_strptime

rails declare absolute time in a specific timezone

how can I get a variable which is holding always Today midnight in my timezone?
The hosting server is several hours behind me, both Time.now.midnight and Date.today are on yesterday date for good part of the day.
Thanks
Found the solution.
now=DateTime.now
=> Fri, 03 Feb 2012 21:57:21 EST -05:00
now.in_time_zone('London').midnight
=> Sat, 04 Feb 2012 00:00:00 GMT +00:00
now.in_time_zone('Hawaii').midnight
=> Fri, 03 Feb 2012 00:00:00 HST -10:00

Resources