Function Repository Resource:

SecondsToQuantity

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 (3) 

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]=

Convert an approximate value represented as an Around object:

In[6]:=
ResourceFunction["SecondsToQuantity"][Around[1.3421, 0.012]]
Out[6]=

Convert a range of values represented by an Interval object:

In[7]:=
ResourceFunction["SecondsToQuantity"][Interval[{0.012, 1.3421}]]
Out[7]=

Options (5) 

MixedUnits (2) 

By default, SecondsToQuantity can return MixedUnit in the output:

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

Get non-mixed units instead:

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

MaxMixedUnits (3) 

By default, the number of units used in MixedUnit outputs is limited:

In[10]:=
ResourceFunction["SecondsToQuantity"][1234567890.0]
Out[10]=

Restrict to a maximum of two units:

In[11]:=
ResourceFunction["SecondsToQuantity"][1234567890.0, "MaxMixedUnits" -> 2]
Out[11]=

Show all available mixed units:

In[12]:=
ResourceFunction["SecondsToQuantity"][1234567890.0, "MaxMixedUnits" -> Infinity]
Out[12]=
In[13]:=
ResourceFunction["SecondsToQuantity"][12345.6789, "MaxMixedUnits" -> Infinity]
Out[13]=

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

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

Applications (2) 

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

In[16]:=
SessionTime[]
Out[16]=
In[17]:=
ResourceFunction["SecondsToQuantity"][%]
Out[17]=
In[18]:=
First[AbsoluteTiming[Inverse[RandomReal[{-1, 1}, {1000, 1000}]]]]
Out[18]=
In[19]:=
ResourceFunction["SecondsToQuantity"][%]
Out[19]=

Convert the number of seconds since January 1, 1970, in the GMT time zone to more readable units:

In[20]:=
ResourceFunction["SecondsToQuantity"][UnixTime[]] // N
Out[19]=

Properties and Relations (1) 

The resource function RelativeTimeString uses SecondsToQuantity to convert units:

In[21]:=
values = Quantity[#, "Seconds"] & /@ PowerRange[1, 2^28, 2]
Out[21]=
In[22]:=
TableForm[{ResourceFunction["RelativeTimeString"][#], Round[ResourceFunction["SecondsToQuantity"][#1, "MixedUnits" -> False]]} & /@ values]
Out[22]=

Possible Issues (3) 

MixedUnit values are not used for values that are very large or very small:

In[23]:=
ResourceFunction["SecondsToQuantity"][1234567890123456789.0, "MixedUnits" -> True, "MaxMixedUnits" -> Infinity]
Out[23]=
In[24]:=
ResourceFunction["SecondsToQuantity"][0.0000123456789, "MixedUnits" -> True, "MaxMixedUnits" -> Infinity]
Out[24]=

With the default setting of "MaxMixedUnits"Automatic, mixed units are not used for Interval or Around objects:

In[25]:=
ResourceFunction["SecondsToQuantity"][Around[1234567.89, 123.45]]
Out[25]=
In[26]:=
ResourceFunction["SecondsToQuantity"][Interval[{0.00001, 123.45}]]
Out[26]=

Specify a positive value to override the default behavior:

In[27]:=
ResourceFunction["SecondsToQuantity"][Around[1234567.89, 123.45], "MaxMixedUnits" -> 2]
Out[27]=
In[28]:=
ResourceFunction["SecondsToQuantity"][Interval[{0.00001, 123.45}], "MaxMixedUnits" -> 2]
Out[28]=

Units must be compatible:

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

Version History

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

Related Resources

License Information