processi

about processes and engines

‘1y2w3d4s’ for 33004804 seconds

While working on the next version of Densha, I was looking for a library/gem/plugin that would turn time duration expressed in seconds into strings like “yesterday” or “2 weeks ago”. I want to state since how long a business process is running or how long a workitem stayed in a worklist.

In the gem rufus_scheduler (née ‘openwferu_scheduler’), there is already a parse_time_string method :

require "rubygems"
require "rufus/otime" 
    # sudo gem install rufus-scheduler

Rufus.parse_time_string('1y2w3d4s')
    # => 33004804.0

Going in the other direction, in today’s release 1.0.4 of the rufus-scheduler gem, I added the sister method, to_time_string, which takes as input a Float or an integer :

Rufus.to_duration_string 0                    
    # => '0s'
Rufus.to_duration_string 60                   
    # => '1m'
Rufus.to_time_string 3661                 
    # => '1h1m1s'
Rufus.to_time_string 7 * 24 * 3600        
    # => '1w'
Rufus.to_time_string 30 * 24 * 3600 + 1   
    # => "4w2d1s"
Rufus.to_time_string 0.120 + 30 * 24 * 3600 + 1    
    # => "4w2d1s120"

# the :drop_seconds and :months options in action :

Rufus.to_time_string(
    61.12 + 30 * 24 * 3600, :drop_seconds => true)   
    # =>"4w2d1m"
Rufus.to_time_string(
    61.12 + 30 * 24 * 3600, :months => true)    
    # =>"1M1m1s120"

# (please note that a month here is 30 days)

# there is a to_duration_hash method 
# behind the scenes :

Rufus.to_duration_hash(
    61.12 + 30 * 24 * 3600, :drop_seconds => true)   
    # => { :w => 4, :d => 2, :m => 1 }

(to_time_string is an alias to to_duration_string)

So instead of saying this business process has been started “two weeks ago”, my web interface will say it’s running since “2w”, it’s less pretty, but it will do for now.

Written by John Mettraux

February 29, 2008 at 9:10 am

Posted in ruby, rufus, scheduling

2 Responses

Subscribe to comments with RSS.

  1. Perhaps you may have overlooked the rails helper distance_in_time_from_now?

    I use it like:

    def created_at_distance
    distance_in_time_from_now(self.created_at)
    end

    E. James O'Kelly

    February 29, 2008 at 5:55 pm

  2. Hi James,

    yes, I have overlooked it. Thanks for the pointer.

    John

    John Mettraux

    March 1, 2008 at 1:35 am


Comments are closed.

%d bloggers like this: