Sha256: 2c8b8ed1325af957b7690d17bc0a48ca927d53647171e132beb00d13ed1c17c5

Contents?: true

Size: 1.01 KB

Versions: 13

Compression:

Stored size: 1.01 KB

Contents

require 'date'

module NginxTail
  module TimeLocal
    
    def self.included(base) # :nodoc:
      base.class_eval do

        # >> DateTime.strptime("13/Apr/2010:04:45:51 +0100", '%d/%b/%Y:%T %z').to_s
        # => "2010-04-13T04:45:51+01:00"
        # >> DateTime.strptime("13/Apr/2010:04:45:51 +0100", '%d/%b/%Y:%H:%M:%S %z').to_s
        # => "2010-04-13T04:45:51+01:00"
        # >> _

        def self.to_date(time_local)
          DateTime.strptime(time_local, '%d/%b/%Y:%T %z')
        end
        
        def self.to_date_s(time_local, format = "%Y-%m-%d %X")
          self.to_date(time_local).strftime(format)
        end

        # this ensures the below module methods actually make sense...
        raise "Class #{base.name} should implement instance method 'time_local'" unless base.instance_methods.map(&:to_s).include? 'time_local'
        
      end
    end
    
    def to_date
      self.class.to_date(self.time_local)
    end

    def to_date_s
      self.class.to_date_s(self.time_local)
    end
    
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
ntail-1.3.1 lib/ntail/time_local.rb
ntail-1.3.0 lib/ntail/time_local.rb
ntail-1.2.0 lib/ntail/time_local.rb
ntail-1.1.4 lib/ntail/time_local.rb
ntail-1.1.3 lib/ntail/time_local.rb
ntail-1.1.2 lib/ntail/time_local.rb
ntail-1.1.1 lib/ntail/time_local.rb
ntail-1.1.0 lib/ntail/time_local.rb
ntail-1.0.2 lib/ntail/time_local.rb
ntail-1.0.0 lib/ntail/time_local.rb
ntail-0.2.0 lib/ntail/time_local.rb
ntail-0.1.0 lib/ntail/time_local.rb
ntail-0.0.12 lib/ntail/time_local.rb