From f4218e102522ccf25f7731fb247f830172707a82 Mon Sep 17 00:00:00 2001 From: Guillaume DOTT Date: Tue, 10 Sep 2013 15:01:17 +0200 Subject: [PATCH] Add timestamp class to convert from PDU format to DateTime --- lib/biju/pdu.rb | 5 ++--- lib/biju/pdu/timestamp.rb | 26 ++++++++++++++++++++++++++ spec/biju/pdu/timestamp_spec.rb | 9 +++++++++ spec/biju/sms_spec.rb | 4 ++-- 4 files changed, 39 insertions(+), 5 deletions(-) create mode 100644 lib/biju/pdu/timestamp.rb create mode 100644 spec/biju/pdu/timestamp_spec.rb diff --git a/lib/biju/pdu.rb b/lib/biju/pdu.rb index 5f86e77..afbf491 100644 --- a/lib/biju/pdu.rb +++ b/lib/biju/pdu.rb @@ -4,6 +4,7 @@ require 'biju/pdu/encoding/ucs2' require 'biju/pdu/user_data' require 'biju/pdu/data_coding_scheme' +require 'biju/pdu/timestamp' require 'biju/pdu/phone_number' require 'biju/pdu/type_of_address' @@ -38,9 +39,7 @@ module Biju protocol_identifier: string[34..35], data_coding_scheme: string[36..37], - timestamp: DateTime.strptime( - "#{string[38..49].reverse}+#{string[50..51].reverse}", - '%S%M%H%d%m%y%Z'), + timestamp: Timestamp.new(string[38, 14]).to_datetime, user_data_length: string[52..53], } res[:sender_number] = PhoneNumber.new( diff --git a/lib/biju/pdu/timestamp.rb b/lib/biju/pdu/timestamp.rb new file mode 100644 index 0000000..19b5b20 --- /dev/null +++ b/lib/biju/pdu/timestamp.rb @@ -0,0 +1,26 @@ +module Biju + module PDU + class Timestamp + attr_reader :timestamp + + def initialize(timestamp) + @timestamp = timestamp + end + + def timezone + timezone = timestamp[-2, 2].reverse.hex + + sign = (timezone >> 7 == 0 ? '+' : '-') + tens_digit = ((timezone & 0b01110000) >> 4) + units_digit = (timezone & 0b00001111) + + sign << '%02d' % ((tens_digit * 10 + units_digit) / 4) + end + + def to_datetime + DateTime.strptime( + "#{timestamp[0..-3].reverse}#{timezone}", '%S%M%H%d%m%y%Z') + end + end + end +end diff --git a/spec/biju/pdu/timestamp_spec.rb b/spec/biju/pdu/timestamp_spec.rb new file mode 100644 index 0000000..78e352e --- /dev/null +++ b/spec/biju/pdu/timestamp_spec.rb @@ -0,0 +1,9 @@ +require 'spec_helper' +require 'biju/pdu' + +describe Biju::PDU::Timestamp do + subject { Biju::PDU::Timestamp.new('31900141039580') } + + its(:timezone) { should eq('+02') } + its(:to_datetime) { should eq(DateTime.new(2013, 9, 10, 14, 30, 59, '+02')) } +end diff --git a/spec/biju/sms_spec.rb b/spec/biju/sms_spec.rb index a2f262e..512cba9 100644 --- a/spec/biju/sms_spec.rb +++ b/spec/biju/sms_spec.rb @@ -6,7 +6,7 @@ describe Biju::Sms do Biju::Sms.new( id: 1, phone_number: "144", - datetime: "11/07/28,15:34:08-12", + datetime: DateTime.new(2011, 7, 28, 15, 34, 8, '-12'), message: "Some text here") end @@ -21,7 +21,7 @@ describe Biju::Sms do '07913396050066F3040B913366666666F600003190509095928004D4F29C0E') end - its(:datetime) { should eq(DateTime.new(2013, 9, 5, 9, 59, 29, '+08')) } + its(:datetime) { should eq(DateTime.new(2013, 9, 5, 9, 59, 29, '+02')) } its(:message) { should eq('Test') } its(:phone_number) { should eq('33666666666') } its(:type_of_address) { should eq(:international) }