class Datte::Dattetime

Constants

DEFAULT_OPTIONS

Attributes

day[R]
hour[R]
min[R]
month[R]
year[R]

Public Class Methods

new(options = {}) click to toggle source
# File lib/datte/dattetime.rb, line 11
def initialize(options = {})
  @options = DEFAULT_OPTIONS.merge(options)
  @date = DateTime.now
end

Public Instance Methods

after(md) click to toggle source

何年後、何ヶ月後、何日後, 何時間後, 何分後

# File lib/datte/dattetime.rb, line 37
def after(md)
  if md.matched?(:year)
    @year, @month, @day = now[:year] + md[:year].to_i, now[:month], now[:day]
  end
  if md.matched?(:month)
    carry = (now[:month] + md[:month].to_i) / 12
    @year, @month, @day = now[:year] + carry, now[:month] + md[:month].to_i , now[:day]
  end
  if md.matched?(:day)
    days = [31,28,31,30,31,30,31,31,30,31,30,31][now[:month] - 1]
    carry = (now[:day] + md[:day].to_i) / days
    @year, @month, @day = now[:year], now[:month] + carry, now[:day] + md[:day].to_i - carry * days
  end
  if md.matched?(:hour)
    carry = (now[:hour] + md[:hour].to_i) / 24
    @day, @hour, @min = now[:day] + carry, now[:hour] + md[:hour].to_i - carry * 24, now[:min]
  end
  if md.matched?(:min)
    carry = (now[:min] + md[:min].to_i) / 60
    @day, @hour, @min = now[:day], now[:hour] + carry, now[:min] + md[:min].to_i - 60 * carry
  end
  # @date >> (md[:year].to_i * 12) if md.matched?(:year) # 何年後
  # @date >> md[:month].to_i if md.matched?(:month) # 何ヶ月後
  # @date + md[:day].to_i if md.matched?(:day) # 何日後
  # @date + Rational(md[:hour].to_i, 24) if md.matched?(:hour) # 何時間後
  # @date + Rational(md[:hour].to_i, 24 * 60) if md.matched?(:hour) # 何分後
end
to_datetime() click to toggle source
# File lib/datte/dattetime.rb, line 16
def to_datetime
  return nil unless check_level?
  DateTime.new(y, m, d, h, mi, 0) rescue nil
end
update_date(md, options = @options) click to toggle source

年か月か日を更新

# File lib/datte/dattetime.rb, line 22
def update_date(md, options = @options)
  op = @options[:force_update] ? '=' : '||='
  eval("@year #{op} year!(md)")
  eval("@month #{op} month!(md)")
  eval("@day #{op} day!(md)")
end
update_time(md, options = @options) click to toggle source

時か分を更新

# File lib/datte/dattetime.rb, line 30
def update_time(md, options = @options)
  op = @options[:force_update] ? '=' : '||='
  eval("@hour #{op} hour!(md)")
  eval("@min #{op} min!(md)")
end

Private Instance Methods

check_level?() click to toggle source
# File lib/datte/dattetime.rb, line 98
def check_level?
  counter = 0
  [@year, @month, @day, @hour, @min].each do |check|
    counter += 1 unless check.nil?
  end
  @options[:level] < counter
end
d() click to toggle source
# File lib/datte/dattetime.rb, line 69
def d; @day || now[:day] end
day!(md) click to toggle source
# File lib/datte/dattetime.rb, line 86
def day!(md)
  md.matched?(:day) ? md[:day].to_i : now[:day]
end
h() click to toggle source
# File lib/datte/dattetime.rb, line 70
def h; @hour || now[:hour] end
hour!(md) click to toggle source
# File lib/datte/dattetime.rb, line 90
def hour!(md)
  md.matched?(:hour) ? md[:hour].to_i : now[:hour]
end
m() click to toggle source
# File lib/datte/dattetime.rb, line 68
def m; @month || now[:month] end
mi() click to toggle source
# File lib/datte/dattetime.rb, line 71
def mi; @min || 0 end
min!(md) click to toggle source
# File lib/datte/dattetime.rb, line 94
def min!(md)
  md.matched?(:min) ? md[:min].to_i : 0
end
month!(md) click to toggle source
# File lib/datte/dattetime.rb, line 82
def month!(md)
  md.matched?(:month) ? md[:month].to_i : now[:month]
end
now() click to toggle source
# File lib/datte/dattetime.rb, line 73
def now
  @d ||= DateTime.now
  { year: @d.year, month: @d.month, day: @d.day, hour: @d.hour, min: @d.min }
end
y() click to toggle source
# File lib/datte/dattetime.rb, line 67
def y; @year || now[:year] end
year!(md) click to toggle source
# File lib/datte/dattetime.rb, line 78
def year!(md)
  md.matched?(:year) ? md[:year].to_i : now[:year]
end