runt 0.3.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,137 +1,141 @@
1
- #!/usr/bin/env ruby
2
-
3
- require 'runt'
4
- require 'date'
5
-
6
- module Runt
7
-
8
- # :title:DPrecision
9
- # == DPrecision
10
- # Module providing automatic precisioning of Date, DateTime, and PDate classes.
11
- #
12
- # Inspired by a <tt>pattern</tt>[http://martinfowler.com/ap2/timePoint.html] by Martin Fowler.
13
- #
14
- #
15
- # Author:: Matthew Lipper
16
- module DPrecision
17
-
18
- def DPrecision.to_p(date,prec=DEFAULT)
19
-
20
- case prec
21
- when MIN then PDate.min(*DPrecision.explode(date,prec))
22
- when DAY then PDate.day(*DPrecision.explode(date,prec))
23
- when HOUR then PDate.hour(*DPrecision.explode(date,prec))
24
- when MONTH then PDate.month(*DPrecision.explode(date,prec))
25
- when YEAR then PDate.year(*DPrecision.explode(date,prec))
26
- when SEC then PDate.sec(*DPrecision.explode(date,prec))
27
- when MILLI then raise "Not implemented."
28
- else PDate.default(*DPrecision.explode(date,prec))
29
- end
30
- end
31
-
32
- def DPrecision.explode(date,prec)
33
- result = [date.year,date.month,date.day]
34
- if(date.respond_to?("hour"))
35
- result << date.hour << date.min << date.sec
36
- else
37
- result << 0 << 0 << 0
38
- end
39
- result
40
- end
41
-
42
- #Simple value class for keeping track of precisioned dates
43
- class Precision
44
- include Comparable
45
-
46
- attr_reader :precision
47
- private_class_method :new
48
-
49
- #Some constants w/arbitrary integer values used internally for comparisions
50
- YEAR_PREC = 0
51
- MONTH_PREC = 1
52
- DAY_PREC = 2
53
- HOUR_PREC = 3
54
- MIN_PREC = 4
55
- SEC_PREC = 5
56
- MILLI_PREC = 6
57
-
58
- #String values for display
59
- LABEL = { YEAR_PREC => "YEAR",
60
- MONTH_PREC => "MONTH",
61
- DAY_PREC => "DAY",
62
- HOUR_PREC => "HOUR",
63
- MIN_PREC => "MIN",
64
- SEC_PREC => "SEC",
65
- MILLI_PREC => "MILLI"}
66
-
67
- #Minimun values that precisioned fields get set to
68
- FIELD_MIN = { YEAR_PREC => 1,
69
- MONTH_PREC => 1,
70
- DAY_PREC => 1,
71
- HOUR_PREC => 0,
72
- MIN_PREC => 0,
73
- SEC_PREC => 0,
74
- MILLI_PREC => 0}
75
-
76
- def Precision.year
77
- new(YEAR_PREC)
78
- end
79
-
80
- def Precision.month
81
- new(MONTH_PREC)
82
- end
83
-
84
- def Precision.day
85
- new(DAY_PREC)
86
- end
87
-
88
- def Precision.hour
89
- new(HOUR_PREC)
90
- end
91
-
92
- def Precision.min
93
- new(MIN_PREC)
94
- end
95
-
96
- def Precision.sec
97
- new(SEC_PREC)
98
- end
99
-
100
- def Precision.millisec
101
- new(MILLI_PREC)
102
- end
103
-
104
- def min_value()
105
- FIELD_MIN[@precision]
106
- end
107
-
108
- def initialize(prec)
109
- @precision = prec
110
- end
111
-
112
- def <=>(other)
113
- self.precision <=> other.precision
114
- end
115
-
116
- def ===(other)
117
- self.precision == other.precision
118
- end
119
-
120
- def to_s
121
- "DPrecision::#{LABEL[@precision]}"
122
- end
123
- end
124
-
125
- #Pseudo Singletons:
126
- YEAR = Precision.year
127
- MONTH = Precision.month
128
- DAY = Precision.day
129
- HOUR = Precision.hour
130
- MIN = Precision.min
131
- SEC = Precision.sec
132
- MILLI = Precision.millisec
133
- DEFAULT=MIN
134
-
135
- end
136
-
137
- end
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'runt'
4
+ require 'date'
5
+
6
+ module Runt
7
+
8
+ # :title:DPrecision
9
+ # == DPrecision
10
+ # Module providing automatic precisioning of Date, DateTime, and PDate classes.
11
+ #
12
+ # Inspired by a <tt>pattern</tt>[http://martinfowler.com/ap2/timePoint.html] by Martin Fowler.
13
+ #
14
+ #
15
+ # Author:: Matthew Lipper
16
+ module DPrecision
17
+
18
+ def DPrecision.to_p(date,prec=DEFAULT)
19
+
20
+ case prec
21
+ when MIN then PDate.min(*DPrecision.explode(date,prec))
22
+ when DAY then PDate.day(*DPrecision.explode(date,prec))
23
+ when HOUR then PDate.hour(*DPrecision.explode(date,prec))
24
+ when MONTH then PDate.month(*DPrecision.explode(date,prec))
25
+ when YEAR then PDate.year(*DPrecision.explode(date,prec))
26
+ when SEC then PDate.sec(*DPrecision.explode(date,prec))
27
+ when MILLI then date #raise "Not implemented."
28
+ else PDate.default(*DPrecision.explode(date,prec))
29
+ end
30
+ end
31
+
32
+ def DPrecision.explode(date,prec)
33
+ result = [date.year,date.month,date.day]
34
+ if(date.respond_to?("hour"))
35
+ result << date.hour << date.min << date.sec
36
+ else
37
+ result << 0 << 0 << 0
38
+ end
39
+ result
40
+ end
41
+
42
+ #Simple value class for keeping track of precisioned dates
43
+ class Precision
44
+ include Comparable
45
+
46
+ attr_reader :precision
47
+ private_class_method :new
48
+
49
+ #Some constants w/arbitrary integer values used internally for comparisions
50
+ YEAR_PREC = 0
51
+ MONTH_PREC = 1
52
+ DAY_PREC = 2
53
+ HOUR_PREC = 3
54
+ MIN_PREC = 4
55
+ SEC_PREC = 5
56
+ MILLI_PREC = 6
57
+
58
+ #String values for display
59
+ LABEL = { YEAR_PREC => "YEAR",
60
+ MONTH_PREC => "MONTH",
61
+ DAY_PREC => "DAY",
62
+ HOUR_PREC => "HOUR",
63
+ MIN_PREC => "MINUTE",
64
+ SEC_PREC => "SECOND",
65
+ MILLI_PREC => "MILLISECOND"}
66
+
67
+ #Minimun values that precisioned fields get set to
68
+ FIELD_MIN = { YEAR_PREC => 1,
69
+ MONTH_PREC => 1,
70
+ DAY_PREC => 1,
71
+ HOUR_PREC => 0,
72
+ MIN_PREC => 0,
73
+ SEC_PREC => 0,
74
+ MILLI_PREC => 0}
75
+
76
+ def Precision.year
77
+ new(YEAR_PREC)
78
+ end
79
+
80
+ def Precision.month
81
+ new(MONTH_PREC)
82
+ end
83
+
84
+ def Precision.day
85
+ new(DAY_PREC)
86
+ end
87
+
88
+ def Precision.hour
89
+ new(HOUR_PREC)
90
+ end
91
+
92
+ def Precision.min
93
+ new(MIN_PREC)
94
+ end
95
+
96
+ def Precision.sec
97
+ new(SEC_PREC)
98
+ end
99
+
100
+ def Precision.millisec
101
+ new(MILLI_PREC)
102
+ end
103
+
104
+ def min_value()
105
+ FIELD_MIN[@precision]
106
+ end
107
+
108
+ def initialize(prec)
109
+ @precision = prec
110
+ end
111
+
112
+ def <=>(other)
113
+ self.precision <=> other.precision
114
+ end
115
+
116
+ def ===(other)
117
+ self.precision == other.precision
118
+ end
119
+
120
+ def to_s
121
+ "DPrecision::#{self.label}"
122
+ end
123
+
124
+ def label
125
+ LABEL[@precision]
126
+ end
127
+ end
128
+
129
+ #Pseudo Singletons:
130
+ YEAR = Precision.year
131
+ MONTH = Precision.month
132
+ DAY = Precision.day
133
+ HOUR = Precision.hour
134
+ MIN = Precision.min
135
+ SEC = Precision.sec
136
+ MILLI = Precision.millisec
137
+ DEFAULT=MIN
138
+
139
+ end
140
+
141
+ end
@@ -1,126 +1,153 @@
1
- #!/usr/bin/env ruby
2
-
3
- require 'date'
4
- require 'runt'
5
-
6
-
7
- module Runt
8
-
9
- # :title:PDate
10
- # == PDate
11
- # Date and DateTime with explicit precision.
12
- #
13
- # Based the <tt>pattern</tt>[http://martinfowler.com/ap2/timePoint.html] by Martin Fowler.
14
- #
15
- #
16
- # Author:: Matthew Lipper
17
- class PDate < DateTime
18
- include DPrecision
19
-
20
- attr_accessor :date_precision
21
-
22
- class << self
23
- alias_method :old_civil, :civil
24
-
25
- def civil(*args)
26
- if(args[0].instance_of?(DPrecision::Precision))
27
- precision = args.shift
28
- else
29
- return PDate::sec(*args)
30
- end
31
- _civil = old_civil(*args)
32
- _civil.date_precision = precision
33
- _civil
34
- end
35
- end
36
-
37
- class << self; alias_method :new, :civil end
38
-
39
- def + (n)
40
- raise TypeError, 'expected numeric' unless n.kind_of?(Numeric)
41
- case @date_precision
42
- when YEAR then
43
- return DPrecision::to_p(PDate::civil(year+n,month,day),@date_precision)
44
- when MONTH then
45
- current_date = self.class.to_date(self)
46
- return DPrecision::to_p((current_date>>n),@date_precision)
47
- when DAY then
48
- return new_self_plus(n)
49
- when HOUR then
50
- return new_self_plus(n){ |n| n = (n*(1.to_r/24) ) }
51
- when MIN then
52
- return new_self_plus(n){ |n| n = (n*(1.to_r/1440) ) }
53
- when SEC then
54
- return new_self_plus(n){ |n| n = (n*(1.to_r/86400) ) }
55
- end
56
- end
57
-
58
- def - (x)
59
- case x
60
- when Numeric then
61
- return self+(-x)
62
- #FIXME!!
63
- when Date; return @ajd - x.ajd
64
- end
65
- raise TypeError, 'expected numeric or date'
66
- end
67
-
68
- def <=> (other)
69
- result = nil
70
- if(other.respond_to?("date_precision") && other.date_precision>@date_precision)
71
- result = super(DPrecision::to_p(other,@date_precision))
72
- else
73
- result = super(other)
74
- end
75
- puts "#{self.to_s}<=>#{other.to_s} => #{result}" if $DEBUG
76
- result
77
- end
78
-
79
- def new_self_plus(n)
80
- if(block_given?)
81
- n=yield(n)
82
- end
83
- return DPrecision::to_p(self.class.new0(@ajd + n, @of, @sg),@date_precision)
84
- end
85
-
86
- def PDate.to_date(pdate)
87
- if( pdate.date_precision > DPrecision::DAY) then
88
- DateTime.new(pdate.year,pdate.month,pdate.day,pdate.hour,pdate.min,pdate.sec)
89
- end
90
- return Date.new(pdate.year,pdate.month,pdate.day)
91
- end
92
-
93
- def PDate.year(yr,*ignored)
94
- PDate.civil(YEAR, yr, MONTH.min_value, DAY.min_value )
95
- end
96
-
97
- def PDate.month( yr,mon,*ignored )
98
- PDate.civil(MONTH, yr, mon, DAY.min_value )
99
- end
100
-
101
- def PDate.day( yr,mon,day,*ignored )
102
- PDate.civil(DAY, yr, mon, day )
103
- end
104
-
105
- def PDate.hour( yr,mon,day,hr=HOUR.min_value,*ignored )
106
- PDate.civil(HOUR, yr, mon, day,hr,MIN.min_value, SEC.min_value)
107
- end
108
-
109
- def PDate.min( yr,mon,day,hr=HOUR.min_value,min=MIN.min_value,*ignored )
110
- PDate.civil(MIN, yr, mon, day,hr,min, SEC.min_value)
111
- end
112
-
113
- def PDate.sec( yr,mon,day,hr=HOUR.min_value,min=MIN.min_value,sec=SEC.min_value,*ignored )
114
- PDate.civil(SEC, yr, mon, day,hr,min, sec)
115
- end
116
-
117
- def PDate.millisecond( yr,mon,day,hr,min,sec,ms,*ignored )
118
- raise "Not implemented yet."
119
- end
120
-
121
- def PDate.default(*args)
122
- PDate.civil(DEFAULT, *args)
123
- end
124
-
125
- end
126
- end
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'date'
4
+ require 'runt'
5
+
6
+
7
+ module Runt
8
+
9
+
10
+ # :title:PDate
11
+ # == PDate
12
+ # Date and DateTime with explicit precision.
13
+ #
14
+ # Based the <tt>pattern</tt>[http://martinfowler.com/ap2/timePoint.html] by Martin Fowler.
15
+ #
16
+ #
17
+ # Author:: Matthew Lipper
18
+ class PDate < DateTime
19
+ include DPrecision
20
+
21
+ attr_accessor :date_precision
22
+
23
+ class << self
24
+ alias_method :old_civil, :civil
25
+
26
+ def civil(*args)
27
+ precision=nil
28
+ if(args[0].instance_of?(DPrecision::Precision))
29
+ precision = args.shift
30
+ else
31
+ return PDate::sec(*args)
32
+ end
33
+ _civil = old_civil(*args)
34
+ _civil.date_precision = precision
35
+ _civil
36
+ end
37
+ end
38
+
39
+ class << self; alias_method :new, :civil end
40
+
41
+ def include?(expr)
42
+ eql?(expr)
43
+ end
44
+
45
+ def + (n)
46
+ raise TypeError, 'expected numeric' unless n.kind_of?(Numeric)
47
+ case @date_precision
48
+ when YEAR then
49
+ return DPrecision::to_p(PDate::civil(year+n,month,day),@date_precision)
50
+ when MONTH then
51
+ current_date = self.class.to_date(self)
52
+ return DPrecision::to_p((current_date>>n),@date_precision)
53
+ when DAY then
54
+ return new_self_plus(n)
55
+ when HOUR then
56
+ return new_self_plus(n){ |n| n = (n*(1.to_r/24) ) }
57
+ when MIN then
58
+ return new_self_plus(n){ |n| n = (n*(1.to_r/1440) ) }
59
+ when SEC then
60
+ return new_self_plus(n){ |n| n = (n*(1.to_r/86400) ) }
61
+ when MILLI then
62
+ return self
63
+ end
64
+ end
65
+
66
+ def - (x)
67
+ case x
68
+ when Numeric then
69
+ return self+(-x)
70
+ #FIXME!!
71
+ when Date; return @ajd - x.ajd
72
+ end
73
+ raise TypeError, 'expected numeric or date'
74
+ end
75
+
76
+ def <=> (other)
77
+ result = nil
78
+ if(other.respond_to?("date_precision") && other.date_precision>@date_precision)
79
+ result = super(DPrecision::to_p(other,@date_precision))
80
+ else
81
+ result = super(other)
82
+ end
83
+ puts "#{self.to_s}<=>#{other.to_s} => #{result}" if $DEBUG
84
+ result
85
+ end
86
+
87
+ def new_self_plus(n)
88
+ if(block_given?)
89
+ n=yield(n)
90
+ end
91
+ return DPrecision::to_p(self.class.new0(@ajd + n, @of, @sg),@date_precision)
92
+ end
93
+
94
+ def PDate.to_date(pdate)
95
+ if( pdate.date_precision > DPrecision::DAY) then
96
+ DateTime.new(pdate.year,pdate.month,pdate.day,pdate.hour,pdate.min,pdate.sec)
97
+ end
98
+ return Date.new(pdate.year,pdate.month,pdate.day)
99
+ end
100
+
101
+ def PDate.year(yr,*ignored)
102
+ PDate.civil(YEAR, yr, MONTH.min_value, DAY.min_value )
103
+ end
104
+
105
+ def PDate.month( yr,mon,*ignored )
106
+ PDate.civil(MONTH, yr, mon, DAY.min_value )
107
+ end
108
+
109
+ def PDate.day( yr,mon,day,*ignored )
110
+ PDate.civil(DAY, yr, mon, day )
111
+ end
112
+
113
+ def PDate.hour( yr,mon,day,hr=HOUR.min_value,*ignored )
114
+ PDate.civil(HOUR, yr, mon, day,hr,MIN.min_value, SEC.min_value)
115
+ end
116
+
117
+ def PDate.min( yr,mon,day,hr=HOUR.min_value,min=MIN.min_value,*ignored )
118
+ PDate.civil(MIN, yr, mon, day,hr,min, SEC.min_value)
119
+ end
120
+
121
+ def PDate.sec( yr,mon,day,hr=HOUR.min_value,min=MIN.min_value,sec=SEC.min_value,*ignored )
122
+ PDate.civil(SEC, yr, mon, day,hr,min, sec)
123
+ end
124
+
125
+ def PDate.millisecond( yr,mon,day,hr,min,sec,ms,*ignored )
126
+ PDate.civil(SEC, yr, mon, day,hr,min, sec, ms, *ignored)
127
+ #raise "Not implemented yet."
128
+ end
129
+
130
+ def PDate.default(*args)
131
+ PDate.civil(DEFAULT, *args)
132
+ end
133
+
134
+ #
135
+ # Custom dump which preserves DatePrecision
136
+ #
137
+ # Author:: Jodi Showers
138
+ #
139
+ def marshal_dump
140
+ [date_precision, ajd, sg, of]
141
+ end
142
+
143
+ #
144
+ # Custom load which preserves DatePrecision
145
+ #
146
+ # Author:: Jodi Showers
147
+ #
148
+ def marshal_load(dumped_obj)
149
+ @date_precision, @ajd, @sg, @of=dumped_obj
150
+ end
151
+
152
+ end
153
+ end