Project

General

Profile

Actions

Feature #12173

open

`Time#till_now`

Added by sawa (Tsuyoshi Sawada) about 8 years ago. Updated about 8 years ago.

Status:
Open
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 2 (1 open1 closed)

Related to Ruby master - Feature #8640: Add Time#elapsed to return nanoseconds since creationOpenActions
Related to Ruby master - Feature #8096: introduce Time.current_timestampFeedbackmatz (Yukihiro Matsumoto)Actions

Updated by sawa (Tsuyoshi Sawada) about 8 years 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."

Updated by phluid61 (Matthew Kerwin) about 8 years 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"

Updated by phluid61 (Matthew Kerwin) about 8 years ago

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

Actions #4

Updated by kosaki (Motohiro KOSAKI) about 8 years ago

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

Updated by kosaki (Motohiro KOSAKI) about 8 years ago

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

Updated by sawa (Tsuyoshi Sawada) about 8 years 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).

Updated by phluid61 (Matthew Kerwin) about 8 years 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.

Updated by sawa (Tsuyoshi Sawada) about 8 years 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.

Updated by phluid61 (Matthew Kerwin) about 8 years 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.

Updated by kosaki (Motohiro KOSAKI) about 8 years ago

2016-03-14 21:06 GMT-04:00 :

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'.

Updated by avit (Andrew Vit) about 8 years 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.)

Updated by Eregon (Benoit Daloze) about 8 years 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 { ... }.

Updated by elifoster (Eli Clemente Gordillo Foster) about 8 years ago

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

Updated by shevegen (Robert A. Heiler) about 8 years 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.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0