• Stars
    star
    3,223
  • Rank 13,340 (Top 0.3 %)
  • Language
    Ruby
  • License
    MIT License
  • Created over 16 years ago
  • Updated 7 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Chronic is a pure Ruby natural language date parser.

Chronic

Chronic is a natural language date/time parser written in pure Ruby. See below for the wide variety of formats Chronic will parse.

Installation

$ gem install chronic

Usage

require 'chronic'

Time.now   #=> Sun Aug 27 23:18:25 PDT 2006

Chronic.parse('tomorrow')
  #=> Mon Aug 28 12:00:00 PDT 2006

Chronic.parse('monday', :context => :past)
  #=> Mon Aug 21 12:00:00 PDT 2006

Chronic.parse('this tuesday 5:00')
  #=> Tue Aug 29 17:00:00 PDT 2006

Chronic.parse('this tuesday 5:00', :ambiguous_time_range => :none)
  #=> Tue Aug 29 05:00:00 PDT 2006

Chronic.parse('may 27th', :now => Time.local(2000, 1, 1))
  #=> Sat May 27 12:00:00 PDT 2000

Chronic.parse('may 27th', :guess => false)
  #=> Sun May 27 00:00:00 PDT 2007..Mon May 28 00:00:00 PDT 2007

Chronic.parse('6/4/2012', :endian_precedence => :little)
  #=> Fri Apr 06 00:00:00 PDT 2012

Chronic.parse('INVALID DATE')
  #=> nil

If the parser can find a date or time, either a Time or Chronic::Span will be returned (depending on the value of :guess). If no date or time can be found, nil will be returned.

See Chronic.parse for detailed usage instructions.

Examples

Chronic can parse a huge variety of date and time formats. Following is a small sample of strings that will be properly parsed. Parsing is case insensitive and will handle common abbreviations and misspellings.

Simple

  • thursday
  • november
  • summer
  • friday 13:00
  • mon 2:35
  • 4pm
  • 10 to 8
  • 10 past 2
  • half past 2
  • 6 in the morning
  • friday 1pm
  • sat 7 in the evening
  • yesterday
  • today
  • tomorrow
  • last week
  • next week
  • this tuesday
  • next month
  • last winter
  • this morning
  • last night
  • this second
  • yesterday at 4:00
  • last friday at 20:00
  • last week tuesday
  • tomorrow at 6:45pm
  • afternoon yesterday
  • thursday last week

Complex

  • 3 years ago
  • a year ago
  • 5 months before now
  • 7 hours ago
  • 7 days from now
  • 1 week hence
  • in 3 hours
  • 1 year ago tomorrow
  • 3 months ago saturday at 5:00 pm
  • 7 hours before tomorrow at noon
  • 3rd wednesday in november
  • 3rd month next year
  • 3rd thursday this september
  • 4th day last week
  • fourteenth of june 2010 at eleven o'clock in the evening
  • may seventh '97 at three in the morning

Specific Dates

  • January 5
  • 22nd of june
  • 5th may 2017
  • February twenty first
  • dec 25
  • may 27th
  • October 2006
  • oct 06
  • jan 3 2010
  • february 14, 2004
  • february 14th, 2004
  • 3 jan 2000
  • 17 april 85
  • 5/27/1979
  • 27/5/1979
  • 05/06
  • 1979-05-27
  • Friday
  • 5
  • 4:00
  • 17:00
  • 0800

Specific Times (many of the above with an added time)

  • January 5 at 7pm
  • 22nd of june at 8am
  • 1979-05-27 05:00:00
  • 03/01/2012 07:25:09.234567
  • 2013-08-01T19:30:00.345-07:00
  • 2013-08-01T19:30:00.34-07:00
  • etc

Time Zones

Chronic allows you to set which Time class to use when constructing times. By default, the built in Ruby time class creates times in your system's local time zone. You can set this to something like ActiveSupport's TimeZone class to get full time zone support.

>> Time.zone = "UTC"
>> Chronic.time_class = Time.zone
>> Chronic.parse("June 15 2006 at 5:45 AM")
=> Thu, 15 Jun 2006 05:45:00 UTC +00:00

Limitations

