Project

General

Profile

Feature #12173

`Time#till_now`

Added by sawa (Tsuyoshi Sawada) about 1 year ago. Updated about 1 year ago.

Status:
Open
Priority:
Normal
Assignee:
-
Target version:
-
[ruby-core:74300]

Description

It is very frequent to have a time instance:

t = Time.now

and then after some operations, do:

Time.now - t

I propose Time#till_now, which is equivalent to:

class Time
  def till_now; self.class.now - self end
end

and similar methods can perhaps be defined on Date and DateTime classes as well. Another candidate for the method name is until_now.

Then we can do:

t = Time.now
# some heavy operation
puts "It took #{t.till_now} secs."

Related issues

Related to CommonRuby - Feature #8640: Add Time#elapsed to return nanoseconds since creation Open 07/16/2013
Related to CommonRuby - Feature #8096: introduce Time.current_timestamp Feedback 03/15/2013

History

#1 [ruby-core:74304] Updated by sawa (Tsuyoshi Sawada) about 1 year ago

This can also be used to wait until a preset time:

target_time = Time.new(2017, 1, 1, 0, 0, 0)
sleep(-target_time.till_now)

Or perhaps, the opposite (multiplied by -1) might be more intuitive:

class Time
  def from_now; self - self.class.now end
end

so that we can do:

target_time = Time.new(2017, 1, 1, 0, 0, 0)
sleep(target_time.from_now)

or

t = Time.now
# some heavy operation
puts "It took #{-t.from_now} secs."

#2 [ruby-core:74305] Updated by phluid61 (Matthew Kerwin) about 1 year ago

I would use both of these if they existed, and have defined my own equivalent versions in past projects.

Please note that "till_now" is not correct spelling; it should be "until_now" or "til_now"

#3 [ruby-core:74306] Updated by phluid61 (Matthew Kerwin) about 1 year ago

This is related to (but different from) #8640, #8096

#4 Updated by kosaki (Motohiro KOSAKI) about 1 year ago

  • Related to Feature #8640: Add Time#elapsed to return nanoseconds since creation added

#5 Updated by kosaki (Motohiro KOSAKI) about 1 year ago

  • Related to Feature #8096: introduce Time.current_timestamp added

#6 [ruby-core:74308] Updated by sawa (Tsuyoshi Sawada) about 1 year ago

Matthew Kerwin wrote:

Please note that "till_now" is not correct spelling; it should be "until_now" or "til_now"

till and until are the correct spelling. Perhaps you are confusing it with 'til (which is pretty much informal).

#7 [ruby-core:74311] Updated by phluid61 (Matthew Kerwin) about 1 year ago

Tsuyoshi Sawada wrote:

Matthew Kerwin wrote:

Please note that "till_now" is not correct spelling; it should be "until_now" or "til_now"

till and until are the correct spelling. Perhaps you are confusing it with 'til (which is pretty much informal).

till means cash register, or to plough soil. 'til is an abbreviation of until, which isn't allowed in Ruby syntax so I dropped the apostrophe.

#8 [ruby-core:74314] Updated by sawa (Tsuyoshi Sawada) about 1 year ago

Matthew Kerwin wrote:

till means cash register, or to plough soil.

That is a homonym (as well as a homograph) of the word in question.

#9 [ruby-core:74315] Updated by phluid61 (Matthew Kerwin) about 1 year ago

Tsuyoshi Sawada wrote:

Matthew Kerwin wrote:

till means cash register, or to plough soil.

That is a homonym (as well as a homograph) of the word in question.

I've just done some research and you're right, I retract my opposition.

#10 [ruby-core:74316] Updated by kosaki (Motohiro KOSAKI) about 1 year ago

2016-03-14 21:06 GMT-04:00 matthew@kerwin.net.au:

Issue #12173 has been updated by Matthew Kerwin.

Tsuyoshi Sawada wrote:

Matthew Kerwin wrote:

till means cash register, or to plough soil.

That is a homonym (as well as a homograph) of the word in question.

I've just done some research and you're right, I retract my opposition.

dictionary.com includes this usage:

http://www.dictionary.com/browse/till?s=t

but freedictionary.org doesn't.

http://freedictionary.org/?Query=till&button=Search

So, I'm afraid Matthew is not alone and I'd suggest avoiding 'till'.

#11 [ruby-core:74325] Updated by avit (Andrew Vit) about 1 year ago

Tsuyoshi Sawada wrote:

That is a homonym (as well as a homograph) of the word in question.

I also think "till" is the traditional spelling ("till the cows come home") and I've seen
the apostrophe 'til used more frequently (USA/Canada) in modern writing but I still
prefer "until" anyway.

For reference, ActiveSupport also defines similar methods for numeric classes:
http://api.rubyonrails.org/classes/ActiveSupport/Duration.html#method-i-until
(These are the inverse receiver/types of the proposed method.)

#12 [ruby-core:74335] Updated by Eregon (Benoit Daloze) about 1 year ago

Tsuyoshi Sawada wrote:

It is very frequent to have a time instance:

t = Time.now

and then after some operations, do:

Time.now - t

This seems fairly related to #8640.
Just a note: the Time.now - t is typically not what you want if you want to benchmark some piece of code.
A monotonic clock like Process.clock_gettime(Process::CLOCK_MONOTONIC) should be used instead (I know, it's a bit lengthy).
Or well if you can require 'benchmark' then it's just Benchmark.realtime { ... }.

#13 [ruby-core:74358] Updated by elifoster (Eli Clemente Gordillo Foster) about 1 year ago

I think it would be best for these from_now and until_now methods to return a Time object rather than a Float.

#14 [ruby-core:74378] Updated by shevegen (Robert A. Heiler) about 1 year ago

I think it would be best for these from_now and until_now methods to return
a Time object rather than a Float.

I don't know the current way - is it custom for Ruby to return a time object
for time-related methods? I mean it would not be a huge issue, one could call
.seconds to get the seconds.

Anyway +1 on the idea of the initial suggestion above. I have no particular
preferences on the name itself, till_now, until_now but we should all settle
for something in the end.

Also available in: Atom PDF