From: Robert Klemme Date: 2009-12-29T00:14:29+09:00 Subject: Re: unpack :( 2009/12/28 Alfonso Caponi : > Alfonso Caponi wrote: >>>>> systemdate.unpack("nccccc") >>> => [2009, 12, 28, 11, 42, 40] >>> >>> I'm not sure how you want the last 4 bytes interpreted, but a bit of >>> googling for SNMP systemDate suggests the following: >>> >>>>> systemdate.unpack("nccccccAcc") >>> => [2009, 12, 28, 11, 42, 40, 0, "+", 1, 0] >>> >>> i.e. 11:42:40.0 (tenths of a second) with a timezone of +01:00 >> >> Great! It's very good for my script! Thank you very much! :) > > dtm = systemDate.unpack("ncccccc") > dtm =~ /(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/ > systemDate = "#{dtm[0]}-#{dtm[1]}-#{dtm[2]} > #{dtm[3]}:#{dtm[4]}:#{dtm[5]}" > > Is it obscene? :) Your code is a bit weird. First you unpack which returns an Array. Then you try to match that Array against a regular expression without doing anything with the result (as far as I see it will always return nil anyway). Finally you do array accesses on the Array returned by #unpack. Why do you have the regexp match attempt in there? Note that there are tools in Ruby to actually deal with date and time. So in your case you could do something like this (including giving values proper names): require 'date' year, month, day, hour, minute, second, fract, = *system_date.unpack "ncccccc" date = DateTime.new year, month, day, hour, minute, second + fract / 10.0 Now you can do all the date math with "date" that you need or just print it. Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/