Tuesday, June 24, 2014

clj-relative-time 0.1.1

Running automated reports and queries often involves date ranges. Clj-relative-time library translates a human readable relative date period string into absolute begin and end dates. This library is based on clj-time.

Supported vocabulary

["this" "last" "prev"] ; minor term
[1 2 3 4 ...]  ; middle term, an integer
["day" "week" "4week" "month" "calmonth" "quarter" "half" "year"] ; major term

Usage

git clone https://github.com/loganis/clj-relative-time.git
cd clj-relative-time
lein test
(:use clj-relative-time.core)
(period2begend "last_2_weeks") ; {:beg "2014-06-16", :end "2014-06-23"}

;;Example definitions

;;Last week:
    {:beg (-> now (dminus (-> 7 (* middle) (ddays))) (dformat))
     :end (-> now (dminus (-> 1 (ddays))) (dformat))}
;;Last calendar month
    {:beg (-> now (dminus (-> middle (dmonths)))
                  (dminus (-> (- dom 1) (ddays))) (dformat))
     :end (-> now (dminus (-> dom (ddays))) (dformat))}

Sample test output

...
{:last_1_day {:beg "2014-06-23", :end "2014-06-23"}}
{:last_1_week {:beg "2014-06-17", :end "2014-06-23"}}
{:last_1_4week {:beg "2014-05-27", :end "2014-06-23"}}
{:last_1_month {:beg "2014-05-27", :end "2014-06-23"}}
{:last_1_calmonth {:beg "2014-05-01", :end "2014-05-31"}}
{:last_1_quarter {:beg "2014-01-01", :end "2014-03-31"}}
{:last_1_half {:beg "2013-07-01", :end "2013-12-31"}}
{:last_1_year {:beg "2013-01-01", :end "2013-12-31"}}
{:last_2_day {:beg "2014-06-22", :end "2014-06-23"}}
{:last_2_week {:beg "2014-06-10", :end "2014-06-23"}}
{:last_2_4week {:beg "2014-04-29", :end "2014-06-23"}}
{:last_2_month {:beg "2014-04-29", :end "2014-06-23"}}
{:last_2_calmonth {:beg "2014-04-01", :end "2014-05-31"}}
{:last_2_quarter {:beg "2013-10-01", :end "2014-03-31"}}
{:last_2_half {:beg "2013-01-01", :end "2013-12-31"}}
{:last_2_year {:beg "2012-01-01", :end "2013-12-31"}}
{:last_3_day {:beg "2014-06-21", :end "2014-06-23"}}
{:last_3_week {:beg "2014-06-03", :end "2014-06-23"}}
{:last_3_4week {:beg "2014-04-01", :end "2014-06-23"}}
{:last_3_month {:beg "2014-04-01", :end "2014-06-23"}}
{:last_3_calmonth {:beg "2014-03-01", :end "2014-05-31"}}
{:last_3_quarter {:beg "2013-07-01", :end "2014-03-31"}}
{:last_3_half {:beg "2012-07-01", :end "2013-12-31"}}
{:last_3_year {:beg "2011-01-01", :end "2013-12-31"}}
...

Features

  • Letter case and plural insensitive ("This_2_Week" and "this_2_weeks" are the same)
  • Space also can be used ("This 2 Weeks" and "this_2_week" are the same)
  • Source code is hosted on Github.com
  • Distributed under the Eclipse Public License, the same as Clojure.

Your feedback is welcome.

No comments:

Post a Comment