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.



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

def date_precision
  @date_precision
end

Class Method Details

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



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

def civil(*args)
  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



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

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

.default(*args) ⇒ Object



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

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

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



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

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



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

def PDate.millisecond( 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



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

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



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

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

.old_civilObject



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

alias_method :old_civil, :civil

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



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

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



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

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



93
94
95
# File 'lib/runt/pdate.rb', line 93

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

Instance Method Details

#+(n) ⇒ Object

Raises:

  • (TypeError)


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/runt/pdate.rb', line 39

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) ) }
  end
end

#-(x) ⇒ Object

Raises:

  • (TypeError)


58
59
60
61
62
63
64
65
66
# File 'lib/runt/pdate.rb', line 58

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



68
69
70
71
72
73
74
75
76
77
# File 'lib/runt/pdate.rb', line 68

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

#new_self_plus(n) ⇒ Object



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

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