What's the difference between due < now() and due < "0" - jira

I have two JQL queries:
due >= "0"
due >= now()
Is there a difference? I cannot find a reference description for what "0" means.
Thank you.

No, there is no difference. The second query
2. due >= now()
compares due to the current date. The first query
1. due >= "0"
compares due to the current date MINUS "0" offset days or weeks, which is again equal to the current date.
Compare the Atlassian Advanced searching - fields reference page:
Or use "w" (weeks) or "d" (days) to specify a date relative to the current date. Be sure to use quote-marks (").

Related

PostgreSQL OVERLAPS overator not inclusive of end time - Rails

I am using Rails 5 and postgresql in my application.
The query is as follows.
.where('(start_time, end_time) OVERLAPS (time without time zone ?, time without time zone ?)',
start_time, end_time)
Scenario 1:
Range 1 => 0:00 - 08:00
Range 2 => 07:59 - 12:00
This shows an overlap which is correct.
Scenario 2:
Range 1 => 0:00 - 08:00
Range 2 => 08:00 - 12:00
This does not show an overlap. This is incorrect since 08:00 falls in both time ranges.
I assume the issue is that OVERLAPS considers < end_time and not <= end_time.
Any idea on how to fix this?
The documentation makes this quite clear:
Each time period is considered to represent the half-open interval start <= time < end, unless start and end are equal in which case it represents that single time instant. This means for instance that two time periods with only an endpoint in common do not overlap.
You can get what you want with range types and the overlaps operator &&:
WHERE tsrange(start_time, end_time, '[]') && tsrange(?, ?, '[]')
Here the third argument to tsrange specifies than both ends are included.
Using ranges has the additional advantage that you can support the condition with a GiST index.

Rails 5 active record query where date range overlaps with other date range

I have a model with planting_date_begin and planting_date_end. I want to retrieve all records where any date in planting_date_begin..planting_date_end overlap with the range for the current_week
example:
if planting_date_begin: 3/5/2017 and planting_date_end: 3-12/2017
and this week is 3/26/2017-4/1/2017 it is not included in query.
if planting_date_begin: 3/1/2017 and planting_date_end: 4/15/2017 it would be included.
I set current_week range:
today = Date.today
days_in_week = today.at_beginning_of_week..today.at_end_of_week
This syntax is not right but I want to do something like:
Planting.where((planting_date_begin..planting_date_end).overlaps?(days_in_week) )
What is a succinct way to handle this? Incidentally, I am using postgres in case there is a way to do it differently.
Maybe not as succinct, but I have to do this a lot in a current project and my method is...
start_date = Date.today.at_beginning_of_week
end_date = Date.today.at_end_of_week
#plantings = Planting.where('planting_date_end >= ? AND planting_date_begin <= ?', start_date, end_date)
This covers all overlaps.. if planting starts before the range and ends after the range, if planting starts during the range, if planting ends during the range.

How to write a query in Google Spreadsheet that returns date >= today?

I am writing the following =filter formula in Google spreadsheet:
=FILTER('Tab'!6:1963, ‘Tab’!E6:E1963 = "Major", ’Tab’!D6:D1963 > NOW())
Column D are dates and I am interested in including today. For instance today is the 7/19 and I would like to have the data that includes 7/19. My current formula returns values only from tomorrow (7/20). I tried the now()-1 but returned an #VALUE!.
Any help?
NOW() returns a datetime object (which includes the time). If you are comparing with a date, NOW() will (almost :) ) always be greater than the date alone (which would have a time component of 12:00 A.M., which is essentially 0).
Try using TODAY() to get the date only (adding/subtracting 1 will use tomorrow/yesterday, respectively).

Check if the value is in range between closest array's elements. (ruby)

