I'm brand new to learning Ruby. I learned eons ago some basic Pascal in high school - but this is of course comparing apples to oranges. What I do have is a partner who is much more experienced than I in the language.
I am attempting to create a program that can compare two different calendar styles to each other. Obviously the Gregorian calendar that we use on a daily basis and a Kemetic (Ancient Egyptian) calendar. The Kemetic calendar consists of 3 seasons with four months each and 30 days to each month. The first four months are Akhet, the second four are Peret, the final four are Shomu. Additionally there is a micro-month (Intercalary) that consists of 5 days, 6 during a leap year. A new Kemetic year starts on Aug 5th, or Aug 6th during a leap year.
The code is working to have the two calendars running side-by-side, however it is the part where I am trying to have them work together or translate from A-to-B where I could use some assistance. E.g. Today is 03 July 2021, what is the Kemetic date?
The first part of it is posted here, the rest at PasteBin: https://pastebin.com/uDNsbCJd
require 'date'
class KemeticDate
##kem_months = ['IAkhet', 'IIAkhet', 'IIIAkhet', 'IVAkhet',
'IPeret', 'IIPeret', 'IIIPeret', 'IVPeret',
'IShomu', 'IIShomu', 'IIIShomu', 'IVShomu',
'Intercalary']
##jul_months = ['January', 'February', 'March',
'April', 'May', 'June', 'July',
'August', 'September', 'October',
'November', 'December']
end
Related
I am trying to get the difference between two dates in months for an xPath.
I have no problems getting it in days (1127)
days-from-duration(xs:date('2012-06-17')-xs:date('2009-05-17'))
When I try doing it in months I get 0
months-from-duration(xs:date('2012-06-17')-xs:date('2009-05-17'))
I did notice that this comes back as days only ("P1126D") so that maybe the problem just not sure how to fix it.
xs:date('2012-06-17')-xs:date('2009-05-17')
Thanks for any help!
So the best thing I can seem to do is manually calculate it.
(year-from-date(xs:date('2012-06-17')) - year-from-date(xs:date('2012-05-18')))*12 + month-from-date(xs:date('2012-06-17')) -month-from-date(xs:date('2012-05-18')) + (if (day-from-date(xs:date('2012-06-17')) < day-from-date(xs:date('2012-05-18')) ) then -1 else 0)
This function was not included in the spec because we couldn't agree semantics for it. We heard arguments that the difference between 31 March 2015 and 30 Sept 2015 was six months, and arguments that it was five months. (Advice: negative differences seem to be even more problematic than positive differences.) You can easily find that a date plus 6 months minus 6 months is not the date where you started. You'll have to define what you think the answer should be, and implement it using lower-level facilities such as month-from-date and year-from-date.
The problem is that days-from-duration returns the days field -- not the number of days the duration includes. Likewise for months and years.
Now, you can divide by days, but not by months (since months have uneven boundaries):
(: arg1 here is a dayTimeDuration, which can't be easily converted to a yearMonthDuration :)
let $arg1 := xs:date('2012-06-17')-xs:date('2009-05-17')
return xs:dayTimeDuration($arg1) div xs:dayTimeDuration("P30D")
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
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
Drools seems to support only days, hours, minutes, seconds and milliseconds for the units of time used with Temporal Operators. I am working on a rule that looks for people within a particular age group. For example: between 6 months and 5 years, younger than 18 years, older than 12 years etc..
My person Class has a dateOfBirth instance variable, but no person.age method to do a direct comparison like:
$p : Person(age <= 18)
I don't have too much liberty to modify the Person Class and I am trying to avoid writing utils methods and using 'eval' for comparing, as 'evals' are not optimized. I am a drools newbie and have written the following rule.
rule "Under18Years"
when
now : RuleTime()
$p : Person(dateOfBirth after[-6570d, 0s] $now )
then
System.out.println("Under18Years fired");
end
I know this isn't very accurate as I just used 6570 (365*18) days; ignoring the leap years. I might be better off using seconds in a standard SI year (31,556,926) times 18 to account for 18 years, but is there a better way? This doesn't work for conditions involving months either. Does anyone have any other ideas/solution to this problem?
You can create a package or a function that does the above
public int getAge() {
Years years = Years.yearsBetween(dateOfBirth, currentDate);
return years.getYears();
For aslong as i have been using drools there has allways been years available to call upon. i see this was posted a while ago and maybe you have found out since.
mainly I just need to know what the commands are to get month, day and year, first day of month and last day of month. I should be able to figure it out from there. I've built a nice one in PHP but I would rather use ruby since using a database is so much easier in ruby.
So if you can point me in the right direction.
ActiveSupport (part of the Rails framework, but can be used separately) extends the standard library Date class with features fit for your purpose. If you don't have Rails already installed, just install ActiveSupport:
gem install activesupport
Then in your code you can use it like this:
require 'rubygems'
require 'active_support'
# Get today's date
today = Date.today
# These are in the standard library Date class
today.year
=> 2010
today.month
=> 7
today.day
=> 10
today.wday
=> 6
# These are added by ActiveSupport
date = today.beginning_of_month
=> Thu Jul 01 2010
date.end_of_month
=> Sat Jul 31 2010
date.prev_month
=> Tue, 01 Jun 2010
date.next_month
=> Sun, 01 Aug 2010
date + 2.months
=> Wed, 01 Sep 2010
date - 2.months
=> Sat, 01 May 2010
See the documentation for details.
Why you want to create when there is already plugins for that ref CalendarDateSelect Demo for mini-calendar AND event_calendar for event showing in caledar format.
http://ruby-doc.org/core/classes/Date.html describes Ruby's Date class. The wday function will get the day-of-the-week of an arbitrary Date.
http://www.techotopia.com/index.php/Working_with_Dates_and_Times_in_Ruby describes how to use Dates in their entirety.
Also checkout the cal gem. It only does the basics for you so you can focus on building the calendar yourself. Stuff like determining the days in the calendar (usually a few days from the previous month, the days in the current month, and a few days from the next month) and starting the week on a day other than Sunday.