Dates in the Terminal
I’ve recently worked on a project where part of the team was based in Eastern Europe and some in Minnesota. We had to offer support throughout the US (as far west as Hawaii). And of course, our logging and data entries were all done in Coordinated Universal Time. So there was a lot of time conversion going on!
For a while, I “solved” this problem by constantly Googling UTC. That got cumbersome pretty fast! So, I set up a clock widget with the different time zones in my Mac’s Notification Center. That was better. I could open the Notification Center with a keyboard shortcut (the default, always-on shortcut while using a Mac keyboard is Fn-n
, but it can also be set in the Keyboard Shortcuts of your Mac settings). Still, the admittedly very pretty widget uses analog displays, which takes longer to read, and there doesn’t seem to be a way to get a digital display - at least, I gave up after fiddling with it for a while. And that’s when I discovered date
in the terminal.
UTC in the terminal
Like all good terminal commands, date
has several functions. Simply running
1
date
will print today’s date for the system time (for the time zone you have set), e.g. Mon Apr 22 06:03:18 CDT 2024
. But the very handy flag -u
will print out UTC!
1
date -u
The above, for the same time I mentioned before, would be Mon Apr 22 11:03:18 UTC 2024
for the Twin Cities (which is UTC-5). I liked this so much that I aliased it as:
1
alias utc="date -u"
Yes, I know - this is only a few characters shorter. But it saves me from typing a hyphen, and I’m lazy like that. :)
Different time zones
Of course, this doesn’t solve the problem of knowing what the current time is elsewhere. But date
can help here too! By temporarily changing the TZ
(i.e. time zone) environment variable, date will print the date and time in that time zone.
1
TZ=europe/sofia date
will print the time for Sofia, Bulgaria, e.g. Mon Apr 22 14:03:18 EEST 2024
.
Zone info
The time zone information is kept in /usr/share/zoneinfo/
on Unix-like systems, if you’re curious to see what options there are. Different distributions handle setting and getting the time zone differently, so it will probably be safest to enter the full path (e.g. europe/sofia
) rather than just the city, although I believe on some distributions just the city would do the trick too.