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 228937
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T19:43:26+00:00 2026-05-11T19:43:26+00:00

I am trying to come up with an efficient method for truncating Ruby Time

  • 0

I am trying to come up with an efficient method for truncating Ruby Time objects according to a given resolution.

class Time
  def truncate resolution
    t = to_a
    case resolution
    when :min   
      t[0] = 0
    when :hour
      t[0] = t[1] = 0
    when :day
      t[0] = t[1] = 0
      t[2] = 1
    when :month 
      t[0] = t[1] = 0
      t[2] = t[3] = 1
    when :week  
      t[0] = t[1] = 0
      t[2] = 1
      t[3] -= t[6] - 1
    when :year
      t[0] = t[1] = 0
      t[2] = t[3] = t[4] = 1
    end

    Time.local *t
  end
end

Does anyone know a faster version that achieves the same task?

  • 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-05-11T19:43:27+00:00Added an answer on May 11, 2026 at 7:43 pm

    Thanks to both of you. Since I don’t use Rails I copied the code to perform a performance benchmark.

    require 'benchmark'
    
    class Time
      def truncate resolution
        t = to_a
        case resolution
        when :min   
          t[0] = 0
        when :hour
          t[0] = t[1] = 0
        when :day
          t[0] = t[1] = t[2] = 0
        when :week  
          t[0] = t[1] = t[2] = 0
          t[3] -= t[6] - 1
        when :month 
          t[0] = t[1] = t[2] = 0
          t[3] = 1
        when :year
          t[0] = t[1] = t[2] = 0
          t[3] = t[4] = 1
        end
    
        Time.local *t 
      end
    
      def truncate2 resolution
        opts = {}
    
        case resolution
        when :min   
          opts[:sec] = 0
        when :hour
          opts[:sec] = opts[:min] = 0
        when :day
          opts[:sec] = opts[:min] = opts[:hour] = 0
        when :week  
          opts[:sec] = opts[:min] = opts[:hour] = 0
          opts[:day] = wday - 1 if wday != 1
        when :month 
          opts[:sec] = opts[:min] = opts[:hour] = 0
          opts[:day] = 1
        when :year
          opts[:sec] = opts[:min] = opts[:hour] = 0
          opts[:day] = opts[:month] = 1
        end
    
        change opts 
      end
    
      def truncate3 resolution
        t = to_a
        if resolution == :week
          t[0] = t[1] = 0
          t[2] = 1
          t[3] -= t[6] - 1
        else
          len = [:sec, :min, :hour, :day, :month, :year].index(resolution)
          t.fill(0, 0, len)
          t.fill(1, 3, len-3)
        end
    
        Time.local *t
      end    
    
      def change opts 
        Time.local(
          opts[:year]  || year,
          opts[:month] || month,
          opts[:day]   || day,
          opts[:hour]  || hour,
          opts[:min]   || (opts[:hour] ? 0 : min),
          opts[:sec]   || ((opts[:hour] || opts[:min]) ? 0 : sec),
        )
      end
    end
    
    Resolutions = [:sec, :min, :hour, :day, :week, :month, :year]
    
    # Correctness check.
    puts Resolutions.map { |r| "#{r}: #{Time.now.truncate r}" } << "\n"
    puts Resolutions.map { |r| "#{r}: #{Time.now.truncate2 r}" } << "\n"
    puts Resolutions.map { |r| "#{r}: #{Time.now.truncate3 r}" } << "\n"
    
    n = 100000
    now = Time.now
    
    Benchmark.bm(10) do |x|
      x.report("truncate") { n.times { Resolutions.each { |r| now.truncate  r } } }
      x.report("truncate2") { n.times { Resolutions.each { |r| now.truncate2 r } } }
      x.report("truncate3") { n.times { Resolutions.each { |r| now.truncate3 r } } }
    end
    
    Benchmark.bm(10) do |x|
      Resolutions.each do |unit| 
        x.report("#{unit}") { n.times { now.truncate unit } }
        x.report("#{unit}2") { n.times { now.truncate2 unit } }
        x.report("#{unit}3") { n.times { now.truncate3 unit } }
      end
    end
    

    Here are the results:

    sec: 2009-05-26 13:44:20 -0700
    min: 2009-05-26 13:44:00 -0700
    hour: 2009-05-26 13:00:00 -0700
    day: 2009-05-26 00:00:00 -0700
    week: 2009-05-25 00:00:00 -0700
    month: 2009-05-01 00:00:00 -0700
    year: 2008-12-31 23:00:00 -0800
    
    sec: 2009-05-26 13:44:20 -0700
    min: 2009-05-26 13:44:00 -0700
    hour: 2009-05-26 13:00:00 -0700
    day: 2009-05-26 00:00:00 -0700
    week: 2009-05-01 00:00:00 -0700
    month: 2009-05-01 00:00:00 -0700
    year: 2009-01-01 00:00:00 -0800
    
    sec: 2009-05-26 13:44:20 -0700
    min: 2009-05-26 13:44:00 -0700
    hour: 2009-05-26 13:00:00 -0700
    day: 2009-05-26 00:00:00 -0700
    week: 2009-05-25 01:00:00 -0700
    month: 2009-05-01 00:00:00 -0700
    year: 2008-12-31 23:00:00 -0800
    
                    user     system      total        real
    truncate    5.910000   0.020000   5.930000 (  5.947453)
    truncate2   6.180000   0.020000   6.200000 (  6.232918)
    truncate3   6.150000   0.020000   6.170000 (  6.253931)
                    user     system      total        real
    sec         0.720000   0.000000   0.720000 (  0.749040)
    sec2        0.830000   0.010000   0.840000 (  0.863029)
    sec3        0.800000   0.000000   0.800000 (  0.820477)
    min         0.700000   0.000000   0.700000 (  0.709238)
    min2        0.860000   0.010000   0.870000 (  0.860168)
    min3        0.770000   0.000000   0.770000 (  0.795734)
    hour        0.680000   0.000000   0.680000 (  0.705306)
    hour2       0.850000   0.010000   0.860000 (  0.867235)
    hour3       0.740000   0.000000   0.740000 (  0.746338)
    day         0.720000   0.000000   0.720000 (  0.724324)
    day2        0.890000   0.010000   0.900000 (  0.894312)
    day3        0.780000   0.000000   0.780000 (  0.788007)
    week        0.730000   0.000000   0.730000 (  0.736604)
    week2       0.910000   0.000000   0.910000 (  0.910925)
    week3       0.600000   0.000000   0.600000 (  0.611683)
    month       0.720000   0.000000   0.720000 (  0.719515)
    month2      0.880000   0.010000   0.890000 (  0.888045)
    month3      0.780000   0.000000   0.780000 (  0.789726)
    year        1.540000   0.010000   1.550000 (  1.565335)
    year2       0.830000   0.000000   0.830000 (  0.849737)
    year3       1.600000   0.010000   1.610000 (  1.644958)
    

    Seems like my first truncation version is still most efficient, except for the year case. There is a small quirk in the year for truncate and truncate3 though. It is displayed as

    2008-12-31 23:00:00 -0800
    

    rather than

    2009-01-01 00:00:00 -0700
    

    Any ideas why?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 122k
  • Answers 122k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer There are tons of dupes for this, I'll let somebody… May 12, 2026 at 12:42 am
  • Editorial Team
    Editorial Team added an answer you could create an application that starts and then installs… May 12, 2026 at 12:42 am
  • Editorial Team
    Editorial Team added an answer Divide the unsigned 32-bit hexadecimal number range up as a… May 12, 2026 at 12:42 am

Related Questions

This originally was a problem I ran into at work, but is now something
I am trying to come up with the best data structure for use in
I am working on an open source Django time tracking app, Djime , and
I'm using C#, .NET 3.5. I understand how to utilize events, how to declare

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.