Add timestamp class to convert from PDU format to DateTime
This commit is contained in:
		
							parent
							
								
									f049777a00
								
							
						
					
					
						commit
						f4218e1025
					
				| @ -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( | ||||
|  | ||||
							
								
								
									
										26
									
								
								lib/biju/pdu/timestamp.rb
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										26
									
								
								lib/biju/pdu/timestamp.rb
									
									
									
									
									
										Normal 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 | ||||
							
								
								
									
										9
									
								
								spec/biju/pdu/timestamp_spec.rb
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								spec/biju/pdu/timestamp_spec.rb
									
									
									
									
									
										Normal 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 | ||||
| @ -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) } | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 Guillaume DOTT
						Guillaume DOTT