Timezone offset rails - ruby-on-rails

In rails how to get the exect day after adding offset
irb(main):066:0> DateTime.now.utc
=> Sat, 29 Mar 2014 19:12:57 +0000
after adding 5.30 hours it should show 30 march. why it is 29 again .anything I missed here
irb(main):065:0> DateTime.now.utc.change(:offset => "+0530")
=> Sat, 29 Mar 2014 19:09:03 +0530

Related

Include 'UTC` in datetime

One of my assertions is failing because of this slight niggle:
its(:register_token_created_at){ should eq DateTime.now.utc }
results in:
expected: Fri, 28 Mar 2014 00:06:33 +0000
got: Fri, 28 Mar 2014 00:06:33 UTC +00:00
So how should I change DateTime.now.utc for it to pass?
I'm assuming that you have your test environment with the following configuration:
yourapp/config/environments/test.rb
#config.time_zone = "whatever"
config.active_record.default_timezone = :utc
Then, you can replace:
DateTime.now.utc #Fri, 28 Mar 2014 00:54:28 +0000
With:
Time.zone.now #Fri, 28 Mar 2014 00:54:51 UTC +00:00
That works for me...
Another (ugly) solution can be:
DateTime.now.utc.strftime("%a, %m %b %Y %H:%M:%S UTC %:z") #"Fri, 03 Mar 2014 00:55:46 UTC +00:00"
And then, you can compare as strings

Calculations on datetime range

I have a problem with calculation on datetime fields.
I have two variables:
a = Sat, 01 Feb 2014 13:00:00 CET +01:00
and
b = Fri, 28 Feb 2014 13:00:00 CET +01:00
And I want to calculate how many days passed from a to b range.
Please Help.
You can use this.
require 'date'
( Date.parse('Fri, 28 Feb 2014 13:00:00 CET +01:00') - Date.parse('Sat, 01 Feb 2014 13:00:00 CET +01:00')).to_i
=> 27
i.e
( Date.parse(b) - Date.parse(a)).to_i
require 'date'
a=Date.parse('Sat, 01 Feb 2014 13:00:00 CET +01:00')
=> #<Date: 2014-02-01 (4913379/2,0,2299161)>
b=Date.parse('Fri, 28 Feb 2014 13:00:00 CET +01:00')
=> #<Date: 2014-02-28 (4913433/2,0,2299161)>
b-a
=> (27/1)
(b-a).to_i
=> 27
You can also use time_diff gem to calculate the difference.
In your gem file add gem 'time_diff'
I have added the code in index.html.erb but, you can use it any where you want.
take a look on following code.
<% require 'time_diff' %>
<% a = Time.diff(Time.parse('Sat, 01 Feb 2014 13:00:00 CET +01:00'), Time.parse('Fri, 28 Feb 2014 13:00:00 CET +01:00'), "%d") %>
<%= a[:diff] %>
and I got the following result
"27 days"
You can also take a look on
https://github.com/abhidsm/time_diff

How do get a random DateTime rounded to beginning of hour in Rails?

Basically I'd like to get a random datetime within the last year:
rand(1.year).ago #=> Sun, 22 Sep 2013 18:37:44 UTC +00:00 (example)
But how do I go about specifying or limiting this to times on the hour? For example:
Sun, 22 Sep 2013 18:00:00 UTC +00:00
Sat, 02 Nov 2013 10:00:00 UTC +00:00
Fri, 12 Apr 2013 21:00:00 UTC +00:00
I finally found what I was looking for. #Stoic's answer is very good but I found this available method (http://api.rubyonrails.org/classes/DateTime.html):
rand(1.year).ago.beginning_of_hour
Does exactly the same thing but looks neater and prevents you from having to write your own function.
Rounding datetime to the nearest hour in Rails would be
(DateTime.now + 30.minutes).beginning_of_hour
Not the answer to the actual question, but it does answer the title of the question (which is how i got here).
Try this:
def random_time_to_nearest_hour
time = rand(1.year).ago
time - time.sec - 60 * time.min
end
Examples:
[1] pry(main)> random_time_to_nearest_hour
=> Sun, 28 Apr 2013 16:00:00 UTC +00:00
[2] pry(main)> random_time_to_nearest_hour
=> Sat, 08 Jun 2013 15:00:00 UTC +00:00
[3] pry(main)> random_time_to_nearest_hour
=> Thu, 22 Aug 2013 23:00:00 UTC +00:00
[4] pry(main)> random_time_to_nearest_hour
=> Tue, 29 Jan 2013 14:00:00 UTC +00:00
[5] pry(main)> random_time_to_nearest_hour
=> Tue, 13 Aug 2013 06:00:00 UTC +00:00
[6] pry(main)> random_time_to_nearest_hour
=> Mon, 03 Jun 2013 08:00:00 UTC +00:00
[7] pry(main)>
Note that, this method will always floor down to the nearest hour, but since you are anyways generating a random time, it wont matter if this time is getting floor'ed down or getting round'ed. :)

