Module: CodinRep::TimeUtil

Defined in:
lib/codin_rep/time_util.rb

Class Method Summary collapse

Class Method Details

.pack(time = nil) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/codin_rep/time_util.rb', line 24

def pack(time=nil)
  # The current weekday is expected by the REP in the following format: 1
  # for Sunday, 2 for Monday and so on. However there's no direct
  # transformation using Time#strftime for the weekday.  So we use
  # String#next, which adds + 1 to the last character on it, which is
  # exactly what the format requires.
  formated_time = time.strftime('%H %M %S %d %m %y 0%w').next
  codified_time = formated_time.split.collect{|c| c.to_i(16)}.pack('C*')
  codified_time
end

.unpack(time) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/codin_rep/time_util.rb', line 35

def unpack(time)
  hours, minutes, seconds,
  day, month, year,
  weekday = time.unpack('H2H2H2H2H2H2C').collect{|c| c.to_i}
  # Year is in 2-digit format, so add 2000 to it:
  year += 2000
  time = Time.new(year, month, day, hours, minutes, seconds)
  if weekday - 1 != time.wday
    message = "Received weekday is wrong. Expected: #{time.wday + 1}; " +
              "received: #{weekday}"
    raise WrongWeekday.new(message)
  end
  time
end