Sha256: e87f5e39fcd772555b7483225375aaae5ae8f52b6cd241bcb89c17e6ddb18749
Contents?: true
Size: 1.24 KB
Versions: 6
Compression:
Stored size: 1.24 KB
Contents
module Dentaku class DateArithmetic def initialize(date) @base = date end def add(duration) case duration when Numeric @base + duration when Dentaku::AST::Duration::Value case duration.unit when :year Time.local(@base.year + duration.value, @base.month, @base.day).to_datetime when :month @base >> duration.value when :day @base + duration.value end else raise Dentaku::ArgumentError.for(:incompatible_type, value: duration, for: Numeric), "'#{duration || duration.class}' is not coercible for date arithmetic" end end def sub(duration) case duration when DateTime, Numeric @base - duration when Dentaku::AST::Duration::Value case duration.unit when :year Time.local(@base.year - duration.value, @base.month, @base.day).to_datetime when :month @base << duration.value when :day @base - duration.value end else raise Dentaku::ArgumentError.for(:incompatible_type, value: duration, for: Numeric), "'#{duration || duration.class}' is not coercible for date arithmetic" end end end end
Version data entries
6 entries across 6 versions & 2 rubygems