Sha256: 2d9f4502c590219eec9d56fa49a0fbff9617250933e8da5cb80d5444b1ad8480

Contents?: true

Size: 1023 Bytes

Versions: 5

Compression:

Stored size: 1023 Bytes

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

5 entries across 5 versions & 1 rubygems

Version Path
ntail-0.0.11 lib/ntail/time_local.rb
ntail-0.0.10 lib/ntail/time_local.rb
ntail-0.0.9 lib/ntail/time_local.rb
ntail-0.0.8 lib/ntail/time_local.rb
ntail-0.0.7 lib/ntail/time_local.rb