Class: Runt::PDate

Inherits:
DateTime
  • Object
show all
Includes:
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::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.



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

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
  _civil = old_civil(*args)
  _civil.date_precision = precision
  _civil
end

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



109
110
111
# File 'lib/runt/pdate.rb', line 109

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

.default(*args) ⇒ Object



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

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

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



113
114
115
# File 'lib/runt/pdate.rb', line 113

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



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

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



117
118
119
# File 'lib/runt/pdate.rb', line 117

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



105
106
107
# File 'lib/runt/pdate.rb', line 105

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

.old_civilObject



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

alias_method :old_civil, :civil

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



121
122
123
# File 'lib/runt/pdate.rb', line 121

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

.to_date(pdate) ⇒ Object



94
95
96
97
98
99
# File 'lib/runt/pdate.rb', line 94

def PDate.to_date(pdate)
  if( pdate.date_precision > DPrecision::DAY) then
    DateTime.new(pdate.year,pdate.month,pdate.day,pdate.hour,pdate.min,pdate.sec)
  end
  return Date.new(pdate.year,pdate.month,pdate.day)
end

.year(yr, *ignored) ⇒ Object



101
102
103
# File 'lib/runt/pdate.rb', line 101

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

Instance Method Details

#+(n) ⇒ Object

Raises:

  • (TypeError)


45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/runt/pdate.rb', line 45

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

#-(x) ⇒ Object

Raises:

  • (TypeError)


66
67
68
69
70
71
72
73
74
# File 'lib/runt/pdate.rb', line 66

def - (x)
  case x
    when Numeric then
	return self+(-x)
    #FIXME!!
    when Date;    return @ajd - x.ajd
  end
  raise TypeError, 'expected numeric or date'
end

#<=>(other) ⇒ Object



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

def <=> (other)
  result = nil
  if(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.to_s}<=>#{other.to_s} => #{result}" if $DEBUG
  result
end

#include?(expr) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#marshal_dumpObject

Custom dump which preserves DatePrecision

Author

Jodi Showers



139
140
141
# File 'lib/runt/pdate.rb', line 139

def marshal_dump
  [date_precision, ajd, sg, of]
end

#marshal_load(dumped_obj) ⇒ Object

Custom load which preserves DatePrecision

Author

Jodi Showers



148
149
150
# File 'lib/runt/pdate.rb', line 148

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

#new_self_plus(n) ⇒ Object



87
88
89
90
91
92
# File 'lib/runt/pdate.rb', line 87

def new_self_plus(n)
  if(block_given?)
    n=yield(n)
  end
  return DPrecision::to_p(self.class.new0(@ajd + n, @of, @sg),@date_precision)
end