Class: Runt::PDate

Inherits:
DateTime
  • Object
show all
Includes:
Comparable, DPrecision
Defined in:
lib/runt/pdate.rb

Overview

:title:PDate

PDate

Date and DateTime with explicit precision.

Based the pattern[http://martinfowler.com/ap2/timePoint.html] by Martin Fowler.

Author

Matthew Lipper

Constant Summary

Constants included from DPrecision

DPrecision::DAY, DPrecision::DEFAULT, DPrecision::HOUR, DPrecision::MILLI, DPrecision::MIN, DPrecision::MONTH, DPrecision::SEC, DPrecision::WEEK, DPrecision::YEAR

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DPrecision

explode, to_p

Instance Attribute Details

#date_precisionObject

Returns the value of attribute date_precision.



22
23
24
# File 'lib/runt/pdate.rb', line 22

def date_precision
  @date_precision
end

Class Method Details

.civil(*args) ⇒ Object Also known as: new



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/runt/pdate.rb', line 26

def civil(*args)
  precision=nil
  if(args[0].instance_of?(DPrecision::Precision))
    precision = args.shift
  else
    return PDate::sec(*args)
  end
  pdate = super(*args)
  pdate.date_precision = precision
  pdate
end

.day(yr, mon, day, *ignored) ⇒ Object



125
126
127
# File 'lib/runt/pdate.rb', line 125

def PDate.day( yr,mon,day,*ignored )
  PDate.civil(DAY, yr, mon, day )
end

.default(*args) ⇒ Object



146
147
148
# File 'lib/runt/pdate.rb', line 146

def PDate.default(*args)
  PDate.civil(DEFAULT, *args)
end

.hour(yr, mon, day, hr = HOUR.min_value, *ignored) ⇒ Object



129
130
131
# File 'lib/runt/pdate.rb', line 129

def PDate.hour( yr,mon,day,hr=HOUR.min_value,*ignored )
  PDate.civil(HOUR, yr, mon, day,hr,MIN.min_value, SEC.min_value)
end

.millisecond(yr, mon, day, hr, min, sec, ms, *ignored) ⇒ Object



141
142
143
144
# File 'lib/runt/pdate.rb', line 141

def PDate.millisecond( yr,mon,day,hr,min,sec,ms,*ignored )
  PDate.civil(SEC, yr, mon, day,hr,min, sec, ms, *ignored)
  #raise "Not implemented yet."
end

.min(yr, mon, day, hr = HOUR.min_value, min = MIN.min_value, *ignored) ⇒ Object



133
134
135
# File 'lib/runt/pdate.rb', line 133

def PDate.min( yr,mon,day,hr=HOUR.min_value,min=MIN.min_value,*ignored )
  PDate.civil(MIN, yr, mon, day,hr,min, SEC.min_value)
end

.month(yr, mon, *ignored) ⇒ Object



111
112
113
# File 'lib/runt/pdate.rb', line 111

def PDate.month( yr,mon,*ignored )
  PDate.civil(MONTH, yr, mon, DAY.min_value  )
end

.parse(*args) ⇒ Object



38
39
40
41
42
43
# File 'lib/runt/pdate.rb', line 38

def parse(*args)
  opts = args.last.is_a?(Hash) ? args.pop : {}
  pdate = super(*args)
  pdate.date_precision = opts[:precision] || opts[:date_precision]
  pdate
end

.sec(yr, mon, day, hr = HOUR.min_value, min = MIN.min_value, sec = SEC.min_value, *ignored) ⇒ Object



137
138
139
# File 'lib/runt/pdate.rb', line 137

def PDate.sec( yr,mon,day,hr=HOUR.min_value,min=MIN.min_value,sec=SEC.min_value,*ignored )
  PDate.civil(SEC, yr, mon, day,hr,min, sec)
end

.week(yr, mon, day, *ignored) ⇒ Object



115
116
117
118
119
120
121
122
123
# File 'lib/runt/pdate.rb', line 115

def PDate.week( yr,mon,day,*ignored )
  #LJK: need to calculate which week this day implies,
  #and then move the day back to the *first* day in that week;
  #note that since rfc2445 defaults to weekstart=monday, I'm 
  #going to use commercial day-of-week
  raw = PDate.day(yr, mon, day)
  cooked = PDate.commercial(raw.cwyear, raw.cweek, 1)
  PDate.civil(WEEK, cooked.year, cooked.month, cooked.day)
end

.year(yr, *ignored) ⇒ Object



107
108
109
# File 'lib/runt/pdate.rb', line 107

def PDate.year(yr,*ignored)
  PDate.civil(YEAR, yr, MONTH.min_value, DAY.min_value  )
end

Instance Method Details

#+(n) ⇒ Object

Raises:

  • (TypeError)


53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/runt/pdate.rb', line 53

def + (n)
  raise TypeError, 'expected numeric' unless n.kind_of?(Numeric)
	  ndays = n
  case @date_precision
  when YEAR then
    return DPrecision::to_p(PDate::civil(year+n,month,day),@date_precision)
  when MONTH then
    return DPrecision::to_p((self.to_date>>n),@date_precision)
  when WEEK then
    ndays = n*7	
  when DAY then
    ndays = n
  when HOUR then
    ndays = n*(1.to_r/24)
  when MIN then
    ndays = n*(1.to_r/1440)
  when SEC then
    ndays = n*(1.to_r/86400)
  when MILLI then
    ndays = n*(1.to_r/86400000)
  end
	  DPrecision::to_p((self.to_date + ndays),@date_precision)
end

#-(x) ⇒ Object

Raises:

  • (TypeError)


77
78
79
80
81
82
83
84
85
# File 'lib/runt/pdate.rb', line 77

def - (x)
  case x
  when Numeric then
    return self+(-x)
  when Date then
	 	return super(DPrecision::to_p(x,@date_precision))
  end
  raise TypeError, 'expected numeric or date'
end

#<=>(other) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
# File 'lib/runt/pdate.rb', line 87

def <=> (other)
  result = nil
	  raise "I'm broken #{self.to_s}" if @date_precision.nil?
  if(!other.nil? && other.respond_to?("date_precision") && other.date_precision>@date_precision)
    result = super(DPrecision::to_p(other,@date_precision))
  else
    result = super(other)
  end
  puts "self<#{self.to_s}><=>other<#{other.to_s}> => #{result}" if $DEBUG
  result
end

#include?(expr) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/runt/pdate.rb', line 49

def include?(expr)
  eql?(expr)
end

#marshal_dumpObject

Custom dump which preserves DatePrecision

Author

Jodi Showers



156
157
158
# File 'lib/runt/pdate.rb', line 156

def marshal_dump
  [date_precision, ajd, start, offset]
end

#marshal_load(dumped_obj) ⇒ Object

Custom load which preserves DatePrecision

Author

Jodi Showers



166
167
168
# File 'lib/runt/pdate.rb', line 166

def marshal_load(dumped_obj)
  @date_precision, @ajd, @sg, @of=dumped_obj
end

#succObject



99
100
101
# File 'lib/runt/pdate.rb', line 99

def succ
  result = self + 1
end

#to_dateObject



103
104
105
# File 'lib/runt/pdate.rb', line 103

def to_date
  (self.date_precision > DAY) ? DateTime.new(self.year,self.month,self.day,self.hour,self.min,self.sec) : Date.new(self.year, self.month, self.day)
end