Chronic uses Ruby's built in Time class for all time storage and computation. Because of this, only times that the Time class can handle will be properly parsed. Parsing for times outside of this range will simply return nil. Support for a wider range of times is planned for a future release.

Contribute

If you'd like to hack on Chronic, start by forking the repo on GitHub:

https://github.com/mojombo/chronic

The best way to get your changes merged back into core is as follows:

  1. Clone down your fork
  2. Create a thoughtfully named topic branch to contain your change
  3. Install the development dependencies by running bundle install
  4. Hack away
  5. Add tests and make sure everything still passes by running bundle exec rake
  6. Ensure your tests pass in multiple timezones. ie TZ=utc bundle exec rake TZ=BST bundle exec rake
  7. If you are adding new functionality, document it in the README
  8. Do not change the version number, we will do that on our end
  9. If necessary, rebase your commits into logical chunks, without errors
  10. Push the branch up to GitHub
  11. Send a pull request for your branch

More Repositories

1

god

Ruby process monitor
Ruby
2,207
star
2

grit

**Grit is no longer maintained. Check out libgit2/rugged.** Grit gives you object oriented read/write access to Git repositories via Ruby.
Ruby
1,966
star
3

clippy

Clippy is a very simple Flash widget that makes it possible to place arbitrary text onto the client's clipboard.
940
star
4

mojombo.github.io

Jekyll source for my personal blog.
HTML
929
star
5

tpw

698
star
6

proxymachine

A simple TCP routing proxy built on EventMachine that lets you configure the routing logic in Ruby.
Ruby
580
star
7

ernie

Ernie is an Erlang/Ruby BERT-RPC Server.
Erlang
460
star
8

erlectricity

Erlectricity exposes Ruby to Erlang and vice versa.
Ruby
347
star
9

tomdoc

A flexible code documentation specification with human readers in mind.
333
star
10

primer

Primer is a Flash-like API built on top of Canvas using jQuery.
JavaScript
280
star
11

mustache.erl

Mustache template engine for Erlang.
Erlang
239
star
12

bert

BERT (Binary ERlang Term) serialization library for Ruby.
Ruby
206
star
13

github-flavored-markdown

GitHub's Flavor of Markdown
182
star
14

bertrpc

BERTRPC is a Ruby BERT-RPC client library.
Ruby
162
star
15

sf-vegetarian-restaurants

A list of awesome vegetarian-friendly restaurants in SF
141
star
16

rakegem

Simple gem and release management with customizable Rake tasks.
Ruby
131
star
17

egitd

The Erlang git-daemon
Erlang
115
star
18

bert.erl

Erlang BERT encoder/decoder
Erlang
98
star
19

asteroids

Destroy your Atom editor, Asteroids style!
JavaScript
94
star
20

gollum-demo

Gollum test repo
Perl
75
star
21

rebar

Ruby to Erlang Bridge And Runner
Erlang
63
star
22

yaws

YAWS is an erlang web server
Erlang
43
star
23

omniship

Ruby API for Shipper APIs (UPS, FedEx, DHL)
Ruby
37
star
24

glowstick

A realtime, OpenGL graphing library for Ruby
Ruby
36
star
25

mastering-git-basics

A presentation on Git basics using ShowOff
JavaScript
35
star
26

octobeer

The GitHub Kegerator
26
star
27

cubesixel

A pixel font (native size 7x7) I made in a different life
26
star
28

vanhelsing

Super streamlined memory profiler with real time graphs for Ruby programs
Ruby
23
star
29

erlang_pipe

A pipe implementation in pure Erlang
Erlang
21
star
30

fixture-scenarios

This plugin allows you to create 'scenarios' which are collections of fixtures and ruby files that represent a context against which you can run tests.
Ruby
18
star
31

v8

C++
18
star
32

scoped

Scope access for multiple services in Ruby. Warning: OLDSCHOOL CODE!
Ruby
10
star
33

pyberry

A tool for systems analysis.
10
star
34

30daysoflaptops.github.io

CSS
8
star
35

mojombo.github.com

Old location of Jekyll source for tom.preston-werner.com
6
star
36

conceptual_algorithms

5
star
37

erlectricity-presentation

5
star
38

endo

Test repo for talk.
4
star
39

fakegem

This is a fake gem so I can test GitHub's gem building when I need to
Ruby
4
star
40

mojombo

3
star