timeliness 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/.rspec +1 -0
- data/CHANGELOG.rdoc +6 -0
- data/README.rdoc +30 -4
- data/Rakefile +2 -32
- data/benchmark.rb +161 -0
- data/lib/timeliness.rb +5 -4
- data/lib/timeliness/{formats.rb → definitions.rb} +19 -5
- data/lib/timeliness/format.rb +64 -0
- data/lib/timeliness/format_set.rb +19 -74
- data/lib/timeliness/helpers.rb +1 -1
- data/lib/timeliness/parser.rb +59 -17
- data/lib/timeliness/version.rb +1 -1
- data/spec/spec_helper.rb +2 -2
- data/spec/timeliness/{formats_spec.rb → definitions_spec.rb} +23 -23
- data/spec/timeliness/format_set_spec.rb +20 -33
- data/spec/timeliness/format_spec.rb +41 -0
- data/spec/timeliness/parser_spec.rb +134 -61
- data/timeliness.gemspec +14 -22
- metadata +22 -13
data/lib/timeliness/helpers.rb
CHANGED
data/lib/timeliness/parser.rb
CHANGED
@@ -7,42 +7,40 @@ module Timeliness
|
|
7
7
|
def parse(value, *args)
|
8
8
|
return value unless value.is_a?(String)
|
9
9
|
|
10
|
-
options = args
|
11
|
-
type = args.first
|
10
|
+
type, options = type_and_options_from_args(args)
|
12
11
|
|
13
12
|
time_array = _parse(value, type, options)
|
14
13
|
return nil if time_array.nil?
|
15
14
|
|
16
|
-
|
17
|
-
|
15
|
+
default_values_by_type(time_array, type, options) unless type == :datetime
|
16
|
+
|
17
|
+
make_time(time_array[0..7], options[:zone])
|
18
18
|
rescue NoMethodError => ex
|
19
19
|
raise ex unless ex.message =~ /zone/
|
20
20
|
raise MissingTimezoneSupport, "ActiveSupport must be loaded to use timezones other than :utc and :local."
|
21
21
|
end
|
22
22
|
|
23
|
-
def make_time(time_array,
|
23
|
+
def make_time(time_array, zone_option=nil)
|
24
24
|
return nil unless fast_date_valid_with_fallback(*time_array[0..2])
|
25
25
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
Time.use_zone(zone) { Time.zone.local(*time_array) }
|
34
|
-
end
|
26
|
+
zone_or_offset = time_array.delete_at(7)
|
27
|
+
zone, offset = zone_and_offset(zone_or_offset) if zone_or_offset
|
28
|
+
|
29
|
+
value = create_time_in_zone(time_array, zone || zone_option)
|
30
|
+
value = time_in_zone(value, zone_option) if zone
|
31
|
+
|
32
|
+
offset ? value + (value.utc_offset - offset) : value
|
35
33
|
rescue ArgumentError, TypeError
|
36
34
|
nil
|
37
35
|
end
|
38
36
|
|
39
37
|
def _parse(string, type=nil, options={})
|
40
38
|
if options[:strict] && type
|
41
|
-
set =
|
39
|
+
set = Definitions.send("#{type}_format_set")
|
42
40
|
set.match(string, options[:format])
|
43
41
|
else
|
44
42
|
values = nil
|
45
|
-
|
43
|
+
Definitions.format_sets(type, string).find {|set| values = set.match(string, options[:format]) }
|
46
44
|
values
|
47
45
|
end
|
48
46
|
rescue
|
@@ -51,7 +49,18 @@ module Timeliness
|
|
51
49
|
|
52
50
|
private
|
53
51
|
|
54
|
-
def
|
52
|
+
def type_and_options_from_args(args)
|
53
|
+
options = args.last.is_a?(Hash) ? args.pop : {}
|
54
|
+
type_or_now = args.first
|
55
|
+
if type_or_now.is_a?(Symbol)
|
56
|
+
type = type_or_now
|
57
|
+
elsif !type_or_now.is_a?(Hash)
|
58
|
+
options[:now] = type_or_now
|
59
|
+
end
|
60
|
+
return type, options
|
61
|
+
end
|
62
|
+
|
63
|
+
def default_values_by_type(values, type, options)
|
55
64
|
case type
|
56
65
|
when :date
|
57
66
|
values[3..7] = nil
|
@@ -87,6 +96,39 @@ module Timeliness
|
|
87
96
|
end
|
88
97
|
end
|
89
98
|
|
99
|
+
def time_in_zone(time, zone=nil)
|
100
|
+
zone ||= Timeliness.default_timezone
|
101
|
+
case zone
|
102
|
+
when :utc, :local
|
103
|
+
time.send("get#{zone}")
|
104
|
+
when :current
|
105
|
+
time.in_time_zone
|
106
|
+
else
|
107
|
+
Time.use_zone(zone) { time.in_time_zone }
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
def create_time_in_zone(time_array, zone=nil)
|
112
|
+
zone ||= Timeliness.default_timezone
|
113
|
+
case zone
|
114
|
+
when :utc, :local
|
115
|
+
time_with_datetime_fallback(zone, *time_array.compact)
|
116
|
+
when :current
|
117
|
+
Time.zone.local(*time_array)
|
118
|
+
else
|
119
|
+
Time.use_zone(zone) { Time.zone.local(*time_array) }
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
def zone_and_offset(parsed_value)
|
124
|
+
if parsed_value.is_a?(String)
|
125
|
+
zone = Definitions.timezone_mapping[parsed_value] || parsed_value
|
126
|
+
else
|
127
|
+
offset = parsed_value
|
128
|
+
end
|
129
|
+
return zone, offset
|
130
|
+
end
|
131
|
+
|
90
132
|
# Taken from ActiveSupport and simplified
|
91
133
|
def time_with_datetime_fallback(utc_or_local, year, month=1, day=1, hour=0, min=0, sec=0, usec=0)
|
92
134
|
return nil if hour > 23 || min > 59 || sec > 59
|
data/lib/timeliness/version.rb
CHANGED
data/spec/spec_helper.rb
CHANGED
@@ -1,93 +1,93 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe Timeliness::
|
3
|
+
describe Timeliness::Definitions do
|
4
4
|
|
5
5
|
context "add_formats" do
|
6
6
|
before do
|
7
|
-
@default_formats =
|
7
|
+
@default_formats = definitions.time_formats.dup
|
8
8
|
end
|
9
9
|
|
10
10
|
it "should add format to format array" do
|
11
|
-
|
12
|
-
|
11
|
+
definitions.add_formats(:time, "h o'clock")
|
12
|
+
definitions.time_formats.should include("h o'clock")
|
13
13
|
end
|
14
14
|
|
15
15
|
it "should parse new format after its added" do
|
16
16
|
should_not_parse("12 o'clock", :time)
|
17
|
-
|
17
|
+
definitions.add_formats(:time, "h o'clock")
|
18
18
|
should_parse("12 o'clock", :time)
|
19
19
|
end
|
20
20
|
|
21
21
|
it "should raise error if format exists" do
|
22
|
-
|
22
|
+
expect { definitions.add_formats(:time, "hh:nn:ss") }.should raise_error
|
23
23
|
end
|
24
24
|
|
25
25
|
context "with :before option" do
|
26
26
|
it "should add new format with higher precedence" do
|
27
|
-
|
27
|
+
definitions.add_formats(:time, "ss:hh:nn", :before => 'hh:nn:ss')
|
28
28
|
time_array = parser._parse('59:23:58', :time)
|
29
29
|
time_array.should == [nil,nil,nil,23,58,59,nil,nil]
|
30
30
|
end
|
31
31
|
|
32
32
|
it "should raise error if :before format does not exist" do
|
33
|
-
|
33
|
+
expect { definitions.add_formats(:time, "ss:hh:nn", :before => 'nn:hh:ss') }.should raise_error
|
34
34
|
end
|
35
35
|
end
|
36
36
|
|
37
37
|
after do
|
38
|
-
|
39
|
-
|
38
|
+
definitions.time_formats = @default_formats
|
39
|
+
definitions.compile_formats
|
40
40
|
end
|
41
41
|
end
|
42
42
|
|
43
43
|
context "remove_formats" do
|
44
44
|
before do
|
45
|
-
@default_formats =
|
45
|
+
@default_formats = definitions.time_formats.dup
|
46
46
|
end
|
47
47
|
|
48
48
|
it "should remove a single format from the formats array for type" do
|
49
|
-
|
50
|
-
|
49
|
+
definitions.remove_formats(:time, 'h.nn_ampm')
|
50
|
+
definitions.time_formats.should_not include('h.nn_ampm')
|
51
51
|
end
|
52
52
|
|
53
53
|
it "should remove multiple formats from formats array for type" do
|
54
|
-
|
55
|
-
|
56
|
-
|
54
|
+
definitions.remove_formats(:time, 'h:nn', 'h.nn_ampm')
|
55
|
+
definitions.time_formats.should_not include('h:nn')
|
56
|
+
definitions.time_formats.should_not include('h.nn_ampm')
|
57
57
|
end
|
58
58
|
|
59
59
|
it "should prevent parsing of removed format" do
|
60
60
|
should_parse('2.12am', :time)
|
61
|
-
|
61
|
+
definitions.remove_formats(:time, 'h.nn_ampm')
|
62
62
|
should_not_parse('2.12am', :time)
|
63
63
|
end
|
64
64
|
|
65
65
|
it "should raise error if format does not exist" do
|
66
|
-
|
66
|
+
expect { definitions.remove_formats(:time, "ss:hh:nn") }.should raise_error()
|
67
67
|
end
|
68
68
|
|
69
69
|
after do
|
70
|
-
|
71
|
-
|
70
|
+
definitions.time_formats = @default_formats
|
71
|
+
definitions.compile_formats
|
72
72
|
end
|
73
73
|
end
|
74
74
|
|
75
75
|
context "use_euro_formats" do
|
76
76
|
it "should allow ambiguous date to be parsed as European format" do
|
77
77
|
parser._parse('01/02/2000', :date).should == [2000,1,2,nil,nil,nil,nil,nil]
|
78
|
-
|
78
|
+
definitions.use_euro_formats
|
79
79
|
parser._parse('01/02/2000', :date).should == [2000,2,1,nil,nil,nil,nil,nil]
|
80
80
|
end
|
81
81
|
end
|
82
82
|
|
83
83
|
context "use_use_formats" do
|
84
84
|
before do
|
85
|
-
|
85
|
+
definitions.use_euro_formats
|
86
86
|
end
|
87
87
|
|
88
88
|
it "should allow ambiguous date to be parsed as European format" do
|
89
89
|
parser._parse('01/02/2000', :date).should == [2000,2,1,nil,nil,nil,nil,nil]
|
90
|
-
|
90
|
+
definitions.use_us_formats
|
91
91
|
parser._parse('01/02/2000', :date).should == [2000,1,2,nil,nil,nil,nil,nil]
|
92
92
|
end
|
93
93
|
end
|
@@ -1,42 +1,16 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Timeliness::FormatSet do
|
4
|
-
context "
|
5
|
-
|
6
|
-
define_method_for('yyyy-mm-dd').call('2000', '1', '2').should == [2000,1,2,nil,nil,nil,nil,nil]
|
7
|
-
end
|
8
|
-
|
9
|
-
it "should define method which outputs date array from format with different order" do
|
10
|
-
define_method_for('dd/mm/yyyy').call('2', '1', '2000').should == [2000,1,2,nil,nil,nil,nil,nil]
|
11
|
-
end
|
12
|
-
|
13
|
-
it "should define method which outputs time array" do
|
14
|
-
define_method_for('hh:nn:ss').call('01', '02', '03').should == [nil,nil,nil,1,2,3,nil,nil]
|
15
|
-
end
|
16
|
-
|
17
|
-
it "should define method which outputs time array with meridian 'pm' adjusted hour" do
|
18
|
-
define_method_for('hh:nn:ss ampm').call('01', '02', '03', 'pm').should == [nil,nil,nil,13,2,3,nil,nil]
|
19
|
-
end
|
20
|
-
|
21
|
-
it "should define method which outputs time array with meridian 'am' unadjusted hour" do
|
22
|
-
define_method_for('hh:nn:ss ampm').call('01', '02', '03', 'am').should == [nil,nil,nil,1,2,3,nil,nil]
|
23
|
-
end
|
24
|
-
|
25
|
-
it "should define method which outputs time array with microseconds" do
|
26
|
-
define_method_for('hh:nn:ss.u').call('01', '02', '03', '99').should == [nil,nil,nil,1,2,3,990000,nil]
|
27
|
-
end
|
28
|
-
|
29
|
-
it "should define method which outputs datetime array with zone offset" do
|
30
|
-
define_method_for('yyyy-mm-dd hh:nn:ss.u zo').call('2001', '02', '03', '04', '05', '06', '99', '+10:00').should == [2001,2,3,4,5,6,990000,36000]
|
31
|
-
end
|
4
|
+
context "#compile!" do
|
5
|
+
let(:set) { Timeliness::FormatSet.new(['yyyy-mm-dd', 'dd/mm/yyyy']) }
|
32
6
|
|
33
|
-
it
|
34
|
-
|
7
|
+
it 'should set the regexp for the set' do
|
8
|
+
set.compile!
|
9
|
+
set.regexp.should_not be_nil
|
35
10
|
end
|
36
11
|
end
|
37
12
|
|
38
13
|
context "compiled regexp" do
|
39
|
-
|
40
14
|
context "for time formats" do
|
41
15
|
format_tests = {
|
42
16
|
'hh:nn:ss' => {:pass => ['12:12:12', '01:01:01'], :fail => ['1:12:12', '12:1:12', '12:12:1', '12-12-12']},
|
@@ -99,8 +73,21 @@ describe Timeliness::FormatSet do
|
|
99
73
|
|
100
74
|
end
|
101
75
|
|
102
|
-
|
103
|
-
Timeliness::FormatSet.compile([
|
76
|
+
context "#match" do
|
77
|
+
let(:set) { Timeliness::FormatSet.compile(['yyyy-mm-dd', 'dd/mm/yyyy']) }
|
78
|
+
|
79
|
+
it 'should return array if string matches a format in set' do
|
80
|
+
set.match('2000-01-02').should be_kind_of(Array)
|
81
|
+
end
|
82
|
+
|
83
|
+
it 'should return nil if string does not matches a format in set' do
|
84
|
+
set.match('2nd Feb 2000').should be_nil
|
85
|
+
end
|
86
|
+
|
87
|
+
it 'should only use specific format string for match if provided' do
|
88
|
+
set.match('2000-01-02', 'yyyy-mm-dd').should be_kind_of(Array)
|
89
|
+
set.match('2000-01-02', 'dd/mm/yyyy').should be_nil
|
90
|
+
end
|
104
91
|
end
|
105
92
|
|
106
93
|
def compile_regexp(format)
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Timeliness::Format do
|
4
|
+
context "#process" do
|
5
|
+
it "should define method which outputs date array with values in correct order" do
|
6
|
+
format_for('yyyy-mm-dd').process('2000', '1', '2').should == [2000,1,2,nil,nil,nil,nil,nil]
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should define method which outputs date array from format with different order" do
|
10
|
+
format_for('dd/mm/yyyy').process('2', '1', '2000').should == [2000,1,2,nil,nil,nil,nil,nil]
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should define method which outputs time array" do
|
14
|
+
format_for('hh:nn:ss').process('01', '02', '03').should == [nil,nil,nil,1,2,3,nil,nil]
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should define method which outputs time array with meridian 'pm' adjusted hour" do
|
18
|
+
format_for('hh:nn:ss ampm').process('01', '02', '03', 'pm').should == [nil,nil,nil,13,2,3,nil,nil]
|
19
|
+
end
|
20
|
+
|
21
|
+
it "should define method which outputs time array with meridian 'am' unadjusted hour" do
|
22
|
+
format_for('hh:nn:ss ampm').process('01', '02', '03', 'am').should == [nil,nil,nil,1,2,3,nil,nil]
|
23
|
+
end
|
24
|
+
|
25
|
+
it "should define method which outputs time array with microseconds" do
|
26
|
+
format_for('hh:nn:ss.u').process('01', '02', '03', '99').should == [nil,nil,nil,1,2,3,990000,nil]
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should define method which outputs datetime array with zone offset" do
|
30
|
+
format_for('yyyy-mm-dd hh:nn:ss.u zo').process('2001', '02', '03', '04', '05', '06', '99', '+10:00').should == [2001,2,3,4,5,6,990000,36000]
|
31
|
+
end
|
32
|
+
|
33
|
+
it "should define method which outputs datetime array with timezone string" do
|
34
|
+
format_for('yyyy-mm-dd hh:nn:ss.u tz').process('2001', '02', '03', '04', '05', '06', '99', 'EST').should == [2001,2,3,4,5,6,990000,'EST']
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def format_for(format)
|
39
|
+
Timeliness::Format.new(format).compile!
|
40
|
+
end
|
41
|
+
end
|
@@ -1,38 +1,128 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Timeliness::Parser do
|
4
|
-
|
4
|
+
before(:all) do
|
5
|
+
Timecop.freeze(2010,1,1,0,0,0)
|
6
|
+
end
|
7
|
+
|
8
|
+
describe "parse" do
|
9
|
+
it "should return time object for valid datetime string" do
|
10
|
+
parse("2000-01-01 12:13:14").should be_kind_of(Time)
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should return nil for empty string" do
|
14
|
+
parse("").should be_nil
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should return nil for nil value" do
|
18
|
+
parse(nil).should be_nil
|
19
|
+
end
|
20
|
+
|
21
|
+
it "should return return same value if value not a string" do
|
22
|
+
value = Time.now
|
23
|
+
parse(value).should == value
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should return time object for valid date string" do
|
27
|
+
parse("2000-01-01").should be_kind_of(Time)
|
28
|
+
end
|
29
|
+
|
30
|
+
it "should return nil for invalid date string" do
|
31
|
+
should_not_parse("2000-02-30")
|
32
|
+
end
|
33
|
+
|
5
34
|
it "should return time object for valid time string" do
|
6
|
-
parse("
|
35
|
+
parse("12:13:14").should be_kind_of(Time)
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should return nil for invalid time string" do
|
39
|
+
should_not_parse("25:00:00")
|
7
40
|
end
|
8
41
|
|
9
42
|
it "should return nil for datetime string with invalid date part" do
|
10
|
-
should_not_parse("2000-02-30 12:13:14"
|
43
|
+
should_not_parse("2000-02-30 12:13:14")
|
11
44
|
end
|
12
45
|
|
13
46
|
it "should return nil for datetime string with invalid time part" do
|
14
|
-
should_not_parse("2000-02-01 25:13:14"
|
47
|
+
should_not_parse("2000-02-01 25:13:14")
|
15
48
|
end
|
16
49
|
|
17
|
-
|
18
|
-
|
50
|
+
context "string with zone offset value" do
|
51
|
+
context "when current timezone is earler than string zone" do
|
52
|
+
it 'should return value shifted by positive offset in default timezone' do
|
53
|
+
value = parse("2000-06-01T12:00:00+02:00")
|
54
|
+
value.should == Time.local(2000,6,1,20,0,0)
|
55
|
+
value.utc_offset.should == 10.hours
|
56
|
+
end
|
57
|
+
|
58
|
+
it 'should return value shifted by negative offset in default timezone' do
|
59
|
+
value = parse("2000-06-01T12:00:00-01:00")
|
60
|
+
value.should == Time.local(2000,6,1,23,0,0)
|
61
|
+
value.utc_offset.should == 10.hours
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
context "when current timezone is later than string zone" do
|
66
|
+
before(:all) do
|
67
|
+
Timeliness.default_timezone = :current
|
68
|
+
Time.zone = 'America/Phoenix'
|
69
|
+
end
|
70
|
+
|
71
|
+
it 'should return value shifted by positive offset in default timezone' do
|
72
|
+
value = parse("2000-06-01T12:00:00+02:00")
|
73
|
+
value.should == Time.zone.local(2000,6,1,3,0,0)
|
74
|
+
value.utc_offset.should == -7.hours
|
75
|
+
end
|
76
|
+
|
77
|
+
it 'should return value shifted by negative offset in default timezone' do
|
78
|
+
value = parse("2000-06-01T12:00:00-01:00")
|
79
|
+
value.should == Time.zone.local(2000,6,1,6,0,0)
|
80
|
+
value.utc_offset.should == -7.hours
|
81
|
+
end
|
82
|
+
|
83
|
+
after(:all) do
|
84
|
+
Time.zone = nil
|
85
|
+
Timeliness.default_timezone = :local
|
86
|
+
end
|
87
|
+
end
|
19
88
|
end
|
20
89
|
|
21
|
-
|
22
|
-
|
90
|
+
context "string with zone abbreviation" do
|
91
|
+
it 'should return value using string zone in default timezone' do
|
92
|
+
value = parse("Thu, 01 Jun 2000 03:00:00 MST")
|
93
|
+
value.should == Time.local(2000,6,1,20,0,0)
|
94
|
+
value.utc_offset.should == 10.hours
|
95
|
+
end
|
23
96
|
end
|
24
97
|
|
25
|
-
|
26
|
-
|
98
|
+
context "with :datetime type" do
|
99
|
+
it "should return time object for valid datetime string" do
|
100
|
+
parse("2000-01-01 12:13:14", :datetime).should == Time.local(2000,1,1,12,13,14)
|
101
|
+
end
|
27
102
|
end
|
28
103
|
|
29
|
-
|
30
|
-
|
104
|
+
context "with :date type" do
|
105
|
+
it "should return time object for valid date string" do
|
106
|
+
parse("2000-01-01", :date).should == Time.local(2000,1,1)
|
107
|
+
end
|
108
|
+
|
109
|
+
it "should ignore time in datetime string" do
|
110
|
+
parse('2000-02-01 12:13', :date).should == Time.local(2000,2,1)
|
111
|
+
end
|
31
112
|
end
|
32
113
|
|
33
|
-
|
34
|
-
|
35
|
-
|
114
|
+
context "with :time type" do
|
115
|
+
it "should return time object with a dummy date values" do
|
116
|
+
parse('12:13', :time).should == Time.local(2010,1,1,12,13)
|
117
|
+
end
|
118
|
+
|
119
|
+
it "should ignore date in datetime string" do
|
120
|
+
parse('2010-02-01 12:13', :time).should == Time.local(2010,1,1,12,13)
|
121
|
+
end
|
122
|
+
|
123
|
+
it "should raise error if time hour is out of range for AM meridian" do
|
124
|
+
parse('13:14 am', :time).should be_nil
|
125
|
+
end
|
36
126
|
end
|
37
127
|
|
38
128
|
context "with :now option" do
|
@@ -75,14 +165,14 @@ describe Timeliness::Parser do
|
|
75
165
|
context "without ActiveSupport loaded" do
|
76
166
|
it 'should output message' do
|
77
167
|
lambda {
|
78
|
-
Time.should_receive(:
|
79
|
-
time = parse("2000-06-01 12:13:14", :datetime, :zone =>
|
168
|
+
Time.should_receive(:zone).and_raise(NoMethodError.new("undefined method `zone' for Time:Class"))
|
169
|
+
time = parse("2000-06-01 12:13:14", :datetime, :zone => :current)
|
80
170
|
}.should raise_error(Timeliness::Parser::MissingTimezoneSupport)
|
81
171
|
end
|
82
172
|
end
|
83
173
|
end
|
84
174
|
|
85
|
-
|
175
|
+
context "for time type" do
|
86
176
|
context "with date from date_for_time_type" do
|
87
177
|
before do
|
88
178
|
@original = Timeliness.date_for_time_type
|
@@ -110,28 +200,21 @@ describe Timeliness::Parser do
|
|
110
200
|
end
|
111
201
|
|
112
202
|
context "with :zone option" do
|
113
|
-
before(:all) do
|
114
|
-
Timecop.freeze(2010,1,1,0,0,0)
|
115
|
-
end
|
116
|
-
|
117
203
|
it "should use date from the specified zone" do
|
118
204
|
time = parse("12:13:14", :time, :zone => :utc)
|
119
205
|
time.year.should == 2009
|
120
206
|
time.month.should == 12
|
121
207
|
time.day.should == 31
|
122
208
|
end
|
123
|
-
|
124
|
-
after(:all) do
|
125
|
-
Timecop.return
|
126
|
-
end
|
127
209
|
end
|
210
|
+
|
128
211
|
end
|
129
212
|
end
|
130
213
|
|
131
|
-
|
132
|
-
context "with type" do
|
214
|
+
describe "_parse" do
|
215
|
+
context "with no type" do
|
133
216
|
it "should return date array from date string" do
|
134
|
-
time_array = parser._parse('2000-02-01'
|
217
|
+
time_array = parser._parse('2000-02-01')
|
135
218
|
time_array.should == [2000,2,1,nil,nil,nil,nil,nil]
|
136
219
|
end
|
137
220
|
|
@@ -141,45 +224,45 @@ describe Timeliness::Parser do
|
|
141
224
|
end
|
142
225
|
|
143
226
|
it "should return datetime array from datetime string" do
|
144
|
-
time_array = parser._parse('2000-02-01 12:13:14'
|
145
|
-
time_array.should == [2000,2,1,12,13,14,nil,nil]
|
146
|
-
end
|
147
|
-
|
148
|
-
it "should return date array from date string when type is datetime" do
|
149
|
-
time_array = parser._parse('2000-02-01', :datetime)
|
150
|
-
time_array.should == [2000,2,1,nil,nil,nil,nil,nil]
|
151
|
-
end
|
152
|
-
|
153
|
-
it "should return datetime array from datetime string when type is date" do
|
154
|
-
time_array = parser._parse('2000-02-01 12:13:14', :date)
|
227
|
+
time_array = parser._parse('2000-02-01 12:13:14')
|
155
228
|
time_array.should == [2000,2,1,12,13,14,nil,nil]
|
156
229
|
end
|
157
230
|
end
|
158
231
|
|
159
|
-
context "with
|
232
|
+
context "with type" do
|
160
233
|
it "should return date array from date string" do
|
161
|
-
time_array = parser._parse('2000-02-01')
|
234
|
+
time_array = parser._parse('2000-02-01', :date)
|
162
235
|
time_array.should == [2000,2,1,nil,nil,nil,nil,nil]
|
163
236
|
end
|
164
237
|
|
238
|
+
it "should not return time array from time string for :date type" do
|
239
|
+
time_array = parser._parse('12:13:14', :date)
|
240
|
+
time_array.should == nil
|
241
|
+
end
|
242
|
+
|
165
243
|
it "should return time array from time string" do
|
166
244
|
time_array = parser._parse('12:13:14', :time)
|
167
245
|
time_array.should == [nil,nil,nil,12,13,14,nil,nil]
|
168
246
|
end
|
169
247
|
|
170
|
-
it "should return
|
171
|
-
time_array = parser._parse('2000-02-01
|
248
|
+
it "should not return date array from date string for :time type" do
|
249
|
+
time_array = parser._parse('2000-02-01', :time)
|
250
|
+
time_array.should == nil
|
251
|
+
end
|
252
|
+
|
253
|
+
it "should return datetime array from datetime string when type is date" do
|
254
|
+
time_array = parser._parse('2000-02-01 12:13:14', :date)
|
172
255
|
time_array.should == [2000,2,1,12,13,14,nil,nil]
|
173
256
|
end
|
174
257
|
|
175
258
|
it "should return date array from date string when type is datetime" do
|
176
|
-
time_array = parser._parse('2000-02-01')
|
259
|
+
time_array = parser._parse('2000-02-01', :datetime)
|
177
260
|
time_array.should == [2000,2,1,nil,nil,nil,nil,nil]
|
178
261
|
end
|
179
262
|
|
180
|
-
it "should return
|
181
|
-
time_array = parser._parse('
|
182
|
-
time_array.should ==
|
263
|
+
it "should not return time array from time string when type is datetime" do
|
264
|
+
time_array = parser._parse('12:13:14', :datetime)
|
265
|
+
time_array.should == nil
|
183
266
|
end
|
184
267
|
end
|
185
268
|
|
@@ -220,13 +303,6 @@ describe Timeliness::Parser do
|
|
220
303
|
end
|
221
304
|
end
|
222
305
|
|
223
|
-
it "should return nil if time hour is out of range for AM meridian" do
|
224
|
-
time_array = parser._parse('13:14 am', :time)
|
225
|
-
time_array.should == nil
|
226
|
-
time_array = parser._parse('00:14 am', :time)
|
227
|
-
time_array.should == nil
|
228
|
-
end
|
229
|
-
|
230
306
|
context "with :format option" do
|
231
307
|
it "should return values if string matches specified format" do
|
232
308
|
time_array = parser._parse('2000-02-01 12:13:14', :datetime, :format => 'yyyy-mm-dd hh:nn:ss')
|
@@ -327,9 +403,6 @@ describe Timeliness::Parser do
|
|
327
403
|
end
|
328
404
|
|
329
405
|
describe "current_date" do
|
330
|
-
before(:all) do
|
331
|
-
Timecop.freeze(2010,1,1,0,0,0)
|
332
|
-
end
|
333
406
|
|
334
407
|
context "with no options" do
|
335
408
|
it 'should return date_for_time_type values with no options' do
|
@@ -371,9 +444,9 @@ describe Timeliness::Parser do
|
|
371
444
|
current_date(:zone => 'London').should == date_array
|
372
445
|
end
|
373
446
|
end
|
447
|
+
end
|
374
448
|
|
375
|
-
|
376
|
-
|
377
|
-
end
|
449
|
+
after(:all) do
|
450
|
+
Timecop.return
|
378
451
|
end
|
379
452
|
end
|