Add timestamp class to convert from PDU format to DateTime

develop
Guillaume DOTT 2013-09-10 15:01:17 +02:00
parent f049777a00
commit f4218e1025
4 changed files with 39 additions and 5 deletions

View File

@ -4,6 +4,7 @@ require 'biju/pdu/encoding/ucs2'
require 'biju/pdu/user_data' require 'biju/pdu/user_data'
require 'biju/pdu/data_coding_scheme' require 'biju/pdu/data_coding_scheme'
require 'biju/pdu/timestamp'
require 'biju/pdu/phone_number' require 'biju/pdu/phone_number'
require 'biju/pdu/type_of_address' require 'biju/pdu/type_of_address'
@ -38,9 +39,7 @@ module Biju
protocol_identifier: string[34..35], protocol_identifier: string[34..35],
data_coding_scheme: string[36..37], data_coding_scheme: string[36..37],
timestamp: DateTime.strptime( timestamp: Timestamp.new(string[38, 14]).to_datetime,
"#{string[38..49].reverse}+#{string[50..51].reverse}",
'%S%M%H%d%m%y%Z'),
user_data_length: string[52..53], user_data_length: string[52..53],
} }
res[:sender_number] = PhoneNumber.new( res[:sender_number] = PhoneNumber.new(

View File

@ -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

View File

@ -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

View File

@ -6,7 +6,7 @@ describe Biju::Sms do
Biju::Sms.new( Biju::Sms.new(
id: 1, id: 1,
phone_number: "144", 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") message: "Some text here")
end end
@ -21,7 +21,7 @@ describe Biju::Sms do
'07913396050066F3040B913366666666F600003190509095928004D4F29C0E') '07913396050066F3040B913366666666F600003190509095928004D4F29C0E')
end 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(:message) { should eq('Test') }
its(:phone_number) { should eq('33666666666') } its(:phone_number) { should eq('33666666666') }
its(:type_of_address) { should eq(:international) } its(:type_of_address) { should eq(:international) }