Convert String to DateTime Ruby - ruby-on-rails

I have a string "2015-11-01T10:00:00.00+08:00" extracted from json response. How can convert it to Time or DateTime?
I tried Time.new("2015-11-01T10:00:00.00+08:00") its returning 2015-01-01 00:00:00 +0530, clearly the date is changed here and time too.

require 'date'
▶ Date.parse "2015-11-01T10:00:00.00+08:00"
#⇒ #<Date: 2015-11-01 ((2457328j,0s,0n),+0s,2299161j)>
▶ DateTime.parse "2015-11-01T10:00:00.00+08:00"
#⇒ #<DateTime: 2015-11-01T10:00:00+08:00 ((2457328j,7200s,0n),+28800s,2299161j)>
or, even better:
▶ DateTime.iso8601 "2015-11-01T10:00:00.00+08:00"
#⇒ #<DateTime: 2015-11-01T10:00:00+08:00 ((2457328j,7200s,0n),+28800s,2299161j)>

With Rails, depending on your needs, it's recommended to use :
Time.zone.parse("2015-11-01T10:00:00.00+08:00")
#=> Sun, 01 Nov 2015 02:00:00 UTC +00:00
vs
Time.parse("2015-11-01T10:00:00.00+08:00")
#=> 2015-11-01 10:00:00 +0800
Time.zone.parse will return a Datetime expressed in your Timezone.

Time#parse:
require 'time'
Time.parse("2015-11-01T10:00:00.00+08:00")

If it's in rails, just do
"2015-11-01T10:00:00.00+08:00".to_time
worked in rails console.
If it's just plain ruby, go for #mudasobwa's solution

Related

Convert Active Support timezone original format into a string

I'm trying to convert Active Support timezone original format into a string. I want to store it in an array of characters then parse each needed data individually.
Time.zone = current_user.timezone
date_and_time = Time.zone.now
Now
date_and_time = Thu, 21 Apr 2016 20:58:04 PDT -07:00
Ruby method ( to_s ) does not convert it. I found other ways to convert it to but all of them will change the format to numbers only, I want the day to stay the same because I will store it in a variable then use it in a different method.
You can use .to_formatted_s(DATE_FORMAT) for this.
time = Time.now # => Thu Jan 18 06:10:17 CST 2007
time.to_formatted_s(:db) # => "2007-01-18 06:10:17"
time.to_formatted_s(:long) # => "January 18, 2007 06:10"
time.to_formatted_s(:long_ordinal) # => "January 18th, 2007 06:10"
time.to_formatted_s(:rfc822) # => "Thu, 18 Jan 2007 06:10:17 -0600"
time.to_formatted_s(:iso8601) # => "2007-01-18T06:10:17-06:00"
A list of all DATE_FORMATS and more information can be found here:
http://api.rubyonrails.org/classes/Time.html#method-i-to_formatted_s
You can try this
date_and_time.strftime("%a %d %b %Y")
Also You can check this guide, to get format you want
You should get what you want using this :
date_and_time.strftime("%a %d %b %Y %H:%M:%S UTC %:z")
Please see strftime Docs for more info
Explanation
Reason for hardcoding UTC is so that according to the docs
%z - Time zone as hour and minute offset from UTC
So i believe it should be UTC all the time.

Rails arel table query with Date and Time

I am having problems with a query because midnight conversion is not working as expected.
time = Date.today.midnight #=> Mon, 15 Jul 2013 00:00:00 BRT -03:00
time.class #=> ActiveSupport::TimeWithZone
condition = Task.arel_table[:scheduled_to].gt(time)
condition.to_sql #=> "`tasks`.`scheduled_to` > '2013-07-15 03:00:00'"
I was expecting the generated sql to be
`tasks`.`scheduled_to` > '2013-07-15 00:00:00'"
My Time zone is GMT -3. If i change the time zone so it matches GMT -5 the generated sql is
condition.to_sql #=> "`tasks`.`scheduled_to` > '2013-07-15 05:00:00'"
Rails 4.0.0
Ruby 2.0.0p247
Is there any way to ignore the timezone so the query behaves like expected?
Timezones are relative to UTC (0000), so you gotta remove it from your Date.
DateTime.now.midnight.utc #=> '2013-08-23 03:00:00 +0000'
Now, just get rid from the compensated hours.
DateTime.now.midnight.utc.change({:hour => 0, :min => 0}) #=> '2013-08-23 00:00:00 +0000'
Not sure if there is a cleaner way to do it, but it worked for me (Ruby 1.9.3p385).
Date.current or Date.today.midnight.utc Should have solved the problem.

