Kronic

A dirt simple library for parsing and formatting human readable dates (Today, Yesterday, Last Monday). Both a ruby and a javascript implementation are included.

Usage

Ruby

gem install kronic

require 'kronic'
Kronic.parse("Today")     # => Date.today
Kronic.format(Date.today) # => "Today"

Supported formats: Today, yesterday, tomorrow, last thursday, this thursday, 14 Sep, 14 June 2010. Any dates without a year are assumed to be in the past (that’s what my app needs, so that’s what it does).

Javascript

Install by grabbing the latest versions direct from github:

curl http://github.com/xaviershay/kronic/raw/master/lib/js/kronic.js   > public/javascripts/kronic.js

Use with the following script:

<script src="/javascripts/kronic.js"   type="text/javascript"></script>
<script>
  Kronic.parse("Today");
  Kronic.format(new Date());
</script>

Why not Chronic or Tickle

Chronic and Tickle both parse a heap of formats. That’s not useful to me when they fail on cases I want (14 Sep), and also not useful because I don’t need times or spans. I just want to replace my calendar picker.

In addition, Kronic can take a date and give you a human readable form, neither of which Chronic nor Tickle does.

Oh yeah, Kronic is about 150 lines of ruby code with no dependencies, and provides a javascript implementation.

Compatibility

Kronic does not require ActiveSupport, but if it is present Kronic will use the current time zone. In short, everything should work as you would expect.

Kronic is tested on 1.8.7, 1.9.2, Rubinius 1.1, and JRuby 1.5.1.

Developing

git clone git://github.com/xaviershay/kronic.git
bundle install # May take a while, needs to install therubyracer/V8 to test JS
rake # Run the specs
# Dive into the code at spec/kronic_spec.rb

There are comments to help you along but it is pretty basic code, so everything should make sense pretty quickly. The javascript code is a direct translation of the ruby code, and as such it is undocumented - just refer back to the ruby code.

The exact same set of specs are run again the ruby and javascript libraries, so they should be 100% compatible.

Patches welcome! Fork and send a pull request. Please follow coding conventions already in use. If jsl (JSLint) is in your path, rake will run it over the javascript implementation. There are currently no warnings, please keep it that way.

Status

Current release is stable, and being used in production sites. Some I18n work is in yolk’s fork, but it is lagging behind the current release. I am not using this personally. Maybe also a flag to toggle the interpretation of dates without years would be nice, but not something I need.