okay, lets say we have three date ranges.
1. date1=01-20-2015..02-15-2015
2. date2 = 01-01-2015..01-31-2015
3. date3 = 02-01-2015..02-28-2015
I need a way to calculate how many days between date1 and date2 are overlapped (10 days in this case).
and how many days between date1 and date3 are overlapped (15 days in this case)?
Is there a easy way to calculate this?
If you have:
r1= (Date.new(2015, 01, 20))..(Date.new(2015, 2, 15))
r2= (Date.new(2015, 01, 1))..(Date.new(2015, 1, 31))
You can check the intersection this way:
r1.to_a & r2.to_a
=> [Tue, 20 Jan 2015,
Wed, 21 Jan 2015,
Thu, 22 Jan 2015,
Fri, 23 Jan 2015,
Sat, 24 Jan 2015,
Sun, 25 Jan 2015,
Mon, 26 Jan 2015,
Tue, 27 Jan 2015,
Wed, 28 Jan 2015,
Thu, 29 Jan 2015,
Fri, 30 Jan 2015,
Sat, 31 Jan 2015]
The cost is to convert ranges to arrays though
You can notice (r1.to_a & r2.to_a).count #=> 12, not 10 but well depends whether or not you include boundaries.
Related
I would like to say if now is less than Jan 1st at midnight Pacific Standard Time. I thought:
DateTime.new(2017,01,01, 00, 00, 00).in_time_zone('Pacific Time (US & Canada)')
would do it but it gives me back?
Sat, 31 Dec 2016 16:00:00 PST -08:00
Should I be using?
DateTime.new(2017,01,01, 8, 00, 00).in_time_zone('Pacific Time (US & Canada)')
or what is the proper, Rails-approved way of saying a specific day / time in Rails for a specific time zone?
You can specify the timezone when creating the datetime, i.e:
datetime = DateTime.new(2017,01,01, 00, 00, 00, 'PST')
#=> Sun, 01 Jan 2017 00:00:00 -0800
datetime.utc
#=> Sun, 01 Jan 2017 08:00:00 +0000
I need to use .next_week not +1.week. Because the .next_week make the date next week and Monday at the same time. It's so comfortable. And then I subtract some hours. But nothing has been changed. Here's my code.
newDate = DateTime.now
newDate = newDate.next_week - 3.hours
=> Sun, 30 Aug 2015 21:00:00 +0900
newDate = newDate.next_week - 3.hours
=> Sun, 30 Aug 2015 21:00:00 +0900
newDate = newDate.next_week - 3.hours
=> Sun, 30 Aug 2015 21:00:00 +0900
Why they can't be used same time? Please explain to me. Thanks.
This has nothing to do with #next_week and - 3.hours working at the same time. It is a misunderstanding about how #next_week works.
#next_week assumes by default a week is Monday-Sunday. This means when calling just #next_week it will return the next Monday at 00:00:00:00.
Example:
DateTime.now.next_week
#=> Mon, 03 Aug 2015 00:00:00 -0400
Okay so that make sense. Now you are substracting 3.hours making it Sunday again
DateTime.now.next_week - 3.hours
#=> Sun, 02 Aug 2015 21:00:00 -0400
Still makes perfect sense. Now here is where the confusion is. Since you rolled back to Sunday by subtracting 3.hours the #next_week is the following Monday again so
(DateTime.now.next_week - 3.hours).next_week
#=> Mon, 03 Aug 2015 00:00:00 -0400
I hope this helps you understand the situation a bit better.
If you use + 1.week this will take you to the same exact point in time the following week
DateTime.now + 1.week
#=> Fri, 07 Aug 2015 09:01:17 -0400
This does not rely on a weekly calendar but rather the basic math of adding 7 days to the referenced Date object.
In conclusion while #next_week might be comfortable you need to understand what it actually implies and I think I would prefer + 1.week in this case for its flexibility and its lack of assumption.
There are also many other methods for traversing time through ActiveSupport such as #advance, #days_since, weeks_since, etc.
Final note next_week can accept a day parameter as well e.g
DateTime.now.next_week(:tuesday)
#=> Tue, 04 Aug 2015 00:00:00 -0400
DateTime.now.next_week(:thursday)
#=> Thu, 06 Aug 2015 00:00:00 -0400
I have a hash like this:
a = {:start=>"Tue, 27 Jan 2015 13:00:00 +0000", :end=>"Tue, 27 Jan 2015 13:30:00 +0000", :title=>"2015-01-27T13:00:00+00:00 to 2015-01-27T13:30:00+00:00"}
and another hash like
b = {:start=>Tue, 27 Jan 2015 13:30:00 +0000, :end=>Tue, 27 Jan 2015 14:00:00 +0000, :title=>"2015-01-27T13:30:00+00:00 to 2015-01-27T14:00:00+00:00"}
I want to merge these two into
c = {{:start=>"Tue, 27 Jan 2015 13:00:00 +0000", :end=>"Tue, 27 Jan 2015 13:30:00 +0000", :title=>"2015-01-27T13:00:00+00:00 to 2015-01-27T13:30:00+00:00"},{:start=>Tue, 27 Jan 2015 13:30:00 +0000, :end=>Tue, 27 Jan 2015 14:00:00 +0000, title=>"2015-01-27T13:30:00+00:00 to 2015-01-27T14:00:00+00:00"}}
I tried the following:
c = a.merge(b)
and also
a.merge!(b)
This gave back only one value and
c = a+b
gave errors.
How to do this?
Its syntactically incorrect even for JSON, I believe. You want an array of hashes, by turning the outer {} into []. Better: c = [a,b].to_json
I have two DateTime objects, one in the past and one representing the current datetime. I am trying to find out how many minutes have passed between the two:
past = "Wed, 03 Jul 2013 00:59:39 UTC +00:00".to_datetime
now = "Wed, 03 Jul 2013 01:04:19 +0100".to_datetime
seconds = (now - past) #result is (-83/2160)
This is incorrect. Seconds should be 280, the number of seconds that have passed between the two times.
Subtracting two DateTimes returns the elapsed time in days.
So you can do:
past = "Wed, 03 Jul 2013 00:59:39 UTC +00:00".to_datetime
now = "Wed, 03 Jul 2013 01:04:19 +0100".to_datetime
seconds = (now - past) * 1.day
# => -3320.0
Or you could do:
seconds = (now.to_i - past.to_i)
# => -3320
※ The result is negative because of the Timezone.
past.utc
# => Wed, 03 Jul 2013 00:59:39 +0000
now.utc
# => Wed, 03 Jul 2013 00:04:19 +0000
You can see that now is actually older than past.
ActiveSupport::TimeZone.new(-4).parse("2012-08-20T14:00:00-0400")
returns
Mon, 20 Aug 2012 15:00:00 ADT -03:00
I would expect parse() to return a Time with -04:00, like Mon, 20 Aug 2012 14:00:00 EDT -04:00
How would you handle this? I think that Daylight Saving Time is messing up things here.
I've handled this problem using new_offset from DateTime.
time = "2012-08-20T14:00:00-0400"
DateTime.parse(time).new_offset("+2")
returns Mon, 20 Aug 2012 20:00:00 +0200