I need to know how would I check if my given value is between two closest array's members. For example I have an array of dates with the date of week start in given period of time. And I need to check if my given date is in one of its week. For example:
2015-11-02
2015-11-09
2015-11-16
2015-11-23
And my given value is 2015-11-11 for example. How should I check if it is one of these weeks date? Thanks for help.
%w(2015-11-02 2015-11-09 2015-11-16 2015-11-23).any? do |date|
date.to_date.cweek == Date.today.cweek
end
And here is what this does:
First, you have an array of strings, you use any? to loop through it and check if any fulfils a requirement, then you cast you date strings into actual dates, and cweek gives you the number of a week in the year. Date.today gives you today's date.
Instead of Date.today.cweek you can use '2015-11-11'.to_date.cweek.
The loop above returns boolean; you could also get an array of values that fulfil a condition like this:
new_array = %w(2015-11-02 2015-11-09 2015-11-16 2015-11-23).map do |date|
date.to_date.cweek == '2015-11-11'.to_date.cweek
end.compact
Resources:
Date class on ruby-doc.org
Date & Time in Ruby on tutorialspoint.com
UPDATE
If you want to get from the database only records with a date from particular week, this is how you could do it:
my_date = '2015-11-11'.to_date
matching_records = MyResource.where( date: my_date.beginning_of_week..my_date.end_of_week )
The assumptions are that you have a model MyResource, and that it has a column date. What this does is returns a relation with all the records that have dates from the same week as my_date.
Assuming your dates array is sorted:
date >= dates.first && date <= dates.last
If you're dealing with strings, you can "require 'date'" and transform your strings to dates ("Date.parse('2001-02-03')").
As others have suggested, you can then see if your date is between the first and last entry of your list.
If the real list is sorted and each entry is one week apart, then you can easily find where in the list your guy is.
E.g., say the list is [date_0, date_1, date_2, ..., date_k] (all 1 week apart), and you're given a test_date between date_0 and date_k. Then (test_date.jd - date_0.jd)/7 gives you the index of the date in your list that is <= test_date.

Earliest date after current date without associated record

I have a Rails model DailyAssignment with a date column, and would like to find the first date after today which does not have a DailyAssignment associated with it.
For instance, if I have an instance today, no instance tomorrow, and an instance the day after tomorrow, this method should return tomorrow.
If I were to do this in Ruby, it would be something like:
(Date.today..1.year.since.to_date).find do |date|
DailyAssignment.where(date: date).empty?
end
This is medium okay since it will terminate the iteration once it finds a record, but has two issues:
Iterating through a collection in Ruby is slow.
Barring some sort of while construct, I need to specify an 'end' date.
Is there a nice, efficient way to do this in PostgreSQL?
If you can, you should use a custom query to search through your database (these kind of searches are a lot faster within the DB).
If you search for a date within a time range, you can use the
generate_series(timestamp, timestamp, interval) function:
select s
from generate_series(?, ? + interval '1 year'), interval '1 day') s
left join daily_assignment on s = "date"
where "date" is null
limit 1
If you have no real upper bound, you can use a self-join to get the next free date:
select coalesce(
(select c."date" + interval '1 day'
from daily_assignment c
left join daily_assignment n on n."date" = c."date" + interval '1 day'
where c."date" > ? - interval '1 day'
and n."date" is null
order by c."date"
limit 1),
? + interval '1 day'
)
? marks mean the parameter of today (you may need casts, depending on your input); you could use now() instead, if you prefer.
P.S.: please, do not use date as a column name, it is a reserved word in SQL, and tells nothing about the column itself. Instead, you can use names like created_at, updated_at, happens_at, etc. or even at_date.
What I propose is to do 1 select query between dates, then loop your results and compare them with your selected results.
# select all dailyassignments
results = DailyAssignment.where("date >= from_date AND date <= to_date")
not_found_dates = []
(Date.today..1.year.since).find do |date|
found_assignment = results.detect {|instance| instance.date == date }
not_found_dates << date if found_assignment.nil?
end
You can try it this way:
def first_date_without_assignment
assignments = DailyAssignment.select('date').where('date > ?', Date.today)
return Date.tomorrow if assignments.empty?
assignment_dates = assignments.map(&:date)
date_range = (Date.tomorrow..(assignment_dates.last.advance(days: 1)).to_a
(date_range - assignment_dates).first
end
I didn't test it so I could mistype something, but it could work. I also find this, it should work on postgres http://www.postgresql.org/message-id/4F96EC90.6070600#encs.concordia.ca but it could be quite hard to write in rails or at least bad looking.

Resources