Skip to content
/ kronic Public
forked from xaviershay/kronic

A dirt simple library for parsing and formatting human readable dates

License

Notifications You must be signed in to change notification settings

staqapp/kronic

 
 

Repository files navigation

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 https://raw.github.com/xaviershay/kronic/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 Ruby 2.0+.

Developing

git clone git://github.com/xaviershay/kronic.git
bundle install # May take a while, needs to install therubyracer/V8 to test JS
bin/test       # Run the tests
# 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.

About

A dirt simple library for parsing and formatting human readable dates

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Ruby 72.1%
  • JavaScript 27.3%
  • Shell 0.6%