How to find the date number given the Year, month, day of the week and the week it falls on in ruby - ruby

So i'm new to ruby and not to familiar with the syntax of the Date gem, but is there a way to find the date of a moving holiday like fathers day?
So my current class for the moving holidays looks like this:
class MovingHoliday < Holiday
def initialize (name, month, day_of_week, week, year=2016)
#name = name
#month = month
#day_of_week = day_of_week
#week = week
#year = year
#my_date = Date.new(year, month, day)
end
end
and my input looks like this:
fathers_day = MovingHoliday.new("Fathers Day", 6, "sunday", 4, year)
The 6 is the month, "sunday" is well the day_of_the_week, and 4 is the week it falls on.
I just can't figure out the syntax if there is any that would/could do conversion for this.
And I can't use any gems other then rubys Date.

class MovingHoliday < Holiday
def initialize (name, month, day_of_week, week, year=2016)
#name = name
#month = month
#day_of_week = day_of_week
#week = week
#year = year
#my_date = Date.new(year, month, 1)
#my_date += 1 while !my_date.send("#{day_of_week}?")
#my_date += (7 * (week - 1))
end
end
Not pretty, but it works. It takes the first of the month, moves up a day until it's the correct day of the week, and then increases by the appropriate number of weeks.
As it stands, it assumes that you're given an actual day of the week. You'll probably want to sanitize the input in some manner or convert from "sunday" to 0 (my_date.wday returns 0 for sunday, 1 for monday, etc)

date in Ruby is not a gem, it's part of the standard library, it's just not loaded unless you require it. (In Ruby, require is used for both purposes, loading gems and loading parts of the standard library that are not loaded by default.)
I don't know of any built-in functionality that would calculate the date for you, I think you'd have to write that yourself, since you don't want to consider other gems.

Related

LUA credit card expiry date validation

I have an application collecting credit card data. Before sending the information out to the payment entity I am trying to make sure the information entered is, at least, valid. I already worked out the card number and cvv numbers but I am not so sure about the expiry date. The format I get the info is MMYY. So what I am doing is:
-- Simple function to get current date and times
function getdatetime(tz)
local tz = tz or 'America/New_York';
local luatz = require 'luatz';
local function ts2tt(ts)
return luatz.timetable.new_from_timestamp(ts);
end
local utcnow = luatz.time();
local time_zone = luatz.get_tz(tz);
local datetime_raw = tostring(ts2tt(time_zone:localise(utcnow)));
local year, month, day, hour, min, sec, time_reminder = string.match(datetime_raw, "^(%d%d%d%d)%-(%d%d)%-(%d%d)[Tt](%d%d%.?%d*):(%d%d):(%d%d)()");
return year, month, day, hour, min, sec;
end
local current_year, current_month = getdatetime() -- Get current year/Month
local card_expiry_date = 'YYMM'; -- In the app this actually get a value eg: 2204, 2301, 2010, etc.
local card_exp_year = string.sub(card_expiry_date , 3, 4)
local card_exp_month = string.sub(card_expiry_date , 1, 2)
-- Extract the last two digits of the Year
current_year = string.sub(current_year , 3, 4)
-- Check month is valid
if(card_exp_month < '01' or card_exp_month > '12')then
print("This is not a valid month")
else
-- Check date is this month or after
if((card_exp_year < current_year) or (card_exp_year == current_year and card_exp_month < current_month))then
print("Date cannot be before this month.")
else
print("All is good.")
end
end
I do not know if this is the most elegant solution but it works. However it has a huge bug: it will fail at the end of the century. Since I only know the last two digits of the expiry date year, if a card expires in 2102 for instance and we were in 2099 my logic would wrongly reject the date (02 is less than 99).
I am very aware that me an my simple app will likely not be around by then but it bugs me to leave it like this.
Can anyone please suggest a proper way to do this validation?
Thank you!
Wilmar
Credit cards usually expire within a few years. 3 years is average according to some quick web search. Also the owner of a century only card can be safely assumed to be dead and so is his card account.
So when you get a card with 02 in 2099 there is only one reasonable option.
Calculate two differences and pick the smaller one.
Something like local expiresIn = math.min(math.abs(99-2), math.abs(99-102))

List Array of Days Between Two Dates

