summaryrefslogtreecommitdiffstats
path: root/spec/age.rb
blob: 99a09c7c388caa2a858dd97e077f2114f671d740 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#! /usr/bin/env ruby
# -*- coding: UTF-8 -*-

require 'ayk/age'
#
def age_spec(birth,dy,dm,dd)
    at = ((birth>>(12*dy))>>dm)+dd
#    puts at.strftime "%d.%m.%Y"
    y,m,d,h,i,s = birth.age(at)
#    puts "#{y} #{m} #{d}"
    y.should eql dy
    m.should eql dm
    d.should eql dd
end
#
describe DateTime do
    #
    before(:all) do
        @b = DateTime.civil 2009,7,27,4,40,0 # Róisín Mannion-Zurcher [IRL|CH] )
    end
    it "should pass time checks" do
        y,m,d,h,i,s = @b.age( DateTime.civil(2009,7,27,4,40,0) )
        h.should eql 0
        i.should eql 0
        s.should eql 0
        y,m,d,h,i,s = @b.age( DateTime.civil(2009,7,27,7,2,2) )
        h.should eql 2
        i.should eql 22
        s.should eql 2
    end
    it "simple checks" do
        age_spec(@b,0,0,0)
        age_spec(@b,0,0,1)
        age_spec(@b,0,1,0)
        age_spec(@b,0,1,1)
        age_spec(@b,1,0,0)
        age_spec(@b,1,0,1)
        age_spec(@b,1,1,0)
        age_spec(@b,1,1,1)
    end
    it "should pass day computations" do
        at = ((@b>>(12*0))>>15)+3
        y,m,d,h,i,s = @b.age(at)
        y.should eql 1
        m.should eql 3
        d.should eql 3
        at = ((@b>>(12*0))>>1)+366
        y,m,d,h,i,s = @b.age(at)
        y.should eql 1
        m.should eql 1
        d.should eql 1
        at = ((@b>>(12*5))>>10)+15
        y,m,d,h,i,s = @b.age(at)
        y.should eql 5
        m.should eql 10
        d.should eql 15
        at = ((@b>>(12*0))>>0)+750
        y,m,d,h,i,s = @b.age(at)
        y.should eql 2
        m.should eql 0
        d.should eql 20
    end
    it "should pass more complicated one" do
        at = DateTime.civil 2010,7,13,4,40,0 # Corina Mannion
        y,m,d,h,i,s = @b.age(at)
        y.should eql 0
        m.should eql 11
        d.should eql 16
        h.should eql 0
        i.should eql 0
        s.should eql 0
        at = DateTime.civil 2010,7,13,0,0,0 # Corina Mannion
        y,m,d,h,i,s = @b.age(at)
        y.should eql 0
        m.should eql 11
        d.should eql 15
        h.should eql 19
        i.should eql 20
        s.should eql 0
    end
end


#(1..365).each do |n|
#    dt = birth.age( birth+n)
#    puts "#{n} => #{dt[1]} #{dt[2]}"
#end