Sha256: 0d815ee006f08ad87285ac2a2d2cb4da3fa39b3101f33e7558d7ab73da1d98d1

Contents?: true

Size: 1.04 KB

Versions: 9

Compression:

Stored size: 1.04 KB

Contents

# =XMPP4R - XMPP Library for Ruby
# License:: Ruby's license (see the LICENSE file) or GNU GPL, at your option.
# Website::http://home.gna.org/xmpp4r/

require 'time'

module Jabber::EntityTime
  NS_TIME = 'urn:xmpp:time'

  class IqTime < Jabber::IqQuery
    name_xmlns 'time', NS_TIME

    def initialize(time=nil)
      super()
      set_time(time)
    end

    def set_time(time=nil)
      time ||= Time.now

      tzo = self.add_element('tzo')
      tzo.add_text(time_zone_offset(time))

      utc = self.add_element('utc')
      utc.add_text(utc_time(time))
    end

    private
    def utc_time(time)
      raise ArgumentError, 'invalid time object' unless time.respond_to?(:utc)
      time.utc.strftime('%Y-%m-%dT%H:%M:%SZ')
    end

    def time_zone_offset(time)
      raise ArgumentError, 'invalid time object' unless time.respond_to?(:utc_offset)

      sec_offset = time.utc_offset
      h_offset = sec_offset.to_i / 3600
      m_offset = sec_offset.abs % 60

      "%+03d:%02d"%[h_offset, m_offset]
    end

  end # /IqTime
end # /Jabber::EntityTime

Version data entries

9 entries across 9 versions & 3 rubygems

Version Path
xmpp4r-0.5.5 lib/xmpp4r/entity_time/iq.rb
gmcmillan-xmpp4r-0.6.2 lib/xmpp4r/entity_time/iq.rb
gmcmillan-xmpp4r-0.6.1 lib/xmpp4r/entity_time/iq.rb
gmcmillan-xmpp4r-0.6 lib/xmpp4r/entity_time/iq.rb
gmcmillan-xmpp4r-0.5 lib/xmpp4r/entity_time/iq.rb
mad-p-xmpp4r-0.6.3 lib/xmpp4r/entity_time/iq.rb
mad-p-xmpp4r-0.6.2 lib/xmpp4r/entity_time/iq.rb
mad-p-xmpp4r-0.6.1 lib/xmpp4r/entity_time/iq.rb
mad-p-xmpp4r-0.6.0 lib/xmpp4r/entity_time/iq.rb