time_spanner 1.0.0

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.
Files changed (53) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +18 -0
  3. data/.ruby-gemset +1 -0
  4. data/.ruby-version +1 -0
  5. data/Gemfile +13 -0
  6. data/LICENSE.txt +22 -0
  7. data/README.md +60 -0
  8. data/Rakefile +11 -0
  9. data/lib/time_spanner.rb +8 -0
  10. data/lib/time_spanner/duration_chain.rb +55 -0
  11. data/lib/time_spanner/errors.rb +8 -0
  12. data/lib/time_spanner/time_span.rb +34 -0
  13. data/lib/time_spanner/time_unit_collector.rb +59 -0
  14. data/lib/time_spanner/time_units.rb +17 -0
  15. data/lib/time_spanner/time_units/base/calendar_unit.rb +30 -0
  16. data/lib/time_spanner/time_units/base/time_unit.rb +39 -0
  17. data/lib/time_spanner/time_units/base/unit.rb +31 -0
  18. data/lib/time_spanner/time_units/century.rb +30 -0
  19. data/lib/time_spanner/time_units/day.rb +25 -0
  20. data/lib/time_spanner/time_units/decade.rb +25 -0
  21. data/lib/time_spanner/time_units/hour.rb +15 -0
  22. data/lib/time_spanner/time_units/microsecond.rb +15 -0
  23. data/lib/time_spanner/time_units/millennium.rb +25 -0
  24. data/lib/time_spanner/time_units/millisecond.rb +15 -0
  25. data/lib/time_spanner/time_units/minute.rb +15 -0
  26. data/lib/time_spanner/time_units/month.rb +25 -0
  27. data/lib/time_spanner/time_units/nanosecond.rb +15 -0
  28. data/lib/time_spanner/time_units/second.rb +15 -0
  29. data/lib/time_spanner/time_units/week.rb +25 -0
  30. data/lib/time_spanner/time_units/year.rb +25 -0
  31. data/lib/time_spanner/version.rb +3 -0
  32. data/test/test_helper.rb +9 -0
  33. data/test/time_spanner/duration_chain_test.rb +267 -0
  34. data/test/time_spanner/time_spanner_test.rb +27 -0
  35. data/test/time_spanner/time_unit_collector_test.rb +66 -0
  36. data/test/time_spanner/time_units/base/calendar_unit_test.rb +17 -0
  37. data/test/time_spanner/time_units/base/time_unit_test.rb +18 -0
  38. data/test/time_spanner/time_units/base/unit_test.rb +37 -0
  39. data/test/time_spanner/time_units/century_test.rb +55 -0
  40. data/test/time_spanner/time_units/day_test.rb +84 -0
  41. data/test/time_spanner/time_units/decade_test.rb +67 -0
  42. data/test/time_spanner/time_units/hour_test.rb +56 -0
  43. data/test/time_spanner/time_units/microsecond_test.rb +56 -0
  44. data/test/time_spanner/time_units/millennium_test.rb +69 -0
  45. data/test/time_spanner/time_units/millisecond_test.rb +56 -0
  46. data/test/time_spanner/time_units/minute_test.rb +84 -0
  47. data/test/time_spanner/time_units/month_test.rb +68 -0
  48. data/test/time_spanner/time_units/nanosecond_test.rb +40 -0
  49. data/test/time_spanner/time_units/second_test.rb +56 -0
  50. data/test/time_spanner/time_units/week_test.rb +55 -0
  51. data/test/time_spanner/time_units/year_test.rb +91 -0
  52. data/time_spanner.gemspec +23 -0
  53. metadata +143 -0
