<% date = Time.now.beginning_of_day %>
<%= date %> prints 2016-06-01 00:00:00 +0600
<% schedule = Schedule.where(:date_ => date).first %>
<%= date %> prints 2016-05-31 18:00:00 UTC
2016-06-01 00:00:00 +0600
2016-05-31 18:00:00 UTC
Using mongoid
date_ is Time field
My local timezone is UTC +6
I am sorry if my question is stupid -_-'
▶ Time.now
#⇒ 2016-06-26 07:43:42 +0200
▶ Time.now.utc
#⇒ 2016-06-26 05:43:46 UTC
That said, 2016-06-01 00:00:00 +0600 and 2016-05-31 18:00:00 UTC you got are the same time, printed in different timezones.
In Rails you should always explicitly define a timezone you are dealing in:
Time.now # incorrect
Time.zone.now # correct
Further reading: http://danilenko.org/2012/7/6/rails_timezones/
Related
I'm trying to do a time zone comparison
time_finished = "2020-10-15 18:30:00 -0700"
time_finished.to_datetime > Time.zone.now.utc
both time_finished is already in UTC which I am trying to compare to current time
2020-10-27T00:00:00-07:00 > 2020-10-27 02:57:01 UTC
I get true when I'm expecting a false. Not sure what I am doing wrong.
time_finished is not in UTC if you convert it to UTC then it is greater than current UTC time, which is why it is returning true
irb(main):001:0> time_finished = "2020-10-27T00:00:00-07:00"
=> "2020-10-27T00:00:00-07:00"
irb(main):002:0> time_finished.to_datetime
=> Tue, 27 Oct 2020 00:00:00 -0700
irb(main):003:0> time_finished.to_datetime.utc
=> 2020-10-27 07:00:00 UTC
This issue has been eating away at me for a while.
I take a date time string:
[14] pry(#<EventsController>)> params[:event][:ended_at]
=> "05/31/2017 2:00 PM"
I convert it to a DateTime object:
pry(#<EventsController>)> to_datetime = DateTime.strptime(params[:event][:ended_at], "%m/%d/%Y %H:%M %p")
=> Wed, 31 May 2017 14:00:00 +0000
If I run the in_time_zone method on the to_datetime object it outputs the wrong time for pacific timezone:
[16] pry(#<EventsController>)> to_datetime.in_time_zone("Pacific Time (US & Canada)")
=> Wed, 31 May 2017 07:00:00 PDT -07:00
It should read "2:00PM" the same as how it is entered.
If I go to google and check to see if "14:00:00 +0000" is the correct entry for PDT time it verifies as correct:
http://imgur.com/a/ZJ80F
Any clues about why it's not converting correctly?
the error comes that the ended_at is been assumed by the system configuration that in this case is '+0000' you need to include the original timezone the ended_at is.
irb(main):001:0> date = "05/31/2017 2:00 PM"
=> "05/31/2017 2:00 PM"
irb(main):002:0> to_datetime = DateTime.strptime(date, "%m/%d/%Y %H:%M %p")
=> Wed, 31 May 2017 14:00:00 +0000
Note that this one has already set as UTC since was assumed by the system the timezone
irb(main):001:0> date = "05/31/2017 2:00 PM -0700"
=> "05/31/2017 2:00 PM -0700"
irb(main):002:0> to_datetime = DateTime.strptime(date, "%m/%d/%Y %H:%M %p %z")
=> Wed, 31 May 2017 14:00:00 -0700
irb(main):003:0> new_to_datetime = to_datetime.utc
=> Wed, 31 May 2017 21:00:00 +0000
irb(main):004:0> new_to_datetime.in_time_zone("Pacific Time (US & Canada)")
=> Wed, 31 May 2017 14:00:00 PDT -07:00
Update
#antonio's comment mention he was 1 hour off
irb(main):046:0> time = DateTime.strptime(date + " Pacific Time (US & Canada)", "%m/%d/%Y %H:%M %p %Z").class
=> DateTime
irb(main):047:0> time = DateTime.strptime(date + " Pacific Time (US & Canada)", "%m/%d/%Y %H:%M %p %Z")
=> Wed, 31 May 2017 14:00:00 -0800
irb(main):048:0> time.utc.class
=> Time
As you can see these are different classes and that's sign of problems what you can do is use Time instead of DateTime
irb(main):049:0> time = Time.strptime(date + " Pacific Time (US & Canada)", "%m/%d/%Y %H:%M %p %Z")
=> 2017-05-31 14:00:00 -0700
irb(main):050:0> time.class
=> Time
If we look at your first attempt:
pry(#<EventsController>)> to_datetime = DateTime.strptime(params[:event][:ended_at], "%m/%d/%Y %H:%M %p")
=> Wed, 31 May 2017 14:00:00 +0000
we see that "05/31/2017 2:00 PM" is being parsed correctly in UTC, hence the +0000 offset.
Then, if you convert that UTC time to PDT, you apply a -7 hour adjustment:
=> Wed, 31 May 2017 07:00:00 PDT -07:00
# -------------------------------^^^^^^
and the 14:00:00 becomes 07:00:00 because 14 - 7 == 7.
If you want to_datetime to be in PDT then it would be easiest to start that way by telling DateTime.strptime that the timestamp string is PDT:
no_tz = '05/31/2017 2:00 PM'
to_datetime = DateTime.strptime("#{no_tz} PDT", '%m/%d/%Y %H:%M %p %z')
# ------------------------------^^^^^^^^^^^^^^^---------------------^^
Then you'll get the 14:00:00 PDT you're looking for:
> to_datetime.iso8601
=> "2017-05-31T14:00:00-07:00"
# -------------^^------^^^^^^
I have a string that contains ISO8601 formatted date and time with timezone. How can I get Time or TimeWithTimezone object in the timezone specified in the string?
PC's timezone is '+01:00' and ActiveSupport::TimeWithZone is set to UTC. Here's what I've tried so far:
Loading development environment (Rails 3.2.13)
irb(main):001:0> Time.parse '1990-01-23T00:11:22-07:00'
=> 1990-01-23 08:11:22 +0100
irb(main):002:0> Time.zone.parse '1990-01-23T00:11:22-07:00'
=> Tue, 23 Jan 1990 07:11:22 UTC +00:00
Here's what I want:
irb(main):001:0> Time.???? '1990-01-23T00:11:22-07:00'
=> 1990-01-23 00:11:22 -0700
How can I parse the string and get time object in the timezone specified in the string?
Your issue was that you've used Time, whereas had to use DateTime:
DateTime.strptime('1990-01-23T00:11:22-07:00', "%Y-%m-%dT%H:%M:%S%z")
#=> Tue, 23 Jan 1990 00:11:22 -0700
EDIT
To make it a Time class object, you can do something like this (somewhat ugly, but still it does its job):
date = DateTime.strptime('1990-01-23T00:11:22-07:00', "%Y-%m-%dT%H:%M:%S%z")
#=> Tue, 23 Jan 1990 00:11:22 -0700
Time.new(date.year, date.month, date.day, date.hour, date.min, date.sec, date.zone)
#=> 1990-01-23 00:11:22 -0700
I have data as following
pubdate=10 hours ago +0000
pudate=Yesterday +0000
pubdate=1 min ago +0000
i want to convert in this format like this
Wed Jan 30 10:00:00 +0000 2013
please someone help me
You can use strftime on date, datetime object to convert into whatever format you want
1.9.3p327 :006 > date = Date.today
=> Wed, 30 Jan 2013
1.9.3p327 :007 > time = Time.now
=> 2013-01-30 17:00:30 +0530
1.9.3p327 :008 > date.strftime("%a %b %d %H:%M:%S %z %Y")
=> "Wed Jan 30 00:00:00 +0000 2013"
1.9.3p327 :009 > time.strftime("%a %b %d %H:%M:%S %z %Y")
=> "Wed Jan 30 17:00:30 +0530 2013"
I need to loop through several months in a Ruby on Rails application. For each month, I need to find the first and last day of that given month. These dates are then used in a separate query to find events occurring during those dates and running calculations on them.
Initially, I tried something like:
(11.months.ago.to_date.month..Date.today.month).each do |m|
start_date = '01-#{m}-#{Date.today.year}'.to_date.beginning_of_month
end_date = '01-#{m}-#{Date.today.year}'.to_date.end_of_month
end
Of course, the year isn't updated in this case in the event that 11 months ago involves going back to the previous year. And, I don't think this type of for/each loop works. I also tried mapping the numbers to an array and using the same method, but received an error.
What's the best way to accomplish something like this?
Here's one way to do it:
number_of_months = 0..11
number_of_months.to_a.reverse.each do |month_offset|
start_date = month_offset.months.ago.beginning_of_month
end_date = month_offset.months.ago.end_of_month
puts "Start date : #{start_date} End date : #{end_date}"
end
=>
Start date : 2012-01-01 00:00:00 -0700 End date : 2012-01-31 23:59:59 -0700
Start date : 2012-02-01 00:00:00 -0700 End date : 2012-02-29 23:59:59 -0700
Start date : 2012-03-01 00:00:00 -0700 End date : 2012-03-31 23:59:59 -0600
Start date : 2012-04-01 00:00:00 -0600 End date : 2012-04-30 23:59:59 -0600
Start date : 2012-05-01 00:00:00 -0600 End date : 2012-05-31 23:59:59 -0600
Start date : 2012-06-01 00:00:00 -0600 End date : 2012-06-30 23:59:59 -0600
Start date : 2012-07-01 00:00:00 -0600 End date : 2012-07-31 23:59:59 -0600
Start date : 2012-08-01 00:00:00 -0600 End date : 2012-08-31 23:59:59 -0600
Start date : 2012-09-01 00:00:00 -0600 End date : 2012-09-30 23:59:59 -0600
Start date : 2012-10-01 00:00:00 -0600 End date : 2012-10-31 23:59:59 -0600
Start date : 2012-11-01 00:00:00 -0600 End date : 2012-11-30 23:59:59 -0700
Start date : 2012-12-01 00:00:00 -0700 End date : 2012-12-31 23:59:59 -0700
First, count the number of months between two dates (courtesy of Massimiliano Peluso):
start_date = 13.months.ago.to_date
# => Wed, 16 Nov 2011
end_date = Date.today
# => Sun, 16 Dec 2012
number_of_months = (end_date.year*12+end_date.month)-(start_date.year*12+start_date.month)
# => 13
Then from the start month, counting each month thereafter, find the first/last dates and append to an accumulator array.
dates = number_of_months.times.each_with_object([]) do |count, array|
array << [start_date.beginning_of_month + count.months,
start_date.end_of_month + count.months]
end
# => ...
Now dates will contain an array with nested Date pairs corresponding to the first and last date of each month. This dates array will be easy to iterate for processing.
dates
# => [[Tue, 01 Nov 2011, Wed, 30 Nov 2011], [Thu, 01 Dec 2011, Fri, 30 Dec 2011], ...
dates[0].first
# => Tue, 01 Nov 2011
dates[0].last
# => Wed, 30 Nov 2011
dates[0].last.class
# => Date
This is tested and working in Rails 3.2.5/Ruby 1.9.3p194
(start_date..end_date).select{|date| date.day == 1}.map{|date| [date.beginning_of_month, date.end_of_month]}
time_start = 13.months.ago
time_end = 0.seconds.ago
time = time_start
while time < time_end
puts("m:#{time.month} y:#{time.year}")
time = time.advance(months: 1)
end
start and end of next 12 months from (including) some_date:
(0..11).map do |m|
start_date = some_date.at_beginning_of_month + m.month
end_date = start_date.at_end_of_month
end