lib/datte/dattetime.rb in datte-0.1.0 vs lib/datte/dattetime.rb in datte-0.3.0
- old
+ new
@@ -1,25 +1,23 @@
module Datte
class Dattetime
DEFAULT_OPTIONS = {
- force_update: false
+ force_update: false,
+ level: 1
}
- attr_reader :year, :month, :day, :hour, :min, :sec
+ attr_reader :year, :month, :day, :hour, :min
def initialize(options = {})
@options = DEFAULT_OPTIONS.merge(options)
- @date = Date.today
+ @date = DateTime.now
end
- def to_s
- @date.to_s
- end
-
def to_datetime
- DateTime.new(@year, @month, @day, 12, 0, 0) rescue nil
+ return nil unless check_level?
+ DateTime.new(y, m, d, h, mi, 0) rescue nil
end
# 年か月か日を更新
def update_date(md, options = @options)
op = @options[:force_update] ? '=' : '||='
@@ -27,22 +25,33 @@
eval("@month #{op} month!(md)")
eval("@day #{op} day!(md)")
end
# 時か分を更新
- def update_time(hour, min)
+ def update_time(md, options = @options)
+ op = @options[:force_update] ? '=' : '||='
+ eval("@hour #{op} hour!(md)")
+ eval("@min #{op} min!(md)")
end
- # 何年後、何ヶ月後、何日後
- def after_date(year, month, day)
+ # 何年後、何ヶ月後、何日後, 何時間後, 何分後
+ def after(md)
+ @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) # 何時間後
+ @date + Rational(md[:hour].to_i, 24 * 60) # 何分後
end
- # 何時間後、何分後
- def after_time(hour, min)
- end
-
private
+
+ def y; @year || now[:year] end
+ def m; @month || now[:month] end
+ def d; @day || now[:day] end
+ def h; @hour || now[:hour] end
+ def mi; @min || 0 end
+
def now
d = DateTime.now
{ year: d.year, month: d.month, day: d.day, hour: d.hour, min: d.min }
end
@@ -54,9 +63,25 @@
md.matched?(:month) ? md[:month].to_i : now[:month]
end
def day!(md)
md.matched?(:day) ? md[:day].to_i : now[:day]
+ end
+
+ def hour!(md)
+ md.matched?(:hour) ? md[:hour].to_i : now[:hour]
+ end
+
+ def min!(md)
+ md.matched?(:min) ? md[:min].to_i : 0
+ end
+
+ def check_level?
+ counter = 0
+ [@year, @month, @day, @hour, @min].each do |check|
+ counter += 1 unless check.nil?
+ end
+ @options[:level] < counter
end
end
end
class MatchData