night-time 0.0.1 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README CHANGED
@@ -1,5 +1,5 @@
1
1
  NAME
2
- An extension to Time class that supports out of ranged values
2
+ NightTime behaves like Time but it also supports out of ranged values
3
3
 
4
4
  SYNOPSIS
5
5
  Native "mktime" method is too strict.
@@ -7,25 +7,41 @@ SYNOPSIS
7
7
  Time.mktime(2010, 1, 10, 26, 40)
8
8
  => ArgumentError: argument out of range
9
9
 
10
+ Time.mktime(2010, 2, 32)
11
+ => ArgumentError: argument out of range
12
+
10
13
  We the midnight workers want flexible one.
11
14
 
12
15
  require 'night-time'
13
- Time.mktime(2010, 1, 10, 26, 40)
16
+ NightTime.mktime(2010, 1, 10, 26, 40)
14
17
  => Mon Jan 11 02:40:00 +0900 2010
15
18
 
16
- Time.parse('2010-01-10 26:40')
19
+ NightTime.parse('2010-01-10 26:40')
17
20
  => Mon Jan 11 02:40:00 +0900 2010
18
21
 
22
+ NightTime.mktime(2010, 2, 32)
23
+ => Thu Mar 04 00:00:00 +0900 2010
24
+
19
25
  w00t!
20
26
 
21
27
  DESCRIPTION
22
- Modified methods are
28
+ * NightTime.mktime(*args)
29
+ * NightTime.parse(text)
30
+ * NightTime::Jst.parse(text)
31
+
32
+ JST
33
+ NightTime::Jst can extract time from Japanese texts.
23
34
 
24
- * Time.mktime
35
+ jst = NightTime::Jst.new("テレビ7月8日(土)24:30~25:00")
36
+ jst.parse
37
+ => [nil,7,8,24,30,nil]
25
38
 
26
- Added methods are
39
+ jst.time
40
+ => Mon Jul 09 00:30:00 +0900 2012
27
41
 
28
- * Time.parse (use ParseDate)
42
+ CHANGES
43
+ * 1.0.0: independent from Time object
44
+
29
45
 
30
46
  HOMEPAGE
31
47
  http://github.com/maiha/night-time
