Relativity 0.0.1 released

relativity - time relative to a day, a week, a month, a quarter, a year

https://github.com/petervandenabeele/relativity

I could not find a pure Ruby implementation I liked of

  • a time, relative to a day (e.g. 09:30:20 on a random day)
  • a time, relative to a week (e.g. 09:30 on Mondays)
  • a time range, relative to a day (09:30 … 18:00 on a random day)
  • a time range, relative to a week (Monday 13:00 … 18:30 in a random
    week)

So, I started making ‘relativity’. The first class partially implemented
is
RelativeTime. Critique and reviews are very welcome. Testing is based
on rspec, design methodology tries to be red-green-refactor TDD.

If you know of a full solution in pure Ruby, then I am still interested

(I checked facets, rubygems for patterns with time, date, …, googled
for relevant terms, but failed to find a solution that seemed maintained
and complete). But maybe I did not search well enough?

Thanks,

Peter

On Jan 11, 2012, at 2:48 PM, Peter V. wrote:

So, I started making ‘relativity’. The first class partially implemented is
RelativeTime. Critique and reviews are very welcome. Testing is based
on rspec, design methodology tries to be red-green-refactor TDD.

If you know of a full solution in pure Ruby, then I am still interested …
(I checked facets, rubygems for patterns with time, date, …, googled
for relevant terms, but failed to find a solution that seemed maintained
and complete). But maybe I did not search well enough?

This looks useful to me. I have created a few helpers that were similar
in concept but nowhere near as nice as what you did with the library
here.

I am not aware of any other gems or libs that duplicate this task, so I
think you are the first!

cr

Does Chronos(GitHub - apeiros/chronos: A high level time library, dealing with date, time, durations, intervals and more) cover any of this?

I have long thought that Ruby is in dire need of solid, unified and full
blown Date/Time/Calendar (and then some) library. I know of a number of
libraries that cover different aspects but nothing that brings it all
together. There’s ActiveSupport, Chronos, RichUnits, Holidays, Chronic,
etc. i think Chronos was an endeavor to do this, in fact, but
development
stalled.

On Thu, Jan 12, 2012 at 7:34 PM, Chuck R. [email protected]
wrote:

  • a time range, relative to a week (Monday 13:00 … 18:30 in a random
    for relevant terms, but failed to find a solution that seemed maintained
    and complete). But maybe I did not search well enough?

This looks useful to me. I have created a few helpers that were similar in
concept but nowhere near as nice as what you did with the library here.

I am not aware of any other gems or libs that duplicate this task, so I
think you are the first!

Thanks for that feedback. I plan on continuing coming
weekend with the “Range” feature.

Also, in version 0.0.3 that is now online, I changed the concept more
fundamentally to #{"period"Time} and #{"period"Day}:

  • DayTime (a time inside a “cycle” of 1 day, think %)
  • planning also this
  • WeekTime (a time inside a “cycle” of 1 week, no start_of_week needed,
    just time + day names)
  • WeekDay (a day inside a “cycle” of 1 week, no start_of_week
    (cyclical),
    just a day names)
  • QuarterDay etc.
  • DayTimeRange
  • WeekTimeRange
  • WeekDayRange
    etc.

Will be really useful to code things like:

bar.opening_hours = WeekTimeMultiRange.new <<
WeekTimeRange.new(“Monday 09:00 until Monday 18:00”) <<
WeekTimeRange.new(“Tuesday 13:00 until Tuesday 21:30”) <<
WeekTimeRange.new(WeekTime.new(“Wednesday 19:00”),
WeekTime.new(“Thursday
02:00”)) <<
WeekTimeRange.new(“Thursday 22:00 until Friday 02:00”) <<
WeekTimeRange.new(“Sunday 22:00 until Monday 02:00”) # seeming
“overflow” but no issue since cyclical

if bar.opening_hours.include?(Time.now)
display_open
end

workdays = WeekDayRange.new(“Monday until Friday”)

if workdays.include?(Time.now)
go_to_work
end

Also, what I really want to do is an abstract presentation of
the “last day of the month” (independent of the which month
we are actually in, just the abstract concept).

first_day = MonthDay.new(:first)
first_day = MonthDay.new(1) # in days, the first is 1 (not 0) … Pascal
anyone :wink:

last_day = MonthDay.new(:last)
last_day = MonthDay.new(0) # logically, the last one then becomes 0 in a
cyclic counter
before_last_day = MonthDay.new(-1) # E.g. 30 Dec is a before_last_day
pay_day = MonthDay.new(-5) # E.g. 26 Jan would be pay_day in monthly
scheme

if pay_day.today?
prepare_paycheck
end

Similar:

reporting_day = QuarterDay.new(+15)
blocked_period = QuarterDayRange(reporting_day-28, reporting_day+1)

if reporting_day.today?
publish_quarterly_reports
end

if blocked_period.today?
forbid_employee_stock_transactions
end

I know ActiveSupport has support for certain aspects, but I dont think
it is
as abstract as I want it. IIRC , ActiveSupport works on actual Date
instances
(which are not “relative” but anchored to a specific absolute time or
date).
I did not see this abstract concept of the “last day of the month” or
“a Friday in a random week” fully implemented.

Thanks for any feedback,

Peter

On Fri, Jan 13, 2012 at 8:28 PM, Intransition [email protected]
wrote:

Does Chronos(GitHub - apeiros/chronos: A high level time library, dealing with date, time, durations, intervals and more) cover any of this?

Thanks for the pointer.

I checked it out (quickly) and as far as I can see:

  • times, dates, ranges are all “absolute”
  • duration is the only “relative” class
    (and that seems to be a "length of time, expressed in a number of days
  • a number ofpicoseconds").

So, I did not find back the most important aspect of what I need,
a time or a day “relative” to day, week, forthnight, etc.

On top, every range can be cyclical, so there is no <=> operator.

Both the “09:00 until 18:00” and the “20:00 until 06:00” range
are valid (think day time job and night shift). Same for
days: does Monday come before or after Friday? Before if one
thinks “workweek”, after if one thinks “weekend”.

I have long thought that Ruby is in dire need of solid, unified and full
blown Date/Time/Calendar (and then some) library. I know of a number of
libraries that cover different aspects but nothing that brings it all
together. There’s ActiveSupport, Chronos, RichUnits, Holidays, Chronic,
etc. i think Chronos was an endeavor to do this, in fact, but development
stalled.

I plan to continue just with the specific concept of a “relative”
time and then we can see if it makes sense to take on a
broader aspect of “Time management” (pun intended).

Peter