Class: MkTime::Argument

Inherits:
Object
  • Object
show all
Defined in:
lib/mk_time/argument.rb

Instance Method Summary collapse

Constructor Details

#initialize(arg) ⇒ Argument

Returns a new instance of Argument.



3
4
5
# File 'lib/mk_time/argument.rb', line 3

def initialize(arg)
  @arg = arg
end

Instance Method Details

#get_utcObject

引数取得

  • コマンドライン引数を取得して日時の妥当性チェックを行う

  • コマンドライン引数無指定なら、現在日時とする。

@return: utc (Time Object)



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/mk_time/argument.rb', line 15

def get_utc
  (puts Const::MSG_ERR_1; return) unless @arg =~ /^\d{8}$|^\d{14,}$/
  year, month, day = @arg[ 0, 4].to_i, @arg[ 4, 2].to_i, @arg[ 6, 2].to_i
  hour, min,   sec = @arg[ 8, 2].to_i, @arg[10, 2].to_i, @arg[12, 2].to_i
  usec = @arg.split(//).size > 14 ? @arg[14..-1].to_s : "0"
  (puts Const::MSG_ERR_2; return) unless Date.valid_date?(year, month, day)
  (puts Const::MSG_ERR_2; return) if hour > 23 || min > 59 || sec >= 60.0
  d = usec.to_s.split(//).size
  return Time.new(
    year, month, day, hour, min, sec + Rational(usec.to_i, 10 ** d), "+00:00"
  )
end