I would like to list an array of days between two dates. I can list an array of months with the code below. How might I change this to show every day between two dates?
require 'date'
date_from = Date.parse('2011-05-14')
date_to = Date.parse('2011-05-30')
date_range = date_from..date_to
date_months = date_range.map {|d| Date.new(d.year, d.month, 1) }.uniq
date_months.map {|d| d.strftime "%d/%m/%Y" }
puts date_months
I don't know which day you meant, thus I have shown all the ways.
#wday is the day of week (0-6, Sunday is zero).
(date_from..date_to).map(&:wday)
#mday is the day of the month (1-31).
(date_from..date_to).map(&:mday)
#yday is the day of the year (1-366).
(date_from..date_to).map(&:yday)
OP's actual need was not much clear to me. After few comments between us, I came to know from OP's comment, the below answer OP is looking for -
(date_from..date_to).map(&:to_s)

Determine the amount of clicks required to reach a specific date using a JQuery calendar?

I have a jquery calendar for the start date of a project.
Using Watir (automated browser driver, a gem for ruby), I have a set date that I would like to enter in.
The calendar start date is always today's date, whatever that may be for the day it is used. I was wondering if there was a way that ruby can process what today's date is, and use the specified date provided by the user, to calculate the difference of months between them.
Here is an example of the Calendar plugin: http://jqueryui.com/datepicker/
example:
today's date is 30/10/2012, if there was a project that were to start on the 20/12/2012, that would be 2 months from now, so 2 clicks on the next month button.
Is there a way I could do this?
Here is how I approached a similar situation with JSdatepicker:
$today = Time.now.strftime("%e").gsub(" ", "") #one digit day of month without leading space
#browser.text_field(:id => /dateAvailable/).click
Watir::Wait.until(60) {#browser.div(:id => /dateAvailable_popup_cal/).td(:text => $today).exists?}
#browser.div(:id => /dateAvailable_popup_cal/).td(:text => $today).click
Set or grab the date.
Click the text_field that fires the JSDatePicker object
Wait until the calendar actually pops up
The current month is shown, so choose today's date number.
In your case, you also need to set the month. Whether prompting the user for this, or choosing "today", the theory is the same:
$month = Date::MONTHNAMES[Date.today.month] #etc
Pseudo-code making lots of assumptions (only future dates, month name shown on calendar as text, etc):
while !#jquerytablewindow.text.include?($month)
next_month_button.click
end
I don't see a specific advantage to my method versus counting each month, unless of course we add a month to the calendar one day and you still want your code to work!
You could do:
#End date converted to date object
specified_date = '20/12/2012'
end_date = Date.parse(specified_date)
#Start date (today - 30/10/2012)
today = Date.today
#Determine difference in months
number_of_months_up_to_today = (today.month + today.year * 12)
number_of_months_up_to_end = (end_date.month + end_date.year * 12)
clicks_required = number_of_months_up_to_end - number_of_months_up_to_today
#=> 2
Basically it is counting the number of months since the year 0 and then finding the difference.

Convert DDMON (eg. 23JAN) date format to full date with correct year applied in Ruby

Am looking for a compact solution for converting the dates that come from the airline reservation systems (GDS like Sabre/Galileo/Amadeus) which dont have the year - eg. 23JUN to a standard date, but need to determine the year quickly to tack on in Ruby. (FYI - on these systems date can only be max 355 days from today. So there is no ambiguity of interpreting 12MAY as 2013-05-12 instead of 2012-05-12 assuming today is 12MAY 2012). If today is 25th Dec 2012, and date is entered as 01JAN, this would be 01JAN2013 in the future, and if today is 21JUL 2012, input date of 01JAN would also become 01JAN 2013 as it is a past date in the current year.
So I can do something like:
t = DateTime.now
crsdate = DateTime.strptime("23JAN","%d%b")
if crsdate >= t
# Year is current year
else
# Year is next year
end
Is there a more elegant way of doing this?
How about this?
t = DateTime.now
crsdate = DateTime.strptime("23JAN","%d%b")
crsdate >= t ? crsdate >>= 12 : crsdate
If you don't need to modify crsdate because you just want to return this, use >> instead of >>=.

How can I create DateTime object based on the day?

I want to create an Datetime object based on the number of day in the year.
This number is from the 365 days of the year (for example it can be: 123 or 23 or 344...)
How can I do that?
Thanks
Use the DateTime.ordinal method. Here's an example to get the 100th day of year 2011.
require 'date'
year, day = 2011, 100
DateTime.ordinal(year, day)
# #<DateTime: 2011-04-10T00:00:00+00:00 (4911323/2,0,2299161)>
If you want it as the number of days from now you should do the following:
time = Time.new + (60*60*24)*(numberOfDaysFromNow)
If you want it as the number of days from the start of the year you should do the following
time = Time.new(Time.now.year) + (60*60*24)*(dayOfTheYear-1)

Resources