# \[ANN\] relativity 0.0.1 released

**URL:** <https://rubytalk.org/t/ann-relativity-0-0-1-released/64573>\
**Category:** ruby-talk\
**Created:** [11 January 2012 20:48 UTC](https://rubytalk.org/t/ann-relativity-0-0-1-released/64573 "2012-01-11T20:48:11Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Peter\_Vandenabeele1](https://avatars.discourse-cdn.com/v4/letter/p/59ef9b/32.png) [@Peter\_Vandenabeele1](https://rubytalk.org/u/Peter_Vandenabeele1)\
**Post date:** [11 January 2012 20:48 UTC](https://rubytalk.org/t/ann-relativity-0-0-1-released/64573/1 "2012-01-11T20:48:11Z")

</div>

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

&nbsp;&nbsp;&nbsp;&nbsp;[https://github.com/petervandenabeele/relativity](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

> **···**
>
> --  
> Peter Vandenabeele  
> [http://twitter.com/peter\_v](http://twitter.com/peter_v)  
> [http://rails.vandenabeele.com](http://rails.vandenabeele.com)

---

<div class="post-metadata">

**Author:** ![Chuck\_Remes](https://avatars.discourse-cdn.com/v4/letter/c/3be4f8/32.png) [@Chuck\_Remes](https://rubytalk.org/u/Chuck_Remes)\
**Post date:** [12 January 2012 18:34 UTC](https://rubytalk.org/t/ann-relativity-0-0-1-released/64573/2 "2012-01-12T18:34:39Z")

</div>

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

> **···**
>
> On Jan 11, 2012, at 2:48 PM, Peter Vandenabeele wrote:
> 
> > relativity - time relative to a day, a week, a month, a quarter, a year
> > 
> > &nbsp;&nbsp;&nbsp;[https://github.com/petervandenabeele/relativity](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?

---

<div class="post-metadata">

**Author:** ![7rans](https://avatars.discourse-cdn.com/v4/letter/7/a8b319/32.png) [@7rans](https://rubytalk.org/u/7rans)\
**Post date:** [13 January 2012 19:28 UTC](https://rubytalk.org/t/ann-relativity-0-0-1-released/64573/3 "2012-01-13T19:28:24Z")

</div>

Does Chronos([https://github.com/apeiros/chronos](https://github.com/apeiros/chronos)) 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.

---

<div class="post-metadata">

**Author:** ![Peter\_Vandenabeele1](https://avatars.discourse-cdn.com/v4/letter/p/59ef9b/32.png) [@Peter\_Vandenabeele1](https://rubytalk.org/u/Peter_Vandenabeele1)\
**Post date:** [12 January 2012 22:27 UTC](https://rubytalk.org/t/ann-relativity-0-0-1-released/64573/4 "2012-01-12T22:27:07Z")

</div>

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 \<\<  
&nbsp;&nbsp;WeekTimeRange.new("Monday 09:00 until Monday 18:00") \<\<  
&nbsp;&nbsp;WeekTimeRange.new("Tuesday 13:00 until Tuesday 21:30") \<\<  
&nbsp;&nbsp;WeekTimeRange.new(WeekTime.new("Wednesday 19:00"), WeekTime.new("Thursday  
02:00")) \<\<  
&nbsp;&nbsp;WeekTimeRange.new("Thursday 22:00 until Friday 02:00") \<\<  
&nbsp;&nbsp;WeekTimeRange.new("Sunday 22:00 until Monday 02:00") # seeming  
"overflow" but no issue since cyclical

if bar.opening\_hours.include?(Time.now)  
&nbsp;&nbsp;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 😉

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?  
&nbsp;&nbsp;prepare\_paycheck  
end

Similar:

reporting\_day = QuarterDay.new(+15)  
blocked\_period = QuarterDayRange(reporting\_day-28, reporting\_day+1)

if reporting\_day.today?  
&nbsp;&nbsp;publish\_quarterly\_reports  
end

if blocked\_period.today?  
&nbsp;&nbsp;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 Thu, Jan 12, 2012 at 7:34 PM, Chuck Remes \<cremes.devlist@mac.com\> wrote:
> 
> > On Jan 11, 2012, at 2:48 PM, Peter Vandenabeele wrote:
> > 
> > \> relativity - time relative to a day, a week, a month, a quarter, a year  
> > \>  
> > \> [https://github.com/petervandenabeele/relativity](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?
> > 
> > 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!
> 
> --  
> Peter Vandenabeele  
> [http://twitter.com/peter\_v](http://twitter.com/peter_v)  
> [http://rails.vandenabeele.com](http://rails.vandenabeele.com)

---

<div class="post-metadata">

**Author:** ![Peter\_Vandenabeele1](https://avatars.discourse-cdn.com/v4/letter/p/59ef9b/32.png) [@Peter\_Vandenabeele1](https://rubytalk.org/u/Peter_Vandenabeele1)\
**Post date:** [14 January 2012 00:50 UTC](https://rubytalk.org/t/ann-relativity-0-0-1-released/64573/5 "2012-01-14T00:50:32Z")

</div>

> Does Chronos([https://github.com/apeiros/chronos\](https://github.com/apeiros/chronos%5C)) 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  
&nbsp;&nbsp;(and that seems to be a "length of time, expressed in a number of days +  
&nbsp;&nbsp;&nbsp;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

> **···**
>
> On Fri, Jan 13, 2012 at 8:28 PM, Intransition \<transfire@gmail.com\> wrote:
> 
> --  
> Peter Vandenabeele  
> [http://twitter.com/peter\_v](http://twitter.com/peter_v)  
> [http://rails.vandenabeele.com](http://rails.vandenabeele.com)
