LoginSignup
5
3

More than 5 years have passed since last update.

自然言語の日付、時間をパースする chronic gem を試す #ruby

Posted at

概要

自然言語の日付、時間をパースする chronic gem を試しました

インストール

$ gem install chronic

サンプル

サンプルコード

chronic.rb
require 'tbpgr_utils'
require 'chronic'

bulk_puts_eval binding, <<-EOS
Time.now
Chronic.parse('now')
Chronic.parse('tomorrow')
Chronic.parse("2015-04-27T22:00:00")
Chronic.parse("2015-04-27 4am")
Chronic.parse("2015-04-27 4pm")
Chronic.parse("2015/04/27 4pm")
Chronic.parse("2015.04.27 4pm")
Chronic.parse("three days ago")
Chronic.parse("three days after")
Chronic.parse('sunday', :context => :past)
Chronic.parse('sunday', :context => :future)
Chronic.parse('this wednesday 5:00')
Chronic.parse('this wednesday 5:00', :ambiguous_time_range => :none)
Chronic.parse('may 24th')
Chronic.parse('may 24th', :now => Time.local(2000, 1, 1))
Chronic.parse('may 24th', :guess => false)
Chronic.parse('6/4/2015')
Chronic.parse('6/4/2015', :endian_precedence => :little)
Chronic.parse('INVALID DATE')
EOS

__END__
下記はTbpgrUtils gemの機能
bulk_puts_eval

https://rubygems.org/gems/tbpgr_utils
https://github.com/tbpgr/tbpgr_utils
  • 出力確認
% ruby chronic.rb
Time.now                                                             # => 2015-04-27 22:34:17 +0900
Chronic.parse('now')                                                 # => 2015-04-27 22:34:17 +0900
Chronic.parse('tomorrow')                                            # => 2015-04-28 12:00:00 +0900
Chronic.parse("2015-04-27T22:00:00")                                 # => 2015-04-27 22:00:00 +0900
Chronic.parse("2015-04-27 4am")                                      # => 2015-04-27 04:00:00 +0900
Chronic.parse("2015-04-27 4pm")                                      # => 2015-04-27 16:00:00 +0900
Chronic.parse("2015/04/27 4pm")                                      # => 2015-04-27 16:00:00 +0900
Chronic.parse("2015.04.27 4pm")                                      # => 2015-04-27 16:00:00 +0900
Chronic.parse("three days ago")                                      # => 2015-04-24 22:34:17 +0900
Chronic.parse("three days after")                                    # => 2015-04-30 22:34:17 +0900
Chronic.parse('sunday', :context => :past)                           # => 2015-04-26 12:00:00 +0900
Chronic.parse('sunday', :context => :future)                         # => 2015-05-03 12:00:00 +0900
Chronic.parse('this wednesday 5:00')                                 # => 2015-04-29 17:00:00 +0900
Chronic.parse('this wednesday 5:00', :ambiguous_time_range => :none) # => 2015-04-29 05:00:00 +0900
Chronic.parse('may 24th')                                            # => 2015-05-24 12:00:00 +0900
Chronic.parse('may 24th', :now => Time.local(2000, 1, 1))            # => 2000-05-24 12:00:00 +0900
Chronic.parse('may 24th', :guess => false)                           # => 2015-05-24 00:00:00 +0900..2015-05-25 00:00:00 +0900
Chronic.parse('6/4/2015')                                            # => 2015-06-04 12:00:00 +0900
Chronic.parse('6/4/2015', :endian_precedence => :little)             # => 2015-04-06 12:00:00 +0900
Chronic.parse('INVALID DATE')                                        # => nil

:pencil: メモ

たぶん使うことはないだろう

:books: 外部資料

5
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
3