[Feature idea] Truncating timestamp precision

21 views
Skip to first unread message

powi

unread,
Oct 6, 2014, 8:17:58 AM10/6/14
to rubyonra...@googlegroups.com
Hello.

I thought of adding a method in ActiveSupport that would be able to truncate custom precision amounts from a `DateTime` object.

For example:

    DateTime.now.truncate_precision(sec: 10) # => a new DateTime object minus the specified precision

I've needed this in a feature of mine and thought it might be useful to others.

What do you think? If we find this useful, I'd prepare a PR soon.

Cheers!

jsnark

unread,
Oct 6, 2014, 9:45:21 AM10/6/14
to rubyonra...@googlegroups.com
You can extend the class yourself:

DateTime.class_eval do
  define_method(:truncate_precision) { |amount|
    puts amount
  }
end
=> #<Proc:0x0000001fa649a0@(irb):8 (lambda)>
DateTime.now.truncate_precision(sec: 10)
{:sec=>10}
=> nil

powi

unread,
Oct 6, 2014, 9:47:10 AM10/6/14
to rubyonra...@googlegroups.com
Of course I can. I wanted to know if this seems useful to other people using Rails so I can start to work on this feature.

Robert Walker

unread,
Oct 6, 2014, 10:29:42 AM10/6/14
to rubyonra...@googlegroups.com
Agis A. wrote in post #1159152:
> Of course I can. I wanted to know if this seems useful to other people
> using Rails so I can start to work on this feature.

This doesn't feel to me like the sort of feature that warrants inclusion
in ActiveSupport. There are a multitude of possible functions similar to
this. I don't think it makes sense for ActiveSupport. This might be
useful as an extension module, along with other similar functions,
provided by a separate gem, but I'd have to down vote this for inclusion
in ActiveSupport.

--
Posted via http://www.ruby-forum.com/.
Reply all
Reply to author
Forward
0 new messages