data/Rakefile CHANGED
@@ -1,51 +1 @@
1
- require 'rubygems'
2
- require 'rake/gempackagetask'
3
-
4
- GEM_NAME = "night-time"
5
- AUTHOR = "maiha"
6
- EMAIL = "maiha@wota.jp"
7
- HOMEPAGE = "http://github.com/maiha/night-time"
8
- SUMMARY = "An extension to Time class that supports out of ranged values"
9
- GEM_VERSION = "0.0.1"
10
-
11
- spec = Gem::Specification.new do |s|
12
- s.rubyforge_project = 'asakusarb'
13
- s.executables = []
14
- s.name = GEM_NAME
15
- s.version = GEM_VERSION
16
- s.platform = Gem::Platform::RUBY
17
- s.has_rdoc = true
18
- s.extra_rdoc_files = ["README", "MIT-LICENSE"]
19
- s.summary = SUMMARY
20
- s.description = s.summary
21
- s.author = AUTHOR
22
- s.email = EMAIL
23
- s.homepage = HOMEPAGE
24
- s.require_path = 'lib'
25
- s.files = %w(MIT-LICENSE README Rakefile) + Dir.glob("{lib,spec}/**/*")
26
- end
27
-
28
- Rake::GemPackageTask.new(spec) do |pkg|
29
- pkg.gem_spec = spec
30
- end
31
-
32
- desc "Install the gem"
33
- task :install do
34
- Merb::RakeHelper.install(GEM_NAME, :version => GEM_VERSION)
35
- end
36
-
37
- desc "Uninstall the gem"
38
- task :uninstall do
39
- Merb::RakeHelper.uninstall(GEM_NAME, :version => GEM_VERSION)
40
- end
41
-
42
- desc "Create a gemspec file"
43
- task :gemspec do
44
- File.open("#{GEM_NAME}.gemspec", "w") do |file|
45
- file.puts spec.to_ruby
46
- end
47
- end
48
-
49
- require 'spec/rake/spectask'
50
- desc 'Default: run spec examples'
51
- task :default => 'spec'
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,45 @@
1
+ require 'parsedate'
2
+
3
+ module NightTime
4
+ module Calculate
5
+ def mod_midnight(hour, min, sec)
6
+ extra = 0
7
+
8
+ squeeze = lambda{|val, max, unit, extra|
9
+ num, new_val = val.divmod(max)
10
+ [new_val, extra + max*num*unit]
11
+ }
12
+ sec , extra = squeeze.call(sec.to_i , 60, 1 , extra)
13
+ min , extra = squeeze.call(min.to_i , 60, 60 , extra)
14
+ hour, extra = squeeze.call(hour.to_i, 24, 60*60, extra)
15
+
16
+ return [hour, min, sec, extra]
17
+ end
18
+
19
+ def mktime(*args)
20
+ year, mon, day, hour, min, sec, usec = args
21
+ extra = 0
22
+ if hour.to_i >= 24 or min.to_i >= 60 or sec.to_i > 60
23
+ hour, min, sec, extra = mod_midnight(hour, min, sec)
24
+ end
25
+
26
+ begin
27
+ Time.mktime(year, mon, day, hour, min, sec, usec) + extra
28
+ rescue ArgumentError
29
+ if day < 28
30
+ raise
31
+ else
32
+ extra += (day - 28) * 86400
33
+ day = 28
34
+ Time.mktime(year, mon, day, hour, min, sec, usec) + extra
35
+ end
36
+ end
37
+ end
38
+
39
+ def parse(text)
40
+ mktime(*ParseDate.parsedate(text)[0,7])
41
+ end
42
+ end
43
+
44
+ extend Calculate
45
+ end
@@ -0,0 +1,145 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'nkf'
3
+
4
+ module NightTime
5
+ class Jst
6
+ def self.parse(text, now = nil)
7
+ new(text).time(now)
8
+ end
9
+
10
+ class Build < RuntimeError
11
+ attr_accessor :year,:month,:day,:hour,:min,:sec
12
+ def date?; month and day; end
13
+ def time?; hour and min; end
14
+ def full?; date? and time?; end
15
+ def to_array; [year, month, day, hour, min, sec]; end
16
+ def to_time; NightTime.mktime(year, month, day, hour||0, min||0, sec||0); end
17
+ def inspect; "<Build: %s>" % [year,month,day,hour,min,sec].inspect; end
18
+ end
19
+
20
+ def initialize(text)
21
+ @text = NKF.nkf('-Wwxm0Z0', text).gsub(/\s+/m,' ').strip
22
+ @build = Build.new
23
+ end
24
+
25
+ def parsedate
26
+ build!
27
+ @build.to_array
28
+ end
29
+
30
+ def parse
31
+ parsedate
32
+ end
33
+
34
+ def time(now = nil)
35
+ unless now
36
+ now = Time.now
37
+ now = Time.mktime(now.year, now.month, now.day)
38
+ end
39
+
40
+ build!
41
+ b = @build.dup
42
+ b.year ||= now.year
43
+ b.month ||= now.month
44
+ b.day ||= now.day
45
+ b.hour ||= now.hour
46
+ b.min ||= now.min
47
+ b.sec ||= now.sec
48
+ return b.to_time
49
+ end
50
+
51
+ def parse!
52
+ build!
53
+
54
+ if @build.full?
55
+ return @build.to_time
56
+ elsif @build.date?
57
+ build = @build.dup
58
+ build.year ||= Time.now.year
59
+ return build.to_time
60
+ else
61
+ return nil
62
+ end
63
+ end
64
+
65
+ private
66
+ def build!
67
+ catch(:completed) {
68
+ parse_nengappi
69
+ parse_gappi
70
+ parse_iso8601
71
+ parse_jifunbyou
72
+ parse_jifun
73
+ parse_month_slash_day
74
+ parse_hour_colon_min
75
+ }
76
+ end
77
+
78
+ def parse_nengappi
79
+ @text.scan(/(\d{4})\s*年\s*(\d{1,2})\s*月\s*(\d{1,2})\s*日/) {
80
+ @build.year ||= $1.to_i
81
+ @build.month ||= $2.to_i
82
+ @build.day ||= $3.to_i
83
+ return validate!
84
+ }
85
+ end
86
+
87
+ def parse_gappi
88
+ @text.scan(/(\d{1,2})\s*月\s*(\d{1,2})\s*日/) {
89
+ @build.month ||= $1.to_i
90
+ @build.day ||= $2.to_i
91
+ return validate!
92
+ }
93
+ end
94
+
95
+ def parse_iso8601
96
+ @text.scan(/(\d{4})[-\/](\d{1,2})[-\/](\d{1,2})/) {
97
+ @build.year ||= $1.to_i
98
+ @build.month ||= $2.to_i
99
+ @build.day ||= $3.to_i
100
+ return validate!
101
+ }
102
+ end
103
+
104
+ def parse_month_slash_day
105
+ @text.scan(/(\d{1,2})\/(\d{1,2})/) {
106
+ @build.month ||= $1.to_i
107
+ @build.day ||= $2.to_i
108
+ return validate!
109
+ }
110
+ end
111
+
112
+ def parse_jifunbyou
113
+ @text.scan(/(夜)?\s*(\d{1,2})\s*時\s*(\d{1,2})\s*分\s*(\d{1,2})秒/) {
114
+ hour_margin = ($1 and $2.to_i < 5) ? 24 : 0
115
+ @build.hour ||= hour_margin + $2.to_i
116
+ @build.min ||= $3.to_i
117
+ @build.sec ||= $4.to_i
118
+ return validate!
119
+ }
120
+ end
121
+
122
+ def parse_jifun
123
+ @text.scan(/(夜)?\s*(\d{1,2})\s*時\s*(\d{1,2})\s*分/) {
124
+ hour_margin = ($1 and $2.to_i < 5) ? 24 : 0
125
+ @build.hour ||= hour_margin + $2.to_i
126
+ @build.min ||= $3.to_i
127
+ return validate!
128
+ }
129
+ end
130
+
131
+ def parse_hour_colon_min
132
+ @text.scan(/(夜)?\s*(\d{1,2}):(\d{1,2})/) {
133
+ hour_margin = ($1 and $2.to_i < 5) ? 24 : 0
134
+ @build.hour ||= hour_margin + $2.to_i
135
+ @build.min ||= $3.to_i
136
+ return validate!
137
+ }
138
+ end
139
+
140
+ def validate!
141
+ throw(:completed) if @build.full?
142
+ end
143
+ end
144
+ end
145
+
@@ -0,0 +1,3 @@
1
+ module NightTime
2
+ VERSION = "1.0.1"
3
+ end
data/lib/night-time.rb CHANGED
@@ -1,37 +1,8 @@
1
- require 'parsedate'
1
+ require "night-time/version"
2
2
 
