Last Modified
2020-04-01 14:03:43 -0700
Requires
  • json/common
  • json/version
  • json/ext
  • json/pure

Description

frozen_string_literal: false

Additional notes

There are two Time classes. There's one that is part of core Ruby and there is an additional Time class that is part of the standard library.

The standard library Time class extends the core Time class by adding some methods. If you are using the standard library Time class and cannot find documentation for a method, look at the API docs for the core Time class.

Common questions

Q: Given some number of seconds, how can that be converted into minutes and remainder seconds?

A: There are a few ways. Assume we start with 1234 seconds.

    Time.at(1234).strftime "%M:%S"   # This returns a String 

If you want numerical values, you can do some basic math:

    m, s = 1234/60, 1234%60

That second example may be a little too terse for general use. You can wrap it in a method:

    
  def minutes_and_remainder_seconds seconds
    [seconds/60, seconds%60]
  end

  minutes, seconds = *minutes_and_remainder_seconds(1234)
 

You may want to make this available to multiple classes. You can put it in a module ...

  module TimeUtilities

    def minutes_and_remainder_seconds seconds
      [seconds/60, seconds%60]
    end
    
  end
 

... and then include that module in those classes that use it:


   class Foo
     include TimeUtilities

     #  additional class stuff ....
   end
 

Thanks to Andrey Andreevich Ostapchuck and J Bolton for their suggestions on this.