@@ -0,0 +1,69 @@
1
+ require 'test_helper'
2
+
3
+ module TimeSpanner
4
+ module TimeUnits
5
+
6
+ class MillenniumTest < TestCase
7
+
8
+ it 'initializes' do
9
+ millennium = Millennium.new
10
+
11
+ assert millennium.kind_of?(CalendarUnit)
12
+ assert_equal 1, millennium.position
13
+ assert_equal :millenniums, millennium.plural_name
14
+ end
15
+
16
+ it 'calculates only rest (1 nanosecond in seconds)' do
17
+ from = Time.parse '2012-06-12 00:00:00'
18
+ time_at_millenniums = Time.parse '3012-06-12 00:00:00'
19
+ to = Time.at time_at_millenniums.to_r, -0.001
20
+ duration = to.to_r - from.to_r
21
+ millennium = Millennium.new
22
+
23
+ millennium.calculate duration, to
24
+
25
+ assert_equal 0, millennium.amount
26
+ assert millennium.rest > 0
27
+ end
28
+
29
+ it 'calculates with rest (1 minute in seconds)' do
30
+ from = Time.parse('3013-01-01 00:00:00')
31
+ to = Time.parse('5013-01-01 00:01:00')
32
+ duration = to.to_r - from.to_r
33
+ millennium = Millennium.new
34
+
35
+ millennium.calculate duration, to
36
+
37
+ assert_equal 2, millennium.amount
38
+ assert_equal 60, millennium.rest
39
+ end
40
+
41
+ it 'calculates with rest (1 nanosecond)' do
42
+ from = Time.parse('3013-01-01 00:00:00')
43
+ target_millenniums = Time.parse('5013-01-01 00:00:00')
44
+ to = Time.at(target_millenniums.to_r, 0.001)
45
+ duration = to.to_r - from.to_r
46
+ millennium = Millennium.new
47
+
48
+ millennium.calculate duration, to
49
+
50
+ assert_equal 2, millennium.amount
51
+ assert_equal Rational(1152921504606847, 1152921504606846976000000), millennium.rest
52
+ end
53
+
54
+ it 'should not calculate amount of 2 although units equal' do
55
+ from = Time.parse '2012-06-12 00:00:00'
56
+ time_at_millenniums = Time.parse '4012-06-12 00:00:00'
57
+ to = Time.at time_at_millenniums.to_r, -0.001
58
+ duration = to.to_r - from.to_r
59
+ millennium = Millennium.new
60
+
61
+ millennium.calculate duration, to
62
+
63
+ assert_equal 1, millennium.amount
64
+ assert millennium.rest > 0
65
+ end
66
+
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,56 @@
1
+ require 'test_helper'
2
+
3
+ module TimeSpanner
4
+ module TimeUnits
5
+
6
+ class MillisecondTest < TestCase
7
+
8
+ it 'initializes' do
9
+ millisecond = Millisecond.new
10
+
11
+ assert millisecond.kind_of?(TimeUnit)
12
+ assert_equal 11, millisecond.position
13
+ assert_equal :milliseconds, millisecond.plural_name
14
+ end
15
+
16
+ it 'calculates' do
17
+ from = Time.now
18
+ to = Time.at(from.to_r, 2000.0)
19
+ duration = to.to_r - from.to_r
20
+ millisecond = Millisecond.new
21
+
22
+ millisecond.calculate duration
23
+
24
+ assert_equal 2, millisecond.amount
25
+ assert_equal 0, millisecond.rest
26
+ end
27
+
28
+ it 'calculates with rest (999 nanoseconds in seconds)' do
29
+ from = Time.now
30
+ target_millis = Time.at(from.to_r, 2000.0)
31
+ to = Time.at(target_millis.to_r, 0.999)
32
+ duration = to.to_r - from.to_r
33
+ millisecond = Millisecond.new
34
+
35
+ millisecond.calculate duration
36
+
37
+ assert_equal 2, millisecond.amount
38
+ assert_equal Rational(8998192055486251, 9007199254740992000000), millisecond.rest
39
+ end
40
+
41
+ it 'should not calculate amount of 2 although units equal' do
42
+ from = Time.parse '2012-06-12 02:22:00'
43
+ time_at_milliseconds = Time.at(from.to_r, 2000.0)
44
+ to = Time.at time_at_milliseconds.to_r, -0.001
45
+ duration = to.to_r - from.to_r
46
+ millisecond = Millisecond.new
47
+
48
+ millisecond.calculate duration, to
49
+
50
+ assert_equal 1, millisecond.amount
51
+ assert millisecond.rest > 0
52
+ end
53
+
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,84 @@
1
+ require 'test_helper'
2
+
3
+ module TimeSpanner
4
+ module TimeUnits
5
+
6
+ class MinuteTest < TestCase
7
+
8
+ it 'initializes' do
9
+ minute = Minute.new
10
+
11
+ assert minute.kind_of?(TimeUnit)
12
+ assert_equal 9, minute.position
13
+ assert_equal :minutes, minute.plural_name
14
+ end
15
+
16
+ it 'calculates' do
17
+ from = Time.parse('2013-04-03 00:00:00')
18
+ to = Time.parse('2013-04-03 00:02:00')
19
+ duration = to.to_r - from.to_r
20
+ minute = Minute.new
21
+
22
+ minute.calculate duration
23
+
24
+ assert_equal 2, minute.amount
25
+ assert_equal 0, minute.rest
26
+ end
27
+
28
+ it 'calculates with rest (999 nanoseconds in seconds)' do
29
+ from = Time.parse('2013-04-03 00:00:00')
30
+ target_minutes = Time.parse('2013-04-03 00:02:00')
31
+ to = Time.at(target_minutes.to_r, 0.999)
32
+ duration = to.to_r - from.to_r
33
+ minute = Minute.new
34
+
35
+ minute.calculate duration
36
+
37
+ assert_equal 2, minute.amount
38
+ assert_equal Rational(8998192055486251, 9007199254740992000000), minute.rest
39
+ end
40
+
41
+ it 'should not calculate amount of 2 although units equal' do
42
+ from = Time.parse '2012-06-12 01:12:23'
43
+ time_at_minutes = Time.parse '2012-06-12 01:14:23'
44
+ to = Time.at time_at_minutes.to_r, -0.001
45
+ duration = to.to_r - from.to_r
46
+ minute = Minute.new
47
+
48
+ minute.calculate duration, to
49
+
50
+ assert_equal 1, minute.amount
51
+ assert minute.rest > 0
52
+ end
53
+
54
+ describe 'time zone switches' do
55
+
56
+ it 'switches to summer time' do
57
+ from = DateTime.parse('2013-03-31 01:59:00 CEST').to_time
58
+ to = DateTime.parse('2013-03-31 02:01:00 CEST').to_time
59
+ duration = to.to_r - from.to_r
60
+ minute = Minute.new
61
+
62
+ minute.calculate duration
63
+
64
+ assert_equal 2, minute.amount
65
+ assert_equal 0, minute.rest
66
+ end
67
+
68
+ it 'switches to winter time' do
69
+ from = DateTime.parse('2013-10-31 02:59:00 CEST').to_time
70
+ to = DateTime.parse('2013-10-31 03:01:00 CEST').to_time
71
+ duration = to.to_r - from.to_r
72
+ minute = Minute.new
73
+
74
+ minute.calculate duration
75
+
76
+ assert_equal 2, minute.amount
77
+ assert_equal 0, minute.rest
78
+ end
79
+
80
+ end
81
+
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,68 @@
1
+ require 'test_helper'
2
+
3
+ module TimeSpanner
4
+ module TimeUnits
5
+
6
+ class MonthTest < TestCase
7
+
8
+ it 'initializes' do
9
+ month = Month.new
10
+
11
+ assert month.kind_of?(CalendarUnit)
12
+ assert_equal 5, month.position
13
+ assert_equal :months, month.plural_name
14
+ end
15
+
16
+ it 'calculates without rest' do
17
+ from = Time.parse('2013-04-01 00:00:00')
18
+ to = Time.parse('2013-06-01 00:00:00')
19
+ duration = to.to_r - from.to_r
20
+ month = Month.new
21
+
22
+ month.calculate duration, to
23
+
24
+ assert_equal 2, month.amount
25
+ assert_equal 0, month.rest
26
+ end
27
+
28
+ it 'calculates with rest (999 nanoseconds)' do
29
+ from = Time.parse('2013-04-01 00:00:00')
30
+ target_months = Time.parse('2013-06-01 00:00:00')
31
+ to = Time.at(target_months.to_r, 0.999)
32
+ duration = to.to_r - from.to_r
33
+ month = Month.new
34
+
35
+ month.calculate duration, to
36
+
37
+ assert_equal 2, month.amount
38
+ assert_equal Rational(8998192055486251, 9007199254740992000000), month.rest
39
+ end
40
+
41
+ it 'calculates with rest (19 days in seconds)' do
42
+ from = Time.parse('2013-04-12 00:00:00')
43
+ to = Time.parse('2013-07-31 00:00:00')
44
+ duration = to.to_r - from.to_r
45
+ month = Month.new
46
+
47
+ month.calculate duration, to
48
+
49
+ assert_equal 3, month.amount
50
+ assert_equal 1641600, month.rest
51
+ end
52
+
53
+ it 'should not calculate amount of 2 although units equal' do
54
+ from = Time.parse '2012-06-12 00:00:00'
55
+ time_at_months = Time.parse '2012-08-12 00:00:00'
56
+ to = Time.at time_at_months.to_r, -0.001
57
+ duration = to.to_r - from.to_r
58
+ month = Month.new
59
+
60
+ month.calculate duration, to
61
+
62
+ assert_equal 1, month.amount
63
+ assert month.rest > 0
64
+ end
65
+
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,40 @@
1
+ require 'test_helper'
2
+
3
+ module TimeSpanner
4
+ module TimeUnits
5
+
6
+ class NanosecondTest < TestCase
7
+
8
+ it 'initializes' do
9
+ nanosecond = Nanosecond.new
10
+
11
+ assert nanosecond.kind_of?(TimeUnit)
12
+ assert_equal 13, nanosecond.position
13
+ assert_equal :nanoseconds, nanosecond.plural_name
14
+ end
15
+
16
+ it 'calculates amount' do
17
+ from = Time.now
18
+ to = Time.at(from.to_r, 0.002)
19
+ duration = to.to_r - from.to_r
20
+ nanosecond = Nanosecond.new
21
+
22
+ nanosecond.calculate duration
23
+
24
+ assert_equal 2, nanosecond.amount
25
+ end
26
+
27
+ it 'calculates amount on odd nanoseconds' do
28
+ from = Time.at(DateTime.parse('2013-07-28 00:00:01').to_time, 0.021)
29
+ to = Time.at(DateTime.parse('2013-07-28 00:00:01').to_time, 0.023)
30
+ duration = to.to_r - from.to_r
31
+ nanosecond = Nanosecond.new
32
+
33
+ nanosecond.calculate duration
34
+
35
+ assert_equal 2, nanosecond.amount
36
+ end
37
+
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,56 @@
1
+ require 'test_helper'
2
+
3
+ module TimeSpanner
4
+ module TimeUnits
5
+
6
+ class SecondTest < TestCase
7
+
8
+ it 'initializes' do
9
+ second = Second.new
10
+
11
+ assert second.kind_of?(TimeUnit)
12
+ assert_equal 10, second.position
13
+ assert_equal :seconds, second.plural_name
14
+ end
15
+
16
+ it 'calculates' do
17
+ from = Time.parse('2012-12-14 00:00:00')
18
+ to = Time.parse('2012-12-14 00:00:02')
19
+ duration = to.to_r - from.to_r
20
+ second = Second.new
21
+
22
+ second.calculate duration
23
+
24
+ assert_equal 2, second.amount
25
+ assert_equal 0, second.rest
26
+ end
27
+
28
+ it 'calculates with rest' do
29
+ from = Time.now
30
+ target_seconds = Time.at(from.to_r, 2000000.0)
31
+ to = Time.at(target_seconds.to_r, 0.999)
32
+ duration = to.to_r - from.to_r
33
+ second = Second.new
34
+
35
+ second.calculate duration
36
+
37
+ assert_equal 2, second.amount
38
+ assert_equal Rational(8998192055486251, 9007199254740992000000), second.rest
39
+ end
40
+
41
+ it 'should not calculate amount of 2 although units equal' do
42
+ from = Time.parse '2012-06-12 02:22:22'
43
+ time_at_seconds = Time.parse '2012-06-12 02:22:24'
44
+ to = Time.at time_at_seconds.to_r, -0.001
45
+ duration = to.to_r - from.to_r
46
+ second = Second.new
47
+
48
+ second.calculate duration, to
49
+
50
+ assert_equal 1, second.amount
51
+ assert second.rest > 0
52
+ end
53
+
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,55 @@
1
+ require 'test_helper'
2
+
3
+ module TimeSpanner
4
+ module TimeUnits
5
+
6
+ class WeekTest < TestCase
7
+
8
+ it 'initializes' do
9
+ week = Week.new
10
+
11
+ assert week.kind_of?(CalendarUnit)
12
+ assert_equal 6, week.position
13
+ assert_equal :weeks, week.plural_name
14
+ end
15
+
16
+ it 'calculates without rest' do
17
+ from = Time.parse('2013-04-01 00:00:00')
18
+ to = Time.parse('2013-04-08 00:00:00')
19
+ duration = to.to_r - from.to_r
20
+ week = Week.new
21
+
22
+ week.calculate duration, to
23
+
24
+ assert_equal 1, week.amount
25
+ assert_equal 0, week.rest
26
+ end
27
+
28
+ it 'calculates with rest (6 days and 1 second in seconds)' do
29
+ from = Time.parse('2013-04-01 00:00:00')
30
+ to = Time.parse('2013-04-21 00:00:01')
31
+ duration = to.to_r - from.to_r
32
+ week = Week.new
33
+
34
+ week.calculate duration, to
35
+
36
+ assert_equal 2, week.amount
37
+ assert_equal 518401, week.rest
38
+ end
39
+
40
+ it 'should not calculate amount of 2 although units equal' do
41
+ from = Time.parse '2012-06-01 12:00:00'
42
+ time_at_weeks = Time.parse '2012-06-15 12:00:00'
43
+ to = Time.at time_at_weeks.to_r, -0.001
44
+ duration = to.to_r - from.to_r
45
+ week = Week.new
46
+
47
+ week.calculate duration, to
48
+
49
+ assert_equal 1, week.amount
50
+ assert week.rest > 0
51
+ end
52
+
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,91 @@
1
+ require 'test_helper'
2
+
3
+ module TimeSpanner
4
+ module TimeUnits
5
+
6
+ class YearTest < TestCase
7
+
8
+ it 'initializes' do
9
+ year = Year.new
10
+
11
+ assert year.kind_of?(CalendarUnit)
12
+ assert_equal 4, year.position
13
+ assert_equal :years, year.plural_name
14
+ end
15
+
16
+ it 'calculates without rest' do
17
+ from = Time.parse('2013-04-01 00:00:00')
18
+ to = Time.parse('2015-04-01 00:00:00')
19
+ duration = to.to_r - from.to_r
20
+ year = Year.new
21
+
22
+ year.calculate duration, to
23
+
24
+ assert_equal 2, year.amount
25
+ assert_equal 0, year.rest
26
+ end
27
+
28
+ it 'calculates with rest (11 months in seconds)' do
29
+ from = Time.parse('2013-01-01 00:00:00')
30
+ to = Time.parse('2015-12-01 00:00:00')
31
+ duration = to.to_r - from.to_r
32
+ year = Year.new
33
+
34
+ year.calculate duration, to
35
+
36
+ assert_equal 2, year.amount
37
+ assert_equal 28857600, year.rest
38
+ end
39
+
40
+ it 'calculates with rest (11 months and 2 seconds in seconds)' do
41
+ from = Time.parse('2013-01-01 00:00:02')
42
+ to = Time.parse('2015-12-01 00:00:04')
43
+ duration = to.to_r - from.to_r
44
+ year = Year.new
45
+
46
+ year.calculate duration, to
47
+
48
+ assert_equal 2, year.amount
49
+ assert_equal 28857602, year.rest
50
+ end
51
+
52
+ it 'calculates with rest (1 second)' do
53
+ from = Time.parse('2012-01-01 00:00:00')
54
+ to = Time.parse('4014-01-01 00:00:01')
55
+ duration = to.to_r - from.to_r
56
+ year = Year.new
57
+
58
+ year.calculate duration, to
59
+
60
+ assert_equal 2002, year.amount
61
+ assert_equal 1, year.rest
62
+ end
63
+
64
+ it 'should not calculate amount of 2 although units equal' do
65
+ from = Time.parse '2012-06-25 00:00:00'
66
+ time_at_years = Time.parse '2014-06-25 00:00:00'
67
+ to = Time.at time_at_years.to_r, -0.001
68
+ duration = to.to_r - from.to_r
69
+ year = Year.new
70
+
71
+ year.calculate duration, to
72
+
73
+ assert_equal 1, year.amount
74
+ assert year.rest > 0
75
+ end
76
+
77
+ it 'calculates correctly on exact leap day' do
78
+ from = Time.parse('2012-02-29 00:00:00') # leap year
79
+ to = Time.parse('2013-02-28 00:00:00')
80
+ duration = to.to_r - from.to_r
81
+ year = Year.new
82
+
83
+ year.calculate duration, to
84
+
85
+ assert_equal 1, year.amount
86
+ assert_equal 0, year.rest
87
+ end
88
+
89
+ end
90
+ end
91
+ end