3
- class Time
4
- class << self
5
- def mod_midnight(hour, min, sec)
6
- extra = 0
7
-
8
- squeeze = lambda{|val, max, unit, extra|
9
- num, new_val = val.divmod(max)
10
- [new_val, extra + max*num*unit]
11
- }
12
- sec , extra = squeeze.call(sec.to_i , 60, 1 , extra)
13
- min , extra = squeeze.call(min.to_i , 60, 60 , extra)
14
- hour, extra = squeeze.call(hour.to_i, 24, 60*60, extra)
15
-
16
- return [hour, min, sec, extra]
17
- end
18
-
19
-
20
- alias mktime_without_night_time mktime
3
+ module NightTime
4
+ autoload :Jst, "night-time/jst"
5
+ end
21
6
 
22
- def mktime(*args)
23
- year, mon, day, hour, min, sec, usec = args
24
- extra = 0
25
- if hour.to_i >= 24 or min.to_i >= 60 or sec.to_i > 60
26
- hour, min, sec, extra = mod_midnight(hour, min, sec)
27
- end
28
- mktime_without_night_time(year, mon, day, hour, min, sec, usec) + extra
29
- end
7
+ require File.join(File.dirname(__FILE__), "night-time/calculate")
30
8
 
31
- unless respond_to?(:parse)
32
- def parse(text)
33
- Time.mktime(*ParseDate.parsedate(text)[0,7])
34
- end
35
- end
36
- end
37
- end
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "night-time/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "night-time"
7
+ s.version = NightTime::VERSION
8
+ s.authors = ["maiha"]
9
+ s.email = ["maiha@wota.jp"]
10
+ s.homepage = "https://github.com/maiha/night-time"
11
+ s.summary = %q{NightTime behaves like Time but it also supports out of ranged values}
12
+ s.description = %q{NightTime behaves like Time but it also supports out of ranged values}
13
+
14
+ s.rubyforge_project = "night-time"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ s.add_development_dependency "rspec"
22
+ end
data/spec/jst_spec.rb ADDED
@@ -0,0 +1,78 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ require File.join(File.dirname(__FILE__), 'spec_helper.rb')
4
+
5
+ describe NightTime::Jst do
6
+ describe ".parse" do
7
+ it "should return a time object" do
8
+ NightTime::Jst.parse("2012/7/3 11:30").should == Time.mktime(2012,7,3,11,30)
9
+ end
10
+ end
11
+ end
12
+
13
+ describe NightTime::Jst do
14
+ let(:year ) { Time.now.year }
15
+ let(:month) { Time.now.month }
16
+ let(:day ) { Time.now.day }
17
+ subject { NightTime::Jst.new(text) }
18
+
19
+ context "(YYYY-MM-DD)" do
20
+ let(:text ) { "1994-04-12" }
21
+ its(:parse) { should == [1994,4,12,nil,nil,nil] }
22
+ its(:time ) { should == Time.mktime(1994,4,12) }
23
+ end
24
+
25
+ context "(XXXX/X/X)" do
26
+ let(:text ) { "2012/7/3" }
27
+ its(:parse) { should == [2012,7,3,nil,nil,nil] }
28
+ its(:time ) { should == Time.mktime(2012,7,3) }
29
+ end
30
+
31
+ context "(XX月XX日)[半角]" do
32
+ let(:text ) { "テレビ7月8日(土)24:30~25:00" }
33
+ its(:parse) { should == [nil,7,8,24,30,nil] }
34
+ its(:time ) { should == Time.mktime(year,7,9,0,30) }
35
+ end
36
+
37
+ context "(XX月XX日)[全角]" do
38
+ let(:text ) { "テレビ 7月8日(土)24:30~25:00" }
39
+ its(:parse) { should == [nil,7,8,24,30,nil] }
40
+ its(:time ) { should == Time.mktime(year,7,9,0,30) }
41
+ end
42
+
43
+ context "(XX年XX月XX日XX時XX分)" do
44
+ let(:text ) { "日テレ 2012年7月8日(日)12時45分~13時55分(1)水中を走る車" }
45
+ its(:parse) { should == [2012,7,8,12,45,nil] }
46
+ its(:time ) { should == Time.mktime(2012,7,8,12,45) }
47
+ end
48
+
49
+ context "(XX年XX月XX日26時XX分)" do
50
+ let(:text ) { "TBSチャンネルHD 2012年7月6日(金) 26時55分~27時25分" }
51
+ its(:parse) { should == [2012,7,6,26,55,nil] }
52
+ its(:time ) { should == Time.mktime(2012,7,7,2,55) }
53
+ end
54
+
55
+ context "(X月X日X時夜XX分XX秒)" do
56
+ let(:text ) { "【#65】7月2日(月)深夜3時33分22秒~【#66】7月3日(火)" }
57
+ its(:parse) { should == [nil, 7, 2, 27, 33, 22] }
58
+ its(:time ) { should == Time.mktime(year,7,3,3,33,22) }
59
+ end
60
+
61
+ context "(XX年XX月XX日XX:XX)" do
62
+ let(:text ) { "日テレ7月9日(月)19:00~20:54MC" }
63
+ its(:parse) { should == [nil, 7, 9, 19, 0, nil] }
64
+ its(:time ) { should == Time.mktime(year,7,9,19) }
65
+ end
66
+
67
+ context "(XX年XX月XX日 夜XX:XX)" do
68
+ let(:text ) { "XXX7/14(土)夜0:15-1:45 NHK" }
69
+ its(:parse) { should == [nil, 7, 14, 24, 15, nil] }
70
+ its(:time ) { should == Time.mktime(year,7,15,0,15) }
71
+ end
72
+
73
+ context "(XX:XX)" do
74
+ let(:text ) { "隔週木曜23:30-24:00" }
75
+ its(:parse) { should == [nil, nil, nil, 23, 30, nil] }
76
+ its(:time ) { should == Time.mktime(year,month,day,23,30) }
77
+ end
78
+ end
@@ -0,0 +1,32 @@
1
+
2
+ require File.join(File.dirname(__FILE__), 'spec_helper.rb')
3
+
4
+ describe NightTime do
5
+ describe ".mktime" do
6
+ it "should treat late night time" do
7
+ NightTime.mktime(2010,1,10,26,40).should == Time.mktime(2010,1,11,2,40)
8
+ end
9
+
10
+ it "should treat date overflow" do
11
+ NightTime.mktime(2010,2,33).should == Time.mktime(2010,3,5)
12
+ end
13
+
14
+ it "should treat valid time too" do
15
+ NightTime.mktime(2010,1,10, 2,40).should == Time.mktime(2010,1,10,2,40)
16
+ end
17
+ end
18
+
19
+ it "should provide .parse" do
20
+ NightTime.should respond_to(:parse)
21
+ end
22
+
23
+ describe ".parse" do
24
+ it "should parse late night time" do
25
+ NightTime.parse('2010-01-10 26:40').should == Time.mktime(2010,1,11,2,40)
26
+ end
27
+
28
+ it "should treat valid time too" do
29
+ NightTime.parse('2010-01-10 02:40').should == Time.mktime(2010,1,10,2,40)
30
+ end
31
+ end
32
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,4 +1,5 @@
1
+ $KCODE = 'u'
1
2
 