Rails - A better way to parse the time with a timezone?

Time.parse returns a Time object that does not have a timezone. I would like to keep the timezone information. Is there a better way to do this then the following code?
def parse_with_timezone( string_input)
/(.*)([+-]\d\d):?(\d\d)$/.match( string_input) do |match|
tz = ActiveSupport::TimeZone[match[2].to_i.hours + match[3].to_i.minutes]
tz.parse( match[1])
end
end
The input is a string like this "2012-12-25T00:00:00+09:00". This function outputs a TimeWithZone object.
Were you looking for a specific timezone of the current local one?
# Current zone
1.9.3p194> Time.zone.parse('2012-12-25T00:00:00+09:00')
=> Mon, 24 Dec 2012 15:00:00 UTC +00:00
Console was set at UTC for above but will work for whatever you have configured
# Specific timezone
1.9.3p194> Time.find_zone('Wellington').parse('2012-12-25T00:00:00+09:00')
=> Tue, 25 Dec 2012 04:00:00 NZDT +13:00
I notice you're trying to pass +9 so as an example
1.9.3p194> Time.zone = 'Tokyo'
=> "Tokyo"
1.9.3p194> Time.zone.parse('2012-12-25T00:00:00+09:00')
=> Tue, 25 Dec 2012 00:00:00 JST +09:00
Gives you the right result.
What about the Rails Timezone API: http://api.rubyonrails.org/classes/ActiveSupport/TimeZone.html
I prefer to use Chronic for all my date/time parsing needs.

Ruby String to_time broken?

I would expect Time and to_time to be reflective.
UPDATE
This is the answer, if you add the parameter :local, it doesn't ignore the offset. I find it wierd that it ignores data (the offset) by default, though...
Time.zone.now.to_s.to_time(:local) returns 2012-03-20 14:39:03 +0100
/UPDATE
> Time.zone.now.to_s
=> "2012-03-20 12:50:05 +0100"
> Time.zone.now.to_s.to_time.to_s
=> "2012-03-20 12:50:05 UTC"
# PROBLEM:
# UTC is +0000, meaning that there is 1 hour diff in the above
> Time.zone.now
=> Tue, 20 Mar 2012 12:51:32 CET +01:00
> Time.zone.now.to_time
=> Tue, 20 Mar 2012 12:51:32 CET +01:00
> Time.zone.now.to_json
=> "\"2012-03-20T12:50:36+01:00\""
> Time.zone.now.to_json.to_time
=> 2012-03-20 12:50:36 UTC
I have this problem with JSON messages. Anyway, I wouldn't expect to_time to actually change the time. OK to translate to UTC, probably, but it should adjust time accordingly. Anybody know if this is a known issue or "by design"?
This method, String#to_time, defined in ActiveSupport takes one parameter, form, which can be either :utc (default) or :local. So by default, it always returns a Time in UTC.
To get a Time with timezone:
Time.zone.parse("2012-03-20T12:50:36+01:00")

Rails DateTime.now without Time

I need to use DateTime.now to grab the current date, and "strip off" the time.
For example, this shows what I don't want: DateTime.now => Sat, 19 Nov 2011 18:54:13 UTC +00:00
This shows what I do want: DateTime.now.some_operation => 2011-11-06 00:00:00 UTC
You can use one of the following:
DateTime.current.midnight
DateTime.current.beginning_of_day
DateTime.current.to_date
What you need is the function strftime:
Time.now.strftime("%Y-%d-%m %H:%M:%S %Z")
You can use just:
Date.current
What about Date.today.to_time?
If you're happy to require 'active_support/core_ext', then you can use
DateTime.now.midnight # => Sat, 19 Nov 2011 00:00:00 -0800
If you want today's date without the time, just use Date.today
Figured it out. This works:
DateTime.now.in_time_zone.midnight

Resources