DSL for expressing and comparing dates and times
Let's start with a simple example...
This produces a
Date instance representing 72 hours before the present.
Neat. What about the future?
1minutefrom_now # 1..minute.from_now
Easy! Note the use of
minute rather than
minutes. The two are synonymous;
singular and plural properties exist for each of the supported units.
Decimals? You betcha:
1.5hoursago # 1.5.hours.ago
What about dates relative to other points in time?
tomorrow = 1dayfrom_now # var tomorrow = 1..day.from_nowhalloween = '31 October 2011' # var halloween = new Date('31 October 2011')christmas = '25 December 2011' # var christmas = new Date('25 December 2011')#1weekfrom tomorrow # 1..week.from(tomorrow)#2daysafter halloween # 2..days.after(halloween)#1weekbefore christmas # 1..week.before(christmas)
after are synonymous; use whichever reads better.
This is pleasing, you may be thinking, but I'd never say “one week from tomorrow” – it sounds a bit stiff.
Well, if you must...
a = an = 1 # var a = 1, an = 1#aweekfrom tomorrow # a.week.from(tomorrow)#afortnightfrom_now # a.fortnight.from_now#anhourago # an.hour.ago
Oh, and I should mention, you can add
NaturalDate instances using the
anhourand58minutesfrom_now # an.hour.and(58..minutes).from_now#11hoursand36minutesand9secondsago # 11..hours.and(36..minutes).and(9..seconds).ago
Can you help me with date comparison? To determine whether an event occurred more than a week ago I have to ask whether its numeric representation is less than that of "a week ago". It makes my head hurt.
Perhaps you find this more natural?
A practical example:
user_registered = dbgetidregistration_date$'#tips'show if user_registeredless_than15minutesago
after can follow
apply_late_fee if costume_returnedmore_than2daysafter halloween
There's also an
either_side_of method which does what it says on the tin:
unfortunate = birthdayless_than3dayseither_side_of christmas
That just about covers it.
make setup make test
To be clear, an.hour.ago fiddles with
Two properties are added to
The following properties are added to
Each of these properties has a "getter" which returns a
The rest of the methods (
and) are attached to
NaturalDate.prototype (which is not exposed).