UNIX tip of the day: units

A colleague at Automattic today shared an interesting post about a strange sorting bug in PHP, and another colleague shared one I had read before – about how „our email system cannot send emails over 500 miles„. I had remembered reading that one sometime in the last couple years, but couldn’t remember all the details, so I read it again. It turned out to be a timeout issue. Nevertheless, and interesting read. There is a little nugget at the end about converting units, in which the author converts millilightseconds to miles, using the units program. I had not heard of this program before. I decided to see if it was installed on my Mac OS X laptop. Sure enough it was! I tried it out a bit. It can do quite a few conversions. Having grown up in the USA and currently living in Germany, I find I do conversions relatively often, and end up doing them with Google. I think from now on I might more frequently use units, which is much faster.

robfelty$ units
586 units, 56 prefixes
You have: 1 ms
You want: s
conformability error
	1 m
	1 sec
You have: 2 inches
You want: centimeters
	* 5.08
	/ 0.19685039
You have: 2 euros
You want: dollars
conformability error
	2 euro
	1 usdollar
You have: 2 miles
You want: kilometers
	* 3.218688
	/ 0.3106856
You have: 5 km
You want: mile
	* 3.106856
	/ 0.3218688
You have: ^D
robfelty$ units
586 units, 56 prefixes
You have: 2 fluid ounces
unknown unit 'fluid'
You have: 2 fl. oz
unknown unit 'fl.'
You have: 12 ounces
You want: liter
conformability error
	0.34019428 kg
	0.001 m^3
You have: 2 cups
You want: liter
	* 0.47317647
	/ 2.1133764
You have: 1 gallon
You want: liter
	* 3.7854118
	/ 0.26417205
You have: 1 teaspoon
You want: milliliter
	* 4.9289216
	/ 0.20288414
You have: 1 pound
You want: kilogram
	* 0.45359237
	/ 2.2046226

There are some limitations – it doesn’t seem to know fluid ounces, and also can’t convert from Celsius to Fahrenheit, which is documented in the man page

The units program converts quantities expressed in various scales to
their equivalents in other scales. The units program can only handle
multiplicative scale changes. It cannot convert Celsius to Fahrenheit,
for example. It works interactively by prompting the user for input:

I checked to see if it was installed on the Linux machines I usually connect to, including Scientific Linux and Debian, and neither of those had it. According to the man page it was in BSD in 1993, so that probably explains why Mac OSX has it, since it based on BSD.