Sha256: dbf4e4de3269450f8326ef0b410deacfcbc257c2cb928897428e92761dcb1f40

Contents?: true

Size: 1.09 KB

Versions: 2

Compression:

Stored size: 1.09 KB

Contents

# A couple of monkey-patched utility methods.
#--
# (partially from Rails)
class Date

    # Converts to a Time object in the GMT timezone.
    def to_gm_time
        to_time(new_offset, :gm)
    end

    # Converts to a Time object in the local timezone.
    def to_local_time
        to_time(new_offset(DateTime.now.offset), :local)
    end
    
    def to_date
        ::Date.new(year, month, day)
    end
    
    def lformat(format = :default, locale=nil, options={})
        locale ||= Spider.locale
        Spider::I18n.localize(locale, self, format, options)
    end
    
    def self.lparse(string, format = :default, locale=nil, options={})
        locale ||= Spider.locale
        options[:return] = self <= DateTime ? :datetime : :date 
        Spider::I18n.parse_dt(locale, string, format, options)
    end

    private
    def to_time(dest, method)
        #Convert a fraction of a day to a number of microseconds
        usec = (dest.send(:sec_fraction) * 60 * 60 * 24 * (10**6)).to_i
        Time.send(method, dest.year, dest.month, dest.day, dest.hour, dest.min,
        dest.sec, usec)
    end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
spiderfw-0.5.13 lib/spiderfw/utils/monkey/date_time.rb
spiderfw-0.5.12 lib/spiderfw/utils/monkey/date_time.rb