Class Runt::PDate
In: lib/runt/pdate.rb
Parent: DateTime

PDate

Date and DateTime with explicit precision.

Based the pattern by Martin Fowler.

Author:Matthew Lipper

Methods

+   -   <=>   civil   day   default   hour   include?   marshal_dump   marshal_load   millisecond   min   month   new_self_plus   sec   to_date   week   year  

Included Modules

DPrecision

External Aliases

civil -> old_civil
civil -> new

Attributes

date_precision  [RW] 

Public Class methods

[Source]

# 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

[Source]

# File lib/runt/pdate.rb, line 121
  def PDate.day( yr,mon,day,*ignored )
    PDate.civil(DAY, yr, mon, day )
  end

[Source]

# File lib/runt/pdate.rb, line 142
  def PDate.default(*args)
    PDate.civil(DEFAULT, *args)
  end

[Source]

# 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

[Source]

# 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

[Source]

# 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

[Source]

# File lib/runt/pdate.rb, line 107
  def PDate.month( yr,mon,*ignored )
    PDate.civil(MONTH, yr, mon, DAY.min_value  )
  end

[Source]

# 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

[Source]

# 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

[Source]

# 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

[Source]

# File lib/runt/pdate.rb, line 103
  def PDate.year(yr,*ignored)
    PDate.civil(YEAR, yr, MONTH.min_value, DAY.min_value  )
  end

Public Instance methods

[Source]

# 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

[Source]

# 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

[Source]

# 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

[Source]

# File lib/runt/pdate.rb, line 41
    def include?(expr)
      eql?(expr)
    end

Custom dump which preserves DatePrecision

Author:Jodi Showers

[Source]

# File lib/runt/pdate.rb, line 151
  def marshal_dump
    [date_precision, ajd, start, offset]
  end

Custom load which preserves DatePrecision

Author:Jodi Showers

[Source]

# File lib/runt/pdate.rb, line 160
  def marshal_load(dumped_obj)
    @date_precision, @ajd, @sg, @of=dumped_obj
  end

[Source]

# File lib/runt/pdate.rb, line 89
  def new_self_plus(n)
    if(block_given?)
      n=yield(n)
    end
    return DPrecision::to_p(self.class.new!(@ajd + n, @of, @sg),@date_precision)
  end

[Validate]