Wolfram Research

Function Repository Resource:

SecondsToQuantity (1.1.0) current version: 1.2.0 »

Source Notebook

Convert a number representing seconds to a quantity of time in an appropriate unit

Contributed by: Richard Hennigan (Wolfram Research)

ResourceFunction["SecondsToQuantity"][int]

converts an integer int representing seconds to a Quantity object using an appropriate unit.

ResourceFunction["SecondsToQuantity"][Quantity[]]

converts a quantity of time into an appropriate compatible unit.

Details and Options

ResourceFunction["SecondsToQuantity"] accepts the following options:
"MixedUnits"Truewhether to allow MixedUnit in the output
"MaxMixedUnits"Automaticspecifies the maximum number of units to use in MixedUnit outputs

Examples

Basic Examples (2) 

Convert a number of seconds into a Quantity object:

In[1]:=
ResourceFunction["SecondsToQuantity"][12345]
Out[1]=

Convert a Quantity object representing time into an easier-to-read unit:

In[2]:=
q = Quantity[123456789.0, "Seconds"]
Out[2]=
In[3]:=
ResourceFunction["SecondsToQuantity"][q]
Out[3]=

Scope (1) 

For symbolic values, the result is just a Quantity of seconds:

In[4]:=
ResourceFunction["SecondsToQuantity"][f[x]]
Out[4]=
In[5]:=
ResourceFunction["SecondsToQuantity"][Quantity[f[x], "Minutes"]]
Out[5]=

Options (4) 

MixedUnits (2) 

By default, SecondsToQuantity can return MixedUnit in the output:

In[6]:=
ResourceFunction["SecondsToQuantity"][12345.6789]
Out[6]=

Get non-mixed units instead:

In[7]:=
ResourceFunction["SecondsToQuantity"][12345.6789, "MixedUnits" -> False]
Out[7]=

MaxMixedUnits (2) 

Specify that a maximum of two units should be used in MixedUnit outputs:

In[8]:=
ResourceFunction["SecondsToQuantity"][12345.6789, "MaxMixedUnits" -> 2]
Out[8]=

Compare to the default behavior:

In[9]:=
ResourceFunction["SecondsToQuantity"][12345.6789]
Out[9]=

Using "MaxMixedUnits"1 is effectively equivalent to "MixedUnits"False:

In[10]:=
ResourceFunction["SecondsToQuantity"][12345.6789, "MaxMixedUnits" -> 1]
Out[10]=
In[11]:=
ResourceFunction["SecondsToQuantity"][12345.6789, "MixedUnits" -> False]
Out[11]=

Applications (1) 

Improve the readability of the output for functions that return time as a number of seconds:

In[12]:=
SessionTime[]
Out[12]=
In[13]:=
ResourceFunction["SecondsToQuantity"][%]
Out[13]=

Possible Issues (1) 

Units must be compatible:

In[14]:=
ResourceFunction["SecondsToQuantity"][Quantity[500, "Miles"]]
Out[14]=

Version History

  • 1.2.0 – 06 March 2023
  • 1.1.0 – 04 March 2021
  • 1.0.0 – 27 January 2020

Related Resources

License Information