cucumber capybara testing date picker selects wrong day - ruby

I am trying to write a cucumber test for a page that includes a datepicker. I swear this was working yesterday, but not so much today.
Then(/^select date (\d+) day(?:s|) prior to today$/) do |n|
day=Date.today-(n.to_i)
target = Date.strptime("#{day}",'%Y-%m-%d')
target_month_year = target.strftime('%B %Y')
selected_month_year = (find('.datepicker-switch').native.text)
unless target_month_year == selected_month_year
find('.prev').click
sleep 1
end
find('.day', :text => "#{day.day}", match: :prefer_exact).click
sleep 2
end
Then I have a separate test that checks that the correct date is presented after selection. I have verified that day.day is giving me the correct result by including a puts(day.day), as well as all the other variables. I think the problem is a matching issue, today's date is 04/24/2015 and I selecting 15 days prior. So the datepicker that displays the month and year above and allows you to select previous or next, then the days shown are according to how many days in that particular month. and a few day before and after. the days for the previous month are class="old day" and the ones for the month displayed are class="day" and for the next month class="new day". so the month I want is april and the day is the ninth. it keeps selecting the 29th of march. which is the first day listed on the page that contains a "9". but the class is wrong, since I want "day" not "old day" and the day is wrong because I want "9" not "29" I even put in a :prefer_exact because that has fixed matching the wrong element in the past for me.
Not sure what to try next. Any advice greatly appreciated.
cucumber 1.3.10
capybara 2.4.1
ruby 1.9.3p551

Ideally, don't select by text if you can avoid it.
But in this case try using a regex instead of just plain text.
find('.day', :text => Regexp.new("^#{day.day}$"), match: :prefer_exact).click
There's a little related reading at the end of this (currently unimplemented) issue: https://github.com/jnicklas/capybara/issues/1256

Related

Ruby Date Gem Invalid Date

