Sha256: eb0cf97aaccf98060b888d986376c8b4ff230d0ba200e97588e9362cd9fc8f25

Contents?: true

Size: 1.13 KB

Versions: 1

Compression:

Stored size: 1.13 KB

Contents

require 'chronic'

module TimeScopes
  class << self
    def hourly(start=nil)
      start ||= Time.now
      [ Chronic.parse(start.strftime("%Y-%m-%d #{start.hour}:00:00Z")),
        Chronic.parse(start.strftime("%Y-%m-%d #{start.hour}:59:59Z")) ]
    end

    def daily(start=nil)
      start ||= Time.now
      [ Chronic.parse(start_of_day(start.utc)),
        Chronic.parse(start_of_day(1.day.since(start.utc))) ]
    end

    def weekly(start=nil)
      start ||= Time.now
      start = start.utc
      start = start - 1.day while !start.monday?
      [ Chronic.parse(start_of_day(start.utc)),
        Chronic.parse(start_of_day(1.week.since(start.utc))) ]
    end

    def monthly(start=nil)
      start ||= Time.now
      [ Chronic.parse(start_of_month(start.utc)),
        Chronic.parse(start_of_month(1.month.since(start.utc))) ]
    end

    def overall(start=nil)
      [ Chronic.parse('2000-01-01 00:00:00'),
        Chronic.parse(start_of_day(1.day.from_now.utc)) ]
    end

    def start_of_day(time)
      time.strftime("%Y-%m-%d 00:00:00Z")
    end

    def start_of_month(time)
      time.strftime("%Y-%m-01 00:00:00Z")
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
timescopes-0.1.2 lib/timescopes/scopes.rb