Code
The function QuantityUnits`Private`QuantityUnitBox
handles the display form of quantities. We modify only the definition related to mixed magnitudes and mixed units:
(* From a fresh kernel *)
System`Quantity;
Unprotect[QuantityUnits`Private`QuantityUnitBox];
QuantityUnits`Private`QuantityUnitBox[
QuantityUnits`Private`QuantityUnitBox`q :
Quantity[
QuantityUnits`Private`QuantityUnitBox`m_MixedMagnitude,
QuantityUnits`Private`QuantityUnitBox`u_MixedUnit,
QuantityUnits`Private`QuantityUnitBox`opts___?OptionQ
],
QuantityUnits`Private`QuantityUnitBox`form_
] /; QuantityQ[QuantityUnits`Private`QuantityUnitBox`q] := With[
{q = QuantityUnits`Private`QuantityUnitBox`q,
form = QuantityUnits`Private`QuantityUnitBox`form,
m = First@QuantityUnits`Private`QuantityUnitBox`m,
u = First@QuantityUnits`Private`QuantityUnitBox`u},
If[u === {"Hours", "Minutes", "Seconds"} || u === {"Minutes", "Seconds"},
myQuantityDisplay[m, u],
QuantityUnits`Private`ToMixedUnitQuantityTemplateBox[q, form]
]
];
Protect[QuantityUnits`Private`QuantityUnitBox];
Compared to the original version, we just added an If
statement with a test on the form of the mixed units. The function myQuantityDisplay
will fire for OP's requirements only, otherwise the standard display will be kept.
(To change the display of any time quantities, probably UnitDimensions
and TimeUnit
could be used in some way for the test.)
We are left with the definition of myQuantityDisplay
myQuantityDisplay[{m_, s_}, {"Minutes", "Seconds"}] :=
IntegerString[m, 10, 2] <> ":" <> IntegerString[s, 10, 2] <> "s";
myQuantityDisplay[{0, m_, s_}, {"Hours", "Minutes", "Seconds"}] :=
IntegerString[m, 10, 2] <> ":" <> IntegerString[s, 10, 2] <> "s";
myQuantityDisplay[{h_, m_, s_}, {"Hours", "Minutes", "Seconds"}] :=
IntegerString[h, 10, 2] <> ":" <> IntegerString[m, 10, 2] <> ":" <>
IntegerString[s, 10, 2] <> "s";
The above modification only affects the display form, so that we can still perform any operation or standard evaluation on mixed quantites. Also, as mentioned, only the mixed units specifications given in the If
statement are concerned.
(See Comments below for point 3 of OP's question.)
A few examples
a) Modified display
Quantity[MixedMagnitude[{1, 2, 3}], MixedUnit[{"Hours", "Minutes", "Seconds"}]]
% // FullForm

b) Standard display
Quantity[MixedMagnitude[{14, 35}], MixedUnit[{"Hours", "Minutes"}]]

c) Arithmetic operations
q = Quantity[MixedMagnitude[{14, 35}], MixedUnit[{"Minutes", "Seconds"}]]
3 q

Comments
The definitions of the function QuantityUnits`Private`QuantityUnitBox
can be accessed with PrintDefinitions
. Otherwise, from the PrintDefinitions
of Quantity
, one has to search for the function QuantityUnits`QuantityBox
in
MakeBoxes[x : Quantity[_, u_?QuantityUnits`Private`LooksLikeAUnitQ, ___], fmt_] ...
and from its definitions look at the second cell.
Among all the definitions of QuantityUnits`Private`QuantityUnitBox
, we can see one responsible for the format of mixed quantities (modified above), and one that concerns the display (or lack of it) for repeated zeros in the mixed magnitude. So to achieve point 3 of OP's question, simply do
QuantityUnits`Private`$ShowZero = True;
Note that the function myQuantityDisplay
should have definitions that are within the scope of MakeBoxes
(as we can see from above). I think we can get more fancy outputs than the one I gave by tweaking instead the functions used by QuantityUnits`Private`ToMixedUnitQuantityTemplateBox
, though this requires more spelunking.