So I am iterating through a hash where one of the key/values is {date: => 'MM/DD/YYYY'}
When I iterate through, I am using the date gem to find out what day of the week that each date is, (0-6).
To get a day of the week for the index I am currently at as an integer so i can compare it to another integer, the idea is to check if the day of the week of the index is the same as the day of the week i am searching for.
To get that int I run the following commands:
d = Date.parse(hash[i].values[2])
day_of_the_week = d.cwday
When i do this on its own for just a cherry-picked date this works fine, but I am iterating through the hash, what i get is:
search.rb:25:in `parse': invalid date (ArgumentError)
for the particular date '9/13/17'.
Is there something wrong with '9/13/17'? Why does this actually work for other days (it starts at '9/5/17') and then get randomly stuck at this day?
And as I was writing this, I did a little digging and found exactly what index it was:
d = Date.parse(hash[4224].values[2])
day_of_the_week = d.cwday
Gives me the same error, I am completely baffled, what is going on? Also its not the lack of MM in 9/etc because every other month is the same way.
EDIT: The result should be 2, September 12th 2017 was a Tuesday.
You need to pass the format of your date, use
Date.strptime('9/13/2017', '%m/%e/%Y').
I found that using:
d = Date.strptime(hash[i].values[2], '%m/%d/%Y')
Does creates a date object of the current index better than:
d = Date.parse(hash[i].values[2].to_s)
Replacing that did the trick.

Find objects that match today's date in Ruby

I have a calendar object that lists a number of object events. Each event contains a start_date. So when I call
event.start_date it gives me "8/6/2017 3:00pm"
I want to be able to find all the events with today's date. I'm using Chronic. So
Chronic.parse('today') would give me "8/6/2017 00:00:00" or something like it
I'm not using Rails just Ruby. Thus, I don't have all the special methods that come with Rails to help me with this.
If I enter a specific date I could find the events with that specific date by using
event.start_date.starts_with?("8/6/2017")
but I haven't figured out how to do it with today's date.
Any help would be appreciated.
time = Chronic.parse('today')
target_date = Chronic.parse('8/6/2017').to_date
puts time.to_date == target_date
Time class (what Chronic returns) holds datetimes. Date only holds dates. If you convert Time to Date, you discard the time-of-day information, and then you can compare just the date itself.
Note that Date.today is the same thing as Chronic.parse('today').to_date (and same as Time.now.to_date). Also note that Time class doesn't have #to_date unless you require 'time', but Chronic does it for you.

Why does Ruby think there are 366 days in 100 AD?

The following expression returns '366' in Ruby, implying 100 AD is a leap year (which it is not):
(Date.ordinal(101) - Date.ordinal(100)).to_i
Same with DateTime.
However, Date.leap?(100) correctly returns false.
Same results for version 1.9.1. and 2.0.0.
What gives? Should I file a bug report?
Update
Also, apparenly 1582 AD is 10 days short!
(Date.ordinal(1583) - Date.ordinal(1582)).to_i
=> 355
According to Wikipedia, 100 was indeed a leap year and 1582 did indeed skip 10 days.
Apparently, prior to 1582-10-15, Ruby interprets dates as Julian calendar dates, instead of Gregorian calendar dates. More details here:
http://teleologi.blogspot.com/2013/05/ruby-times-dates-good-bad-and-so-on.html
Apparently not a bug, but definitely violates the principle of least surprise (at least in the eyes of this coder).
How confusing!
Edit
Debate about "reasonable defaults" aside, Ruby seems to quite flexible on these touchy issues of calendar-choice, compared to other languages. I've learned that the Date and DateTime constructors can receive a "reform date" constant, which determines when the transition from Julian to Gregorian calendar occurs. The default is ITALY (1582-10-15), but ENGLAND is also an option (the jump occurs at 1752-09-14).
To avoid surprises from transitioning between calendars, I should have used the Gregorian calendar for all dates:
(Date.ordinal(i+1,1,Date::GREGORIAN) - Date.ordinal(i,1,Date::GREGORIAN)).to_i
=> 365
Also, Date.leap?(100) returned "false" because it is an alias of Date.gregorian_leap?. Meanwhile, Date.julian_leap(100) returns true. To avoid surprises, probably best to use method version of leap?, which uses whichever reform date you've picked.
Date.new(100, 1, 1, Date::JULIAN).leap?
=> true
Date.new(100, 1, 1, Date::GREGORIAN).leap?
=> false

How do I get a file's age in days in Ruby?

How would I get the age of a file in days in Ruby?
NOTE that I need a way to accurately get the age of a given file; this means that leap years need to be taken into account.
I need this for a program that removes files after they reach a certain age in days, such as files that are 20 days or older.
And by age, I mean the last access time of a given file, so if a file hasn't been accessed in the past 20 days or more, it gets deleted.
In Perl, I know that you can use date::calc to calculate a date in terms of days since 1 AD, and I used to have a Common-Lisp program that used the Common-Lisp implementation of date::calc, but I don't have that anymore, so I've been looking for an alternative and Ruby seems to have the required capability.
path = '/path/to/file'
(Time.now - File.stat(path).mtime).to_i / 86400.0
#=> 1.001232
Here is the implementation of my above comment, it returns a floating point number expressing the number of days passed.
I know it is an old question, but I needed the same and came up with this solution that might be helpful for others.
As the difference is in days, there is not need to directly deal with seconds.
require 'date'
age_in_days = (Date.today - File.mtime(path).to_date).to_i
if age_in_days > 20
# delete logs
end
If using Rails, you can take advantage of ActiveSupport:
if File.mtime(path) < 20.days.ago
# delete logs
end
If you aren't using Rails, Eduardo's solution above would be my pick.

How to check time left until a specific date?

How do I check how many days left until a specific date?
For example, I want to start executing some logic after 3 days from today? I will be hard coding today's date, and subtract now from it, but how?
EDIT
What I want to do is to disable devise confirmation email at model for the next three days,
if Rails.env== 'production' && (three days left sense 1/7/2013)
devise :confirmable
end
why bother?
if Rails.env== 'production' && Time.now.strftime("%Y%m%d").to_i >= 20130110
devise :confirmable
end
If im getting it right ,
you just store the start time in a variable
then check it against the current time (you'll get the diff in seconds )
and if its larger then 72(3 days) the if statement will be done.
require 'time'
start_time=Time.now
if Rails.env== 'production' && ( (Time.new - start_time)/3600 >72 )
devise :confirmable
end
Use the time_diff gem to get the difference in terms of year, month, week, day, hour, minute and second that can be easily achieved as I did so.
Check this -
Rails calculate time difference
You will get your answer there.
in your class first you have to set date
e.g:
in your case you need to do something like this:
("object name".date.to_time<=time.now+3)
(do something)if x.date.to_time<=today
the technique to_time make`s you able to choose time (day,weeks,month,year)
ENV.all.each do|env|
where (env.date.to_time<= Time.now+3)
if Rails.env== 'production' && (env.date.to_time== today)
devise :confirmable
end
try
if Time.now >= <your_time_object> + 3.days
# start executing some logic
end

Resources