Class: Simatic::Types::DateAndTime

Inherits:
SimaticType show all
Defined in:
lib/simatic/types/date_and_time.rb

Constant Summary collapse

LENGTH =
8

Class Method Summary collapse

Methods inherited from SimaticType

#initialize, parse, #serialize

Constructor Details

This class inherits a constructor from Simatic::Types::SimaticType

Class Method Details

.parse_one(raw_value) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/simatic/types/date_and_time.rb', line 8

def self.parse_one raw_value
  super
  array = raw_value.unpack('H*').first

  year = array[0,2].to_i + (array[0,2].to_i <= 89 ? 2000 : 1900)
  month = array[2,2].to_i
  day = array[4,2].to_i
  hour = array[6,2].to_i
  min = array[8,2].to_i
  sec = array[10,2].to_i

  microsecond = array[12,3].to_i * 1000

  Time.utc(year, month, day, hour, min, sec, microsecond)
end

.serialize(value) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/simatic/types/date_and_time.rb', line 24

def self.serialize value
  raise "Value must be Time class" unless value.kind_of? Time

  hex_string = 
    '%02d' % (value.year - ((value.year >= 2000) ? 2000 : 1900)) +
    '%02d' % value.month +
    '%02d' % value.day +
    '%02d' % value.hour +
    '%02d' % value.min +
    '%02d' % value.sec +
    '%03d' % (value.usec/1000.0).to_i +
    '%01d' % (value.wday+1)

    # puts hex_string
    
    [hex_string].pack('H*')
end