Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8306995
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T18:26:27+00:00 2026-06-08T18:26:27+00:00

How do I return an array of days and hours from a range? So

  • 0

How do I return an array of days and hours from a range? So far I have tried:

(48.hours.ago..Time.now.utc).map { |time| { :hour => time.hour } }.uniq

Returns:

[{:hour=>1}, {:hour=>2}, {:hour=>3}, {:hour=>4}, {:hour=>5}, {:hour=>6}, {:hour=>7}, {:hour=>8}, {:hour=>9}, {:hour=>10}, {:hour=>11}, {:hour=>12}, {:hour=>13}, {:hour=>14}, {:hour=>15}, {:hour=>16}, {:hour=>17}, {:hour=>18}, {:hour=>19}, {:hour=>20}, {:hour=>21}, {:hour=>22}, {:hour=>23}, {:hour=>0}] 

Not ideal, since its iterates over every second. Which takes a long time. And I get several warning messages that say:

/Users/Chris/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.2.2/lib/active_support/time_with_zone.rb:328: warning: Time#succ is obsolete; use time + 1

I am trying to return something like:

[{:day => 25, :hour=>1}, {:day => 25, :hour=>2}, {:day => 25, :hour=>3}, {:day => 25, :hour=>4} ... {:day => 26, :hour=>1}, {:day => 26, :hour=>2}, {:day => 26, :hour=>3}, {:day => 26, :hour=>4}] 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T18:26:29+00:00Added an answer on June 8, 2026 at 6:26 pm

    Use Range#step, but as a precaution, convert the dates to integers first (apparently ranges using integers have step() optimized—YMMV). As a matter of style, I also truncate to the hour first.

    Here’s some quick ‘n’ dirty code:

    #!/usr/bin/env ruby
    
    require 'active_support/all'
    
    s=48.hours.ago
    n=Time.now
    
    st=Time.local(s.year, s.month, s.day, s.hour).to_i
    en=Time.local(n.year, n.month, n.day, n.hour).to_i
    
    result = (st..en).step(1.hour).map do |i|
      t = Time.at(i)
      { day: t.day, hour: t.hour }
    end
    
    puts result.inspect
    

    Yields:

    [{:day=>25, :hour=>11}, {:day=>25, :hour=>12}, {:day=>25, :hour=>13}, 
    {:day=>25, :hour=>14}, {:day=>25, :hour=>15}, {:day=>25, :hour=>16},
    ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have one slider to select time range, from 1 (,2,3,..) day (,weeks,months,..) ago
function convertDates($timestamp) { return date('Y-m-d', $timestamp); } $days = array(); //storing databases php time();
I have the following code in config: <?php return array( 'di' => array( 'instance'
$result = mysql_query(SELECT * FROM project ORDER BY projectid); while($row = mysql_fetch_array($result)) { return(array($row['projectid'],
I have a function that return an array (won't work in IE) with two
Is there any way to return an array from a function? More specifically, I've
From the past two days i have been diving into the concepts of OOPS
I have been trying since some days now to find a concrete answer for
I've been trying for two days now to pass an array into a setTimeout
I have following php function, that creates an array of all the days between

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.