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::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.



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



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

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

.default(*args) ⇒ Object



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

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

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



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

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



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

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



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

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



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

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



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

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



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

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

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



111
112
113
114
115
116
117
118
119
# File 'lib/runt/pdate.rb', line 111

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



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

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
65
66
# 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 WEEK then
	return new_self_plus(n*7)
    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)


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

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



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

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



151
152
153
# File 'lib/runt/pdate.rb', line 151

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

#marshal_load(dumped_obj) ⇒ Object

Custom load which preserves DatePrecision

Author

Jodi Showers



160
161
162
# File 'lib/runt/pdate.rb', line 160

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

#new_self_plus(n) ⇒ Object



89
90
91
92
93
94
# File 'lib/runt/pdate.rb', line 89

def new_self_plus(n)
  if(block_given?)
    n=yield(n)
  end
  return DPrecision::to_p(Time.at(to_time.to_i + (n.to_f * 86400)),date_precision)
end