tokiyomi 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +40 -0
- data/Rakefile +1 -0
- data/lib/tokiyomi.rb +10 -0
- data/lib/tokiyomi/relative_time.rb +50 -0
- data/lib/tokiyomi/version.rb +3 -0
- data/spec/tokiyomi_relative_time_spec.rb +25 -0
- data/spec/tokiyomi_spec.rb +6 -0
- data/tokiyomi.gemspec +23 -0
- metadata +106 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 moro
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
# Tokiyomi
|
2
|
+
|
3
|
+
Japanese relative date/time string parser.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'tokiyomi'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install tokiyomi
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
```
|
22
|
+
Time.now # => 2013-01-10T12:34:56
|
23
|
+
Tokiyomi.parse('10日前') # => 2012-12-31T12:34:56
|
24
|
+
```
|
25
|
+
|
26
|
+
### Supported formats
|
27
|
+
|
28
|
+
* 年, 月, 日, 時間, 分, 秒 + [前後]
|
29
|
+
* eg: 10日前
|
30
|
+
* hour and minutes are also able to specified.
|
31
|
+
* eg: 2ヵ月前の20:00
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
1. Fork it
|
36
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
37
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
38
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
39
|
+
5. Create new Pull Request
|
40
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/lib/tokiyomi.rb
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
require 'active_support/core_ext'
|
3
|
+
|
4
|
+
module Tokiyomi
|
5
|
+
class RelativeTime
|
6
|
+
UNITS = {
|
7
|
+
'年' => :years,
|
8
|
+
'月' => :months,
|
9
|
+
'日' => :days,
|
10
|
+
'時間' => :hours,
|
11
|
+
'分' => :minutes,
|
12
|
+
'秒' => :seconds,
|
13
|
+
}.freeze
|
14
|
+
|
15
|
+
RELATIVE_TIME = /(\d+)(#{UNITS.keys.join('|')})(前|後)(?:の(\d{2}):(\d{2}))?/
|
16
|
+
|
17
|
+
def self.readable?(str)
|
18
|
+
str =~ RELATIVE_TIME
|
19
|
+
end
|
20
|
+
|
21
|
+
attr_reader :duration, :unit, :direction, :hour_min
|
22
|
+
|
23
|
+
def initialize(str)
|
24
|
+
raise ArgumentError, "can't understand `#{str}'" unless self.class.readable?(str)
|
25
|
+
|
26
|
+
init_from_dynamic_value(str)
|
27
|
+
end
|
28
|
+
|
29
|
+
def calculate(base)
|
30
|
+
datetime = duration.send(unit).send(direction, base)
|
31
|
+
|
32
|
+
hour_min_fixable? ? datetime.change(hour_min) : datetime
|
33
|
+
end
|
34
|
+
|
35
|
+
private
|
36
|
+
|
37
|
+
def hour_min_fixable?
|
38
|
+
hour_min && unit.in?([:years, :months, :days])
|
39
|
+
end
|
40
|
+
|
41
|
+
def init_from_dynamic_value(str)
|
42
|
+
duration, unit, direction, *hour_min= str.scan(RELATIVE_TIME).first
|
43
|
+
|
44
|
+
@duration = duration.to_i
|
45
|
+
@unit = UNITS[unit]
|
46
|
+
@direction = direction == '前' ? :ago : :since
|
47
|
+
@hour_min = hour_min.all?(&:nil?) ? nil : Hash[[:hour, :min].zip(hour_min.map(&:to_i))]
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
require 'time'
|
3
|
+
require 'tokiyomi/relative_time'
|
4
|
+
|
5
|
+
describe Tokiyomi::RelativeTime do
|
6
|
+
let(:now) { Time.local(2013, 1, 10, 12, 34, 56) }
|
7
|
+
|
8
|
+
def rel_time(str)
|
9
|
+
Tokiyomi::RelativeTime.new(str)
|
10
|
+
end
|
11
|
+
|
12
|
+
RSpec::Matchers.define :be_calculated_to do |expect|
|
13
|
+
match do |relative_time|
|
14
|
+
relative_time.calculate(now).should == Time.parse(expect)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
specify { expect(rel_time('3日前')).to be_calculated_to('2013/01/07 12:34:56') }
|
19
|
+
specify { expect(rel_time('10分後')).to be_calculated_to('2013/01/10 12:44:56') }
|
20
|
+
|
21
|
+
specify { expect(rel_time('2日後の20:00')).to be_calculated_to('2013/01/12 20:00:00') }
|
22
|
+
|
23
|
+
specify { expect { rel_time('いつか').to raise_error(ArgumentError) } }
|
24
|
+
specify { expect { rel_time('1日後の10:').to raise_error(ArgumentError) } }
|
25
|
+
end
|
data/tokiyomi.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'tokiyomi/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "tokiyomi"
|
8
|
+
gem.version = Tokiyomi::VERSION
|
9
|
+
gem.authors = ["moro"]
|
10
|
+
gem.email = ["moronatural@gmail.com"]
|
11
|
+
gem.description = %q{Japanese relative date/time string parser.}
|
12
|
+
gem.summary = %q{Japanese relative date/time string parser.}
|
13
|
+
gem.homepage = ""
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
gem.add_dependency 'activesupport'
|
20
|
+
|
21
|
+
gem.add_development_dependency 'rake'
|
22
|
+
gem.add_development_dependency 'rspec'
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,106 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: tokiyomi
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- moro
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-01-08 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activesupport
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rake
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rspec
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
description: Japanese relative date/time string parser.
|
63
|
+
email:
|
64
|
+
- moronatural@gmail.com
|
65
|
+
executables: []
|
66
|
+
extensions: []
|
67
|
+
extra_rdoc_files: []
|
68
|
+
files:
|
69
|
+
- .gitignore
|
70
|
+
- Gemfile
|
71
|
+
- LICENSE.txt
|
72
|
+
- README.md
|
73
|
+
- Rakefile
|
74
|
+
- lib/tokiyomi.rb
|
75
|
+
- lib/tokiyomi/relative_time.rb
|
76
|
+
- lib/tokiyomi/version.rb
|
77
|
+
- spec/tokiyomi_relative_time_spec.rb
|
78
|
+
- spec/tokiyomi_spec.rb
|
79
|
+
- tokiyomi.gemspec
|
80
|
+
homepage: ''
|
81
|
+
licenses: []
|
82
|
+
post_install_message:
|
83
|
+
rdoc_options: []
|
84
|
+
require_paths:
|
85
|
+
- lib
|
86
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
87
|
+
none: false
|
88
|
+
requirements:
|
89
|
+
- - ! '>='
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: '0'
|
92
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
93
|
+
none: false
|
94
|
+
requirements:
|
95
|
+
- - ! '>='
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '0'
|
98
|
+
requirements: []
|
99
|
+
rubyforge_project:
|
100
|
+
rubygems_version: 1.8.23
|
101
|
+
signing_key:
|
102
|
+
specification_version: 3
|
103
|
+
summary: Japanese relative date/time string parser.
|
104
|
+
test_files:
|
105
|
+
- spec/tokiyomi_relative_time_spec.rb
|
106
|
+
- spec/tokiyomi_spec.rb
|