Change a hash into an array

I have an Rails 4 application that collects attendance at church services. Some weeks there are two services and some weeks there is only one. I need to get the total attendance for each week and show it as a graph.
By calling:
Stat.calculate(:sum, :attendance, group: :date)
in the console I have been able to collect the data in a hash like this:
{Sun, 06 Jan 2013=>66, Sun, 13 Jan 2013=>65, Sun, 20 Jan 2013=>60, Sun, 27 Jan 2013=>67, Sun, 03 Feb 2013=>60, Sun, 10 Feb 2013=>76, Sun, 17 Feb 2013=>65, Sun, 24 Feb 2013=>52, Sun, 03 Mar 2013=>52, Sun, 10 Mar 2013=>45, Sun, 17 Mar 2013=>56, Sun, 24 Mar 2013=>134, Sun, 31 Mar 2013=>76, Sun, 07 Apr 2013=>88, Sun, 14 Apr 2013=>87, Sun, 28 Apr 2013=>93, Sun, 05 May 2013=>93, Sun, 12 May 2013=>95, Sun, 19 May 2013=>90, Sun, 26 May 2013=>87, Sun, 02 Jun 2013=>71, Sun, 09 Jun 2013=>86, Sun, 16 Jun 2013=>109, Sun, 23 Jun 2013=>80, Sun, 30 Jun 2013=>68, Sun, 07 Jul 2013=>75, Sun, 14 Jul 2013=>73}
But What I need for my chart is an array of hashes in the form of:
{date: "Sun, 23 Jun 2013", attendance: 80}, {date: "Sun, 30 Jun 2013", attendance: 68"}
So I am trying to figure out how to convert the first form into the second form.
I'm sure its something pretty easy, but my limited rails knowledge is hitting a wall.
.collect{|key,value| {:date => key, :attendance => value} }
loop through and create a new hash where the original key becomes the value for date and the original value becomes the value for attendance. These new hashes are collected into an array.
You can think as below:
h = {"Sun, 06 Jan 2013"=>66, "Sun, 13 Jan 2013"=>65, "Sun, 20 Jan 2013"=>60 }
h.map{|k,v| Hash[:date,k,:attend,v]}
# => [{:date=>"Sun, 06 Jan 2013", :attend=>66},
# {:date=>"Sun, 13 Jan 2013", :attend=>65},
# {:date=>"Sun, 20 Jan 2013", :attend=>60}]

active record query over datetime array

Given I have an array of datetimes:
array = [Sat, 30 Jul 2011 00:00:00 CEST +02:00, Sat, 30 Jul 2011 00:15:00 CEST +02:00, Sat, 30 Jul 2011 00:30:00 CEST +02:00, Sat, 30 Jul 2011 00:45:00 CEST +02:00
I want my model class method to return the datetimes that dont match (aren't scheduled)
Sat, 30 Jul 2011 00:00:00 CEST +02:00
#appointment.rb (with colum `date` as DateTime)
def self.booked(array)
where("date NOT IN (?)", array)
end
Thx for advise!
It's because your array is malformed:
array = [Sat, 30 Jul 2011 00:00:00 CEST +02:00, Sat, 30 Jul 2011 00:15:00 CEST +02:00, Sat, 30 Jul 2011 00:30:00 CEST +02:00, Sat, 30 Jul 2011 00:45:00 CEST +02:00
notice it's separating the days as individual elements. Or is this your doing? If not, you should probably convert them all to actual DateTime objects (using #parse probably) and then put them in the array.
Also, you might want to make that method into a scope:
scope :booked, lambda { |datetimes| where("date NOT IN (?)", datetimes) }
I'm guessing that date is actually a date in the database, not a datetime or timestamp. So, you need to convert your Ruby Datetimes to just Date instances:
def self.booked(array)
where("date NOT IN (?)", array.map(&:to_date))
end
If you don't convert them to Dates by hand, AR won't know that it is supposed to convert them to dates for the database; then, depending on the underlying database, you could get empty results or possibly errors.
Your code look fine to me, but you need to provide an array, like this:
where(['date NOT IN(?)', array])
Here is an example with a Webinar class with a date column
>> array = Webinar.all[0..2].map(&:date)
=> [Wed, 04 May 2011 02:16:00 PDT -07:00, Tue, 05 Apr 2011 06:00:00 PDT -07:00, Thu, 30 Jun 2011 07:30:00 PDT -07:00]
>> count_without_array = (Webinar.count - array.size)
>> Webinar.where(['date NOT IN(?)', array]).count == count_without_array
=> true
Keep in mind that date must be exact match, if there is 1 second difference its not going to work.

Resources