2
- require 'spec'
3
+ require 'rspec'
3
4
 
4
5
  require File.join(File.dirname(__FILE__), '/../lib/night-time')
metadata CHANGED
@@ -1,7 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: night-time
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ hash: 21
5
+ prerelease:
6
+ segments:
7
+ - 1
8
+ - 0
9
+ - 1
10
+ version: 1.0.1
5
11
  platform: ruby
6
12
  authors:
7
13
  - maiha
@@ -9,28 +15,44 @@ autorequire:
9
15
  bindir: bin
10
16
  cert_chain: []
11
17
 
12
- date: 2010-01-11 00:00:00 +09:00
13
- default_executable:
14
- dependencies: []
15
-
16
- description: An extension to Time class that supports out of ranged values
17
- email: maiha@wota.jp
18
+ date: 2012-07-03 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rspec
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 3
29
+ segments:
30
+ - 0
31
+ version: "0"
32
+ type: :development
33
+ version_requirements: *id001
34
+ description: NightTime behaves like Time but it also supports out of ranged values
35
+ email:
36
+ - maiha@wota.jp
18
37
  executables: []
19
38
 
20
39
  extensions: []
21
40
 
22
- extra_rdoc_files:
23
- - README
24
- - MIT-LICENSE
41
+ extra_rdoc_files: []
42
+
25
43
  files:
26
44
  - MIT-LICENSE
27
45
  - README
28
46
  - Rakefile
29
47
  - lib/night-time.rb
30
- - spec/time_spec.rb
48
+ - lib/night-time/calculate.rb
49
+ - lib/night-time/jst.rb
50
+ - lib/night-time/version.rb
51
+ - night-time.gemspec
52
+ - spec/jst_spec.rb
53
+ - spec/night_time_spec.rb
31
54
  - spec/spec_helper.rb
32
- has_rdoc: true
33
- homepage: http://github.com/maiha/night-time
55
+ homepage: https://github.com/maiha/night-time
34
56
  licenses: []
35
57
 
36
58
  post_install_message:
@@ -39,23 +61,29 @@ rdoc_options: []
39
61
  require_paths:
40
62
  - lib
41
63
  required_ruby_version: !ruby/object:Gem::Requirement
64
+ none: false
42
65
  requirements:
43
66
  - - ">="
44
67
  - !ruby/object:Gem::Version
68
+ hash: 3
69
+ segments:
70
+ - 0
45
71
  version: "0"
46
- version:
47
72
  required_rubygems_version: !ruby/object:Gem::Requirement
73
+ none: false
48
74
  requirements:
49
75
  - - ">="
50
76
  - !ruby/object:Gem::Version
77
+ hash: 3
78
+ segments:
79
+ - 0
51
80
  version: "0"
52
- version:
53
81
  requirements: []
54
82
 
55
- rubyforge_project: asakusarb
56
- rubygems_version: 1.3.5
83
+ rubyforge_project: night-time
84
+ rubygems_version: 1.8.15
57
85
  signing_key:
58
86
  specification_version: 3
59
- summary: An extension to Time class that supports out of ranged values
87
+ summary: NightTime behaves like Time but it also supports out of ranged values
60
88
  test_files: []
61
89
 
data/spec/time_spec.rb DELETED
@@ -1,28 +0,0 @@
1
-
2
- require File.join(File.dirname(__FILE__), 'spec_helper.rb')
3
-
4
- describe Time do
5
- describe ".mktime" do
6
- it "should treat late night time" do
7
- Time.mktime(2010,1,10,26,40).should == Time.mktime(2010,1,11,2,40)
8
- end
9
-
10
- it "should treat valid time too" do
11
- Time.mktime(2010,1,10, 2,40).should == Time.mktime(2010,1,10,2,40)
12
- end
13
- end
14
-
15
- it "should provide .parse" do
16
- Time.should respond_to(:parse)
17
- end
18
-
19
- describe ".parse" do
20
- it "should parse late night time" do
21
- Time.parse('2010-01-10 26:40').should == Time.mktime(2010,1,11,2,40)
22
- end
23
-
24
- it "should treat valid time too" do
25
- Time.parse('2010-01-10 02:40').should == Time.mktime(2010,1,10,2,40)
26
- end
